^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * NETLINK Kernel-user communication protocol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Patrick McHardy <kaber@trash.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * added netlink_proto_exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * use nlk_sk, as sk->protinfo is on a diet 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * - inc module use count of module that owns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * the kernel socket in case userspace opens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * socket of same protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * - remove all module support, since netlink is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * mandatory if CONFIG_NET=y these days
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/un.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/termios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/jhash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/audit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/rhashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <linux/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <linux/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <linux/nospec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/btf_ids.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include <net/scm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #include "af_netlink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct listeners {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned long masks[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* state bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define NETLINK_S_CONGESTED 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static inline int netlink_is_kernel(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct netlink_table *nl_table __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) EXPORT_SYMBOL_GPL(nl_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static struct lock_class_key nlk_cb_mutex_keys[MAX_LINKS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static const char *const nlk_cb_mutex_key_strings[MAX_LINKS + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) "nlk_cb_mutex-ROUTE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) "nlk_cb_mutex-1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) "nlk_cb_mutex-USERSOCK",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) "nlk_cb_mutex-FIREWALL",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) "nlk_cb_mutex-SOCK_DIAG",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) "nlk_cb_mutex-NFLOG",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) "nlk_cb_mutex-XFRM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) "nlk_cb_mutex-SELINUX",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) "nlk_cb_mutex-ISCSI",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "nlk_cb_mutex-AUDIT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) "nlk_cb_mutex-FIB_LOOKUP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) "nlk_cb_mutex-CONNECTOR",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) "nlk_cb_mutex-NETFILTER",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) "nlk_cb_mutex-IP6_FW",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) "nlk_cb_mutex-DNRTMSG",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) "nlk_cb_mutex-KOBJECT_UEVENT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) "nlk_cb_mutex-GENERIC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) "nlk_cb_mutex-17",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "nlk_cb_mutex-SCSITRANSPORT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) "nlk_cb_mutex-ECRYPTFS",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "nlk_cb_mutex-RDMA",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "nlk_cb_mutex-CRYPTO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) "nlk_cb_mutex-SMC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) "nlk_cb_mutex-23",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) "nlk_cb_mutex-24",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "nlk_cb_mutex-25",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) "nlk_cb_mutex-26",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) "nlk_cb_mutex-27",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) "nlk_cb_mutex-28",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) "nlk_cb_mutex-29",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) "nlk_cb_mutex-30",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) "nlk_cb_mutex-31",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) "nlk_cb_mutex-MAX_LINKS"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static int netlink_dump(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* nl_table locking explained:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Lookup and traversal are protected with an RCU read-side lock. Insertion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * and removal are protected with per bucket lock while using RCU list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * modification primitives and may run in parallel to RCU protected lookups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * Destruction of the Netlink socket may only occur *after* nl_table_lock has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * been acquired * either during or after the socket has been removed from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * the list and after an RCU grace period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) DEFINE_RWLOCK(nl_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) EXPORT_SYMBOL_GPL(nl_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static atomic_t nl_table_users = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static BLOCKING_NOTIFIER_HEAD(netlink_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static const struct rhashtable_params netlink_rhashtable_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static inline u32 netlink_group_mask(u32 group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (group > 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return group ? 1 << (group - 1) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct sk_buff *netlink_to_full_skb(const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) unsigned int len = skb_end_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct sk_buff *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) new = alloc_skb(len, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (new == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) NETLINK_CB(new).portid = NETLINK_CB(skb).portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) NETLINK_CB(new).dst_group = NETLINK_CB(skb).dst_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) NETLINK_CB(new).creds = NETLINK_CB(skb).creds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) skb_put_data(new, skb->data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static unsigned int netlink_tap_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct netlink_tap_net {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct list_head netlink_tap_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct mutex netlink_tap_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int netlink_add_tap(struct netlink_tap *nt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct net *net = dev_net(nt->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct netlink_tap_net *nn = net_generic(net, netlink_tap_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (unlikely(nt->dev->type != ARPHRD_NETLINK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mutex_lock(&nn->netlink_tap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) list_add_rcu(&nt->list, &nn->netlink_tap_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) mutex_unlock(&nn->netlink_tap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) __module_get(nt->module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) EXPORT_SYMBOL_GPL(netlink_add_tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int __netlink_remove_tap(struct netlink_tap *nt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct net *net = dev_net(nt->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct netlink_tap_net *nn = net_generic(net, netlink_tap_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct netlink_tap *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) mutex_lock(&nn->netlink_tap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) list_for_each_entry(tmp, &nn->netlink_tap_all, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (nt == tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) list_del_rcu(&nt->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) pr_warn("__netlink_remove_tap: %p not found\n", nt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) mutex_unlock(&nn->netlink_tap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) module_put(nt->module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return found ? 0 : -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int netlink_remove_tap(struct netlink_tap *nt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ret = __netlink_remove_tap(nt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) EXPORT_SYMBOL_GPL(netlink_remove_tap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static __net_init int netlink_tap_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct netlink_tap_net *nn = net_generic(net, netlink_tap_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) INIT_LIST_HEAD(&nn->netlink_tap_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) mutex_init(&nn->netlink_tap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static struct pernet_operations netlink_tap_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) .init = netlink_tap_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) .id = &netlink_tap_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) .size = sizeof(struct netlink_tap_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static bool netlink_filter_tap(const struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct sock *sk = skb->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* We take the more conservative approach and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * whitelist socket protocols that may pass.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) switch (sk->sk_protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) case NETLINK_ROUTE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case NETLINK_USERSOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) case NETLINK_SOCK_DIAG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) case NETLINK_NFLOG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) case NETLINK_XFRM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) case NETLINK_FIB_LOOKUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) case NETLINK_NETFILTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) case NETLINK_GENERIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int __netlink_deliver_tap_skb(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct sk_buff *nskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct sock *sk = skb->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!net_eq(dev_net(dev), sock_net(sk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (is_vmalloc_addr(skb->head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) nskb = netlink_to_full_skb(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) nskb = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (nskb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) nskb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) nskb->protocol = htons((u16) sk->sk_protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) nskb->pkt_type = netlink_is_kernel(sk) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) PACKET_KERNEL : PACKET_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) skb_reset_network_header(nskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ret = dev_queue_xmit(nskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (unlikely(ret > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) ret = net_xmit_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static void __netlink_deliver_tap(struct sk_buff *skb, struct netlink_tap_net *nn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct netlink_tap *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (!netlink_filter_tap(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) list_for_each_entry_rcu(tmp, &nn->netlink_tap_all, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) ret = __netlink_deliver_tap_skb(skb, tmp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static void netlink_deliver_tap(struct net *net, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct netlink_tap_net *nn = net_generic(net, netlink_tap_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (unlikely(!list_empty(&nn->netlink_tap_all)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) __netlink_deliver_tap(skb, nn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) netlink_deliver_tap(sock_net(dst), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static void netlink_overrun(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (!test_and_set_bit(NETLINK_S_CONGESTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) &nlk_sk(sk)->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) sk->sk_err = ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) atomic_inc(&sk->sk_drops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static void netlink_rcv_wake(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (skb_queue_empty_lockless(&sk->sk_receive_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) clear_bit(NETLINK_S_CONGESTED, &nlk->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!test_bit(NETLINK_S_CONGESTED, &nlk->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) wake_up_interruptible(&nlk->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static void netlink_skb_destructor(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (is_vmalloc_addr(skb->head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (!skb->cloned ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) vfree(skb->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) skb->head = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (skb->sk != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) sock_rfree(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) WARN_ON(skb->sk != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) skb->sk = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) skb->destructor = netlink_skb_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) atomic_add(skb->truesize, &sk->sk_rmem_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) sk_mem_charge(sk, skb->truesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void netlink_sock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (nlk->cb_running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (nlk->cb.done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) nlk->cb.done(&nlk->cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) module_put(nlk->cb.module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) kfree_skb(nlk->cb.skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) skb_queue_purge(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (!sock_flag(sk, SOCK_DEAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) WARN_ON(atomic_read(&sk->sk_rmem_alloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) WARN_ON(refcount_read(&sk->sk_wmem_alloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) WARN_ON(nlk_sk(sk)->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static void netlink_sock_destruct_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct netlink_sock *nlk = container_of(work, struct netlink_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) sk_free(&nlk->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * SMP. Look, when several writers sleep and reader wakes them up, all but one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * immediately hit write lock and grab all the cpus. Exclusive sleep solves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * this, _but_ remember, it adds useless work on UP machines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) void netlink_table_grab(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) __acquires(nl_table_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) write_lock_irq(&nl_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (atomic_read(&nl_table_users)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) add_wait_queue_exclusive(&nl_table_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (atomic_read(&nl_table_users) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) write_unlock_irq(&nl_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) write_lock_irq(&nl_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) remove_wait_queue(&nl_table_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) void netlink_table_ungrab(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) __releases(nl_table_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) write_unlock_irq(&nl_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) wake_up(&nl_table_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) netlink_lock_table(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* read_lock() synchronizes us to netlink_table_grab */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) read_lock_irqsave(&nl_table_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) atomic_inc(&nl_table_users);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) read_unlock_irqrestore(&nl_table_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) netlink_unlock_table(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (atomic_dec_and_test(&nl_table_users))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) wake_up(&nl_table_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct netlink_compare_arg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) possible_net_t pnet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) u32 portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) #define netlink_compare_arg_len \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static inline int netlink_compare(struct rhashtable_compare_arg *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) const void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) const struct netlink_compare_arg *x = arg->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) const struct netlink_sock *nlk = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return nlk->portid != x->portid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct net *net, u32 portid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) memset(arg, 0, sizeof(*arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) write_pnet(&arg->pnet, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) arg->portid = portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct netlink_compare_arg arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) netlink_compare_arg_init(&arg, net, portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return rhashtable_lookup_fast(&table->hash, &arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) netlink_rhashtable_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static int __netlink_insert(struct netlink_table *table, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct netlink_compare_arg arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return rhashtable_lookup_insert_key(&table->hash, &arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) &nlk_sk(sk)->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) netlink_rhashtable_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct netlink_table *table = &nl_table[protocol];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) sk = __netlink_lookup(table, portid, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static const struct proto_ops netlink_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) netlink_update_listeners(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct netlink_table *tbl = &nl_table[sk->sk_protocol];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) unsigned long mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) struct listeners *listeners;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) listeners = nl_deref_protected(tbl->listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (!listeners)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) sk_for_each_bound(sk, &tbl->mc_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) mask |= nlk_sk(sk)->groups[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) listeners->masks[i] = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /* this function is only called with the netlink table "grabbed", which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * makes sure updates are visible before bind or setsockopt return. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) static int netlink_insert(struct sock *sk, u32 portid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct netlink_table *table = &nl_table[sk->sk_protocol];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) err = nlk_sk(sk)->portid == portid ? 0 : -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (nlk_sk(sk)->bound)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) nlk_sk(sk)->portid = portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) err = __netlink_insert(table, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* In case the hashtable backend returns with -EBUSY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * from here, it must not escape to the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (unlikely(err == -EBUSY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) err = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (err == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) err = -EADDRINUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) /* We need to ensure that the socket is hashed and visible. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) /* Paired with lockless reads from netlink_bind(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * netlink_connect() and netlink_sendmsg().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) WRITE_ONCE(nlk_sk(sk)->bound, portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static void netlink_remove(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct netlink_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) table = &nl_table[sk->sk_protocol];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) netlink_rhashtable_params)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) __sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) netlink_table_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (nlk_sk(sk)->subscriptions) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) __sk_del_bind_node(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) netlink_update_listeners(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (sk->sk_protocol == NETLINK_GENERIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) atomic_inc(&genl_sk_destructing_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) netlink_table_ungrab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static struct proto netlink_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) .name = "NETLINK",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) .obj_size = sizeof(struct netlink_sock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static int __netlink_create(struct net *net, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) struct mutex *cb_mutex, int protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) struct netlink_sock *nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) sock->ops = &netlink_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) sock_init_data(sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (cb_mutex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) nlk->cb_mutex = cb_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) nlk->cb_mutex = &nlk->cb_def_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) mutex_init(nlk->cb_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) lockdep_set_class_and_name(nlk->cb_mutex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) nlk_cb_mutex_keys + protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) nlk_cb_mutex_key_strings[protocol]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) init_waitqueue_head(&nlk->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) sk->sk_destruct = netlink_sock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) sk->sk_protocol = protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int netlink_create(struct net *net, struct socket *sock, int protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) struct module *module = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct mutex *cb_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) struct netlink_sock *nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) int (*bind)(struct net *net, int group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) void (*unbind)(struct net *net, int group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) sock->state = SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) return -ESOCKTNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (protocol < 0 || protocol >= MAX_LINKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) return -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) protocol = array_index_nospec(protocol, MAX_LINKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) netlink_lock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) #ifdef CONFIG_MODULES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (!nl_table[protocol].registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) netlink_unlock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) netlink_lock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (nl_table[protocol].registered &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) try_module_get(nl_table[protocol].module))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) module = nl_table[protocol].module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) err = -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) cb_mutex = nl_table[protocol].cb_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) bind = nl_table[protocol].bind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) unbind = nl_table[protocol].unbind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) netlink_unlock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) err = __netlink_create(net, sock, cb_mutex, protocol, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) goto out_module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) sock_prot_inuse_add(net, &netlink_proto, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) nlk = nlk_sk(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) nlk->module = module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) nlk->netlink_bind = bind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) nlk->netlink_unbind = unbind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) out_module:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) module_put(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) static void deferred_put_nlk_sk(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) struct sock *sk = &nlk->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) kfree(nlk->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) nlk->groups = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (!refcount_dec_and_test(&sk->sk_refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (nlk->cb_running && nlk->cb.done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) INIT_WORK(&nlk->work, netlink_sock_destruct_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) schedule_work(&nlk->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) sk_free(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static int netlink_release(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) struct netlink_sock *nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) netlink_remove(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) sock_orphan(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * OK. Socket is unlinked, any packets that arrive now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * will be purged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) /* must not acquire netlink_table_lock in any way again before unbind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * and notifying genetlink is done as otherwise it might deadlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (nlk->netlink_unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) for (i = 0; i < nlk->ngroups; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (test_bit(i, nlk->groups))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) nlk->netlink_unbind(sock_net(sk), i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (sk->sk_protocol == NETLINK_GENERIC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) atomic_dec_return(&genl_sk_destructing_cnt) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) wake_up(&genl_sk_destructing_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) sock->sk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) wake_up_interruptible_all(&nlk->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) skb_queue_purge(&sk->sk_write_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (nlk->portid && nlk->bound) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) struct netlink_notify n = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) .net = sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) .protocol = sk->sk_protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) .portid = nlk->portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) blocking_notifier_call_chain(&netlink_chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) NETLINK_URELEASE, &n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) module_put(nlk->module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (netlink_is_kernel(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) netlink_table_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) BUG_ON(nl_table[sk->sk_protocol].registered == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (--nl_table[sk->sk_protocol].registered == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) struct listeners *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) kfree_rcu(old, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) nl_table[sk->sk_protocol].module = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) nl_table[sk->sk_protocol].bind = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) nl_table[sk->sk_protocol].unbind = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) nl_table[sk->sk_protocol].flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) nl_table[sk->sk_protocol].registered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) netlink_table_ungrab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) call_rcu(&nlk->rcu, deferred_put_nlk_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) static int netlink_autobind(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) struct netlink_table *table = &nl_table[sk->sk_protocol];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) s32 portid = task_tgid_vnr(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) s32 rover = -4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) bool ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) ok = !__netlink_lookup(table, portid, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (!ok) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) /* Bind collision, search negative portid values. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (rover == -4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /* rover will be in range [S32_MIN, -4097] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) else if (rover >= -4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) rover = -4097;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) portid = rover--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) err = netlink_insert(sk, portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) if (err == -EADDRINUSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) /* If 2 threads race to autobind, that is fine. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (err == -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * __netlink_ns_capable - General netlink message capability test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) * @user_ns: The user namespace of the capability to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * @cap: The capability to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * Test to see if the opener of the socket we received the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * from had when the netlink socket was created and the sender of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * message has the capability @cap in the user namespace @user_ns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) struct user_namespace *user_ns, int cap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return ((nsp->flags & NETLINK_SKB_DST) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) ns_capable(user_ns, cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) EXPORT_SYMBOL(__netlink_ns_capable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) * netlink_ns_capable - General netlink message capability test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) * @skb: socket buffer holding a netlink command from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) * @user_ns: The user namespace of the capability to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) * @cap: The capability to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) * Test to see if the opener of the socket we received the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * from had when the netlink socket was created and the sender of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * message has the capability @cap in the user namespace @user_ns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) bool netlink_ns_capable(const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) struct user_namespace *user_ns, int cap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) EXPORT_SYMBOL(netlink_ns_capable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * netlink_capable - Netlink global message capability test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * @skb: socket buffer holding a netlink command from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * @cap: The capability to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) * Test to see if the opener of the socket we received the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) * from had when the netlink socket was created and the sender of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) * message has the capability @cap in all user namespaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) bool netlink_capable(const struct sk_buff *skb, int cap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) return netlink_ns_capable(skb, &init_user_ns, cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) EXPORT_SYMBOL(netlink_capable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * netlink_net_capable - Netlink network namespace message capability test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * @skb: socket buffer holding a netlink command from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) * @cap: The capability to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) * Test to see if the opener of the socket we received the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) * from had when the netlink socket was created and the sender of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) * message has the capability @cap over the network namespace of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) * the socket we received the message from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) bool netlink_net_capable(const struct sk_buff *skb, int cap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) EXPORT_SYMBOL(netlink_net_capable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return (nl_table[sock->sk->sk_protocol].flags & flag) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (nlk->subscriptions && !subscriptions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) __sk_del_bind_node(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) else if (!nlk->subscriptions && subscriptions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) nlk->subscriptions = subscriptions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) static int netlink_realloc_groups(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) unsigned int groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) unsigned long *new_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) netlink_table_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) groups = nl_table[sk->sk_protocol].groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (!nl_table[sk->sk_protocol].registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (nlk->ngroups >= groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (new_groups == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) nlk->groups = new_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) nlk->ngroups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) netlink_table_ungrab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) static void netlink_undo_bind(int group, long unsigned int groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) int undo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) if (!nlk->netlink_unbind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) for (undo = 0; undo < group; undo++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (test_bit(undo, &groups))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) nlk->netlink_unbind(sock_net(sk), undo + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) static int netlink_bind(struct socket *sock, struct sockaddr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) int addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) unsigned long groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) bool bound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (addr_len < sizeof(struct sockaddr_nl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (nladdr->nl_family != AF_NETLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) groups = nladdr->nl_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) /* Only superuser is allowed to listen multicasts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) if (groups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) err = netlink_realloc_groups(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) if (nlk->ngroups < BITS_PER_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) groups &= (1UL << nlk->ngroups) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) /* Paired with WRITE_ONCE() in netlink_insert() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) bound = READ_ONCE(nlk->bound);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) if (bound) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) /* Ensure nlk->portid is up-to-date. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (nladdr->nl_pid != nlk->portid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) netlink_lock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) if (nlk->netlink_bind && groups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) int group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) /* nl_groups is a u32, so cap the maximum groups we can bind */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) for (group = 0; group < BITS_PER_TYPE(u32); group++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (!test_bit(group, &groups))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) err = nlk->netlink_bind(net, group + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) netlink_undo_bind(group, groups, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) /* No need for barriers here as we return to user-space without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) * using any of the bound attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (!bound) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) err = nladdr->nl_pid ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) netlink_insert(sk, nladdr->nl_pid) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) netlink_autobind(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) netlink_undo_bind(BITS_PER_TYPE(u32), groups, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) netlink_unlock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) netlink_table_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) netlink_update_subscriptions(sk, nlk->subscriptions +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) hweight32(groups) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) hweight32(nlk->groups[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) netlink_update_listeners(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) netlink_table_ungrab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) netlink_unlock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) static int netlink_connect(struct socket *sock, struct sockaddr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) int alen, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (alen < sizeof(addr->sa_family))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (addr->sa_family == AF_UNSPEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) sk->sk_state = NETLINK_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) nlk->dst_portid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) nlk->dst_group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if (addr->sa_family != AF_NETLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) if (alen < sizeof(struct sockaddr_nl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if ((nladdr->nl_groups || nladdr->nl_pid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) /* No need for barriers here as we return to user-space without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * using any of the bound attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * Paired with WRITE_ONCE() in netlink_insert().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) if (!READ_ONCE(nlk->bound))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) err = netlink_autobind(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) sk->sk_state = NETLINK_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) nlk->dst_portid = nladdr->nl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) nlk->dst_group = ffs(nladdr->nl_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) static int netlink_getname(struct socket *sock, struct sockaddr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) int peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) nladdr->nl_family = AF_NETLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) nladdr->nl_pad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) nladdr->nl_pid = nlk->dst_portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) nladdr->nl_pid = nlk->portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) netlink_lock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) netlink_unlock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) return sizeof(*nladdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) static int netlink_ioctl(struct socket *sock, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) /* try to hand this ioctl down to the NIC drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) struct sock *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) struct netlink_sock *nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (!sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) return ERR_PTR(-ECONNREFUSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) /* Don't bother queuing skb if kernel socket has no input function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) nlk = nlk_sk(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) if (sock->sk_state == NETLINK_CONNECTED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) nlk->dst_portid != nlk_sk(ssk)->portid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) sock_put(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) return ERR_PTR(-ECONNREFUSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) return sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) struct sock *netlink_getsockbyfilp(struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) struct sock *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (!S_ISSOCK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) return ERR_PTR(-ENOTSOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) sock = SOCKET_I(inode)->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) if (sock->sk_family != AF_NETLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) sock_hold(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) return sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) int broadcast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) if (size <= NLMSG_GOODSIZE || broadcast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) return alloc_skb(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) size = SKB_DATA_ALIGN(size) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) data = vmalloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) skb = __build_skb(data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) vfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) skb->destructor = netlink_skb_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * Attach a skb to a netlink socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) * The caller must hold a reference to the destination socket. On error, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) * reference is dropped. The skb is not send to the destination, just all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) * all error checks are performed and memory in the queue is reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) * Return values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) * < 0: error. skb freed, reference to sock dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) * 0: continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * 1: repeat lookup - reference dropped while waiting for socket memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) long *timeo, struct sock *ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) struct netlink_sock *nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) if (!*timeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) if (!ssk || netlink_is_kernel(ssk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) netlink_overrun(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) __set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) add_wait_queue(&nlk->wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) !sock_flag(sk, SOCK_DEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) *timeo = schedule_timeout(*timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) remove_wait_queue(&nlk->wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) return sock_intr_errno(*timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) netlink_skb_set_owner_r(skb, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) int len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) netlink_deliver_tap(sock_net(sk), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) skb_queue_tail(&sk->sk_receive_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) sk->sk_data_ready(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) int len = __netlink_sendskb(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) int delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) WARN_ON(skb->sk != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) delta = skb->end - skb->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) if (skb_shared(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) struct sk_buff *nskb = skb_clone(skb, allocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) if (!nskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) skb = nskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) pskb_expand_head(skb, 0, -delta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) (allocation & ~__GFP_DIRECT_RECLAIM) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) __GFP_NOWARN | __GFP_NORETRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) struct sock *ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) ret = -ECONNREFUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) if (nlk->netlink_rcv != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) ret = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) netlink_skb_set_owner_r(skb, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) NETLINK_CB(skb).sk = ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) netlink_deliver_tap_kernel(sk, ssk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) nlk->netlink_rcv(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) u32 portid, int nonblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) skb = netlink_trim(skb, gfp_any());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) timeo = sock_sndtimeo(ssk, nonblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) sk = netlink_getsockbyportid(ssk, portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) if (IS_ERR(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) return PTR_ERR(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) if (netlink_is_kernel(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) return netlink_unicast_kernel(sk, skb, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) if (sk_filter(sk, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) err = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) err = netlink_attachskb(sk, skb, &timeo, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) if (err == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) return netlink_sendskb(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) EXPORT_SYMBOL(netlink_unicast);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) int netlink_has_listeners(struct sock *sk, unsigned int group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) struct listeners *listeners;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) BUG_ON(!netlink_is_kernel(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) res = test_bit(group - 1, listeners->masks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) EXPORT_SYMBOL_GPL(netlink_has_listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) bool netlink_strict_get_check(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) const struct netlink_sock *nlk = nlk_sk(NETLINK_CB(skb).sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) return nlk->flags & NETLINK_F_STRICT_CHK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) EXPORT_SYMBOL_GPL(netlink_strict_get_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) netlink_skb_set_owner_r(skb, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) __netlink_sendskb(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) struct netlink_broadcast_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) struct sock *exclude_sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) u32 portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) u32 group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) int failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) int delivery_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) int congested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) int delivered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) gfp_t allocation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) struct sk_buff *skb, *skb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) void *tx_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) static void do_one_broadcast(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) struct netlink_broadcast_data *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) if (p->exclude_sk == sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) !test_bit(p->group - 1, nlk->groups))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) if (!net_eq(sock_net(sk), p->net)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) if (!peernet_has_id(sock_net(sk), p->net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) CAP_NET_BROADCAST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) if (p->failure) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) netlink_overrun(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) if (p->skb2 == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) if (skb_shared(p->skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) p->skb2 = skb_clone(p->skb, p->allocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) p->skb2 = skb_get(p->skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) * skb ownership may have been set when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) * delivered to a previous socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) skb_orphan(p->skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) if (p->skb2 == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) netlink_overrun(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) /* Clone failed. Notify ALL listeners. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) p->failure = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) p->delivery_failure = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) kfree_skb(p->skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) p->skb2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) if (sk_filter(sk, p->skb2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) kfree_skb(p->skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) p->skb2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) if (NETLINK_CB(p->skb2).nsid != NETNSA_NSID_NOT_ASSIGNED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) NETLINK_CB(p->skb2).nsid_is_set = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) val = netlink_broadcast_deliver(sk, p->skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) if (val < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) netlink_overrun(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) p->delivery_failure = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) p->congested |= val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) p->delivered = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) p->skb2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) u32 group, gfp_t allocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) void *filter_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) struct net *net = sock_net(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) struct netlink_broadcast_data info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) skb = netlink_trim(skb, allocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) info.exclude_sk = ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) info.net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) info.portid = portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) info.group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) info.failure = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) info.delivery_failure = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) info.congested = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) info.delivered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) info.allocation = allocation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) info.skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) info.skb2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) info.tx_filter = filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) info.tx_data = filter_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) /* While we sleep in clone, do not allow to change socket list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) netlink_lock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) do_one_broadcast(sk, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) netlink_unlock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) if (info.delivery_failure) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) kfree_skb(info.skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) consume_skb(info.skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) if (info.delivered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) if (info.congested && gfpflags_allow_blocking(allocation))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) yield();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) EXPORT_SYMBOL(netlink_broadcast_filtered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) u32 group, gfp_t allocation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) EXPORT_SYMBOL(netlink_broadcast);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) struct netlink_set_err_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) struct sock *exclude_sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) u32 portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) u32 group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) int code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) if (sk == p->exclude_sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) !test_bit(p->group - 1, nlk->groups))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) sk->sk_err = p->code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) * netlink_set_err - report error to broadcast listeners
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) * @portid: the PORTID of a process that we want to skip (if any)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) * @group: the broadcast group that will notice the error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) * @code: error code, must be negative (as usual in kernelspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) * This function returns the number of broadcast listeners that have set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) * NETLINK_NO_ENOBUFS socket option.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) struct netlink_set_err_data info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) info.exclude_sk = ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) info.portid = portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) info.group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) /* sk->sk_err wants a positive error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) info.code = -code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) read_lock(&nl_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) ret += do_one_set_err(sk, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) read_unlock(&nl_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) EXPORT_SYMBOL(netlink_set_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) /* must be called with netlink table grabbed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) static void netlink_update_socket_mc(struct netlink_sock *nlk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) unsigned int group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) int is_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) int old, new = !!is_new, subscriptions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) old = test_bit(group - 1, nlk->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) subscriptions = nlk->subscriptions - old + new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) if (new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) __set_bit(group - 1, nlk->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) __clear_bit(group - 1, nlk->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) netlink_update_subscriptions(&nlk->sk, subscriptions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) netlink_update_listeners(&nlk->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) static int netlink_setsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) unsigned int val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) if (level != SOL_NETLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) if (optlen >= sizeof(int) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) copy_from_sockptr(&val, optval, sizeof(val)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) case NETLINK_PKTINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) nlk->flags |= NETLINK_F_RECV_PKTINFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) case NETLINK_ADD_MEMBERSHIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) case NETLINK_DROP_MEMBERSHIP: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) err = netlink_realloc_groups(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) if (!val || val - 1 >= nlk->ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) err = nlk->netlink_bind(sock_net(sk), val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) netlink_table_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) netlink_update_socket_mc(nlk, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) optname == NETLINK_ADD_MEMBERSHIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) netlink_table_ungrab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) nlk->netlink_unbind(sock_net(sk), val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) case NETLINK_BROADCAST_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) case NETLINK_NO_ENOBUFS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) clear_bit(NETLINK_S_CONGESTED, &nlk->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) wake_up_interruptible(&nlk->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) case NETLINK_LISTEN_ALL_NSID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) case NETLINK_CAP_ACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) nlk->flags |= NETLINK_F_CAP_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) nlk->flags &= ~NETLINK_F_CAP_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) case NETLINK_EXT_ACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) nlk->flags |= NETLINK_F_EXT_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) nlk->flags &= ~NETLINK_F_EXT_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) case NETLINK_GET_STRICT_CHK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) nlk->flags |= NETLINK_F_STRICT_CHK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) nlk->flags &= ~NETLINK_F_STRICT_CHK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) static int netlink_getsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) char __user *optval, int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) int len, val, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) if (level != SOL_NETLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) if (get_user(len, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) case NETLINK_PKTINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) if (len < sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) if (put_user(len, optlen) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) put_user(val, optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) case NETLINK_BROADCAST_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) if (len < sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) if (put_user(len, optlen) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) put_user(val, optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) case NETLINK_NO_ENOBUFS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) if (len < sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) if (put_user(len, optlen) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) put_user(val, optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) case NETLINK_LIST_MEMBERSHIPS: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) int pos, idx, shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) netlink_lock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) if (len - pos < sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) idx = pos / sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) shift = (pos % sizeof(unsigned long)) * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) if (put_user((u32)(nlk->groups[idx] >> shift),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) (u32 __user *)(optval + pos))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) netlink_unlock_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) case NETLINK_CAP_ACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) if (len < sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) if (put_user(len, optlen) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) put_user(val, optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) case NETLINK_EXT_ACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) if (len < sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) if (put_user(len, optlen) || put_user(val, optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) case NETLINK_GET_STRICT_CHK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) if (len < sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) val = nlk->flags & NETLINK_F_STRICT_CHK ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) if (put_user(len, optlen) || put_user(val, optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) struct nl_pktinfo info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) info.group = NETLINK_CB(skb).dst_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) if (!NETLINK_CB(skb).nsid_is_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) &NETLINK_CB(skb).nsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) u32 dst_portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) u32 dst_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) struct scm_cookie scm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) u32 netlink_skb_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) if (msg->msg_flags & MSG_OOB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) pr_warn_once("Zero length message leads to an empty skb\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) err = scm_send(sock, msg, &scm, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) if (msg->msg_namelen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) if (msg->msg_namelen < sizeof(struct sockaddr_nl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (addr->nl_family != AF_NETLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) dst_portid = addr->nl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) dst_group = ffs(addr->nl_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) if ((dst_group || dst_portid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) netlink_skb_flags |= NETLINK_SKB_DST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) dst_portid = nlk->dst_portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) dst_group = nlk->dst_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) /* Paired with WRITE_ONCE() in netlink_insert() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) if (!READ_ONCE(nlk->bound)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) err = netlink_autobind(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) /* Ensure nlk is hashed and visible. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) if (len > sk->sk_sndbuf - 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) skb = netlink_alloc_large_skb(len, dst_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) NETLINK_CB(skb).portid = nlk->portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) NETLINK_CB(skb).dst_group = dst_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) NETLINK_CB(skb).creds = scm.creds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) NETLINK_CB(skb).flags = netlink_skb_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) err = security_netlink_send(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) if (dst_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) refcount_inc(&skb->users);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags & MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) scm_destroy(&scm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) struct scm_cookie scm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) int noblock = flags & MSG_DONTWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) size_t copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) struct sk_buff *skb, *data_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) int err, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) if (flags & MSG_OOB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) skb = skb_recv_datagram(sk, flags, noblock, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) data_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) if (unlikely(skb_shinfo(skb)->frag_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) * If this skb has a frag_list, then here that means that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) * will have to use the frag_list skb's data for compat tasks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) * and the regular skb's data for normal (non-compat) tasks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) * If we need to send the compat skb, assign it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) * 'data_skb' variable so that it will be used below for data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) * copying. We keep 'skb' for everything else, including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) * freeing both later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) if (flags & MSG_CMSG_COMPAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) data_skb = skb_shinfo(skb)->frag_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) /* Record the max length of recvmsg() calls for future allocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) SKB_WITH_OVERHEAD(32768));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) copied = data_skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) if (len < copied) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) msg->msg_flags |= MSG_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) copied = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) skb_reset_transport_header(data_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) if (msg->msg_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) addr->nl_family = AF_NETLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) addr->nl_pad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) addr->nl_pid = NETLINK_CB(skb).portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) msg->msg_namelen = sizeof(*addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) if (nlk->flags & NETLINK_F_RECV_PKTINFO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) netlink_cmsg_recv_pktinfo(msg, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) netlink_cmsg_listen_all_nsid(sk, msg, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) memset(&scm, 0, sizeof(scm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) scm.creds = *NETLINK_CREDS(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) if (flags & MSG_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) copied = data_skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) skb_free_datagram(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) if (nlk->cb_running &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) ret = netlink_dump(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) sk->sk_err = -ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) scm_recv(sock, msg, &scm, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) netlink_rcv_wake(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) return err ? : copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) static void netlink_data_ready(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) * We export these functions to other modules. They provide a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) * complete set of kernel non-blocking support for message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) * queueing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) struct sock *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) __netlink_kernel_create(struct net *net, int unit, struct module *module,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) struct netlink_kernel_cfg *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) struct netlink_sock *nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) struct listeners *listeners = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) unsigned int groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) BUG_ON(!nl_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) if (unit < 0 || unit >= MAX_LINKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) goto out_sock_release_nosk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) if (!cfg || cfg->groups < 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) groups = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) groups = cfg->groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) if (!listeners)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) goto out_sock_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) sk->sk_data_ready = netlink_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) if (cfg && cfg->input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) nlk_sk(sk)->netlink_rcv = cfg->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) if (netlink_insert(sk, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) goto out_sock_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) nlk->flags |= NETLINK_F_KERNEL_SOCKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) netlink_table_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) if (!nl_table[unit].registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) nl_table[unit].groups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) rcu_assign_pointer(nl_table[unit].listeners, listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) nl_table[unit].cb_mutex = cb_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) nl_table[unit].module = module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) if (cfg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) nl_table[unit].bind = cfg->bind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) nl_table[unit].unbind = cfg->unbind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) nl_table[unit].flags = cfg->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) if (cfg->compare)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) nl_table[unit].compare = cfg->compare;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) nl_table[unit].registered = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) kfree(listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) nl_table[unit].registered++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) netlink_table_ungrab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) return sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) out_sock_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) kfree(listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) netlink_kernel_release(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) out_sock_release_nosk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) sock_release(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) EXPORT_SYMBOL(__netlink_kernel_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) netlink_kernel_release(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) if (sk == NULL || sk->sk_socket == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) sock_release(sk->sk_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) EXPORT_SYMBOL(netlink_kernel_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) struct listeners *new, *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) struct netlink_table *tbl = &nl_table[sk->sk_protocol];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) if (groups < 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) groups = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) old = nl_deref_protected(tbl->listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) rcu_assign_pointer(tbl->listeners, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) kfree_rcu(old, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) tbl->groups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) * netlink_change_ngroups - change number of multicast groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) * This changes the number of multicast groups that are available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) * on a certain netlink family. Note that it is not possible to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) * change the number of groups to below 32. Also note that it does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) * not implicitly call netlink_clear_multicast_users() when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) * number of groups is reduced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) * @groups: The new number of groups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) int netlink_change_ngroups(struct sock *sk, unsigned int groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) netlink_table_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) err = __netlink_change_ngroups(sk, groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) netlink_table_ungrab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) sk_for_each_bound(sk, &tbl->mc_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) netlink_update_socket_mc(nlk_sk(sk), group, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) struct nlmsghdr *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) int size = nlmsg_msg_size(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) nlh = skb_put(skb, NLMSG_ALIGN(size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) nlh->nlmsg_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) nlh->nlmsg_len = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) nlh->nlmsg_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) nlh->nlmsg_pid = portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) nlh->nlmsg_seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) return nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) EXPORT_SYMBOL(__nlmsg_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) * It looks a bit ugly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) * It would be better to create kernel thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) static int netlink_dump_done(struct netlink_sock *nlk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) struct netlink_callback *cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(nlk->dump_done_errno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) NLM_F_MULTI | cb->answer_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) if (WARN_ON(!nlh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) nl_dump_check_consistent(cb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) memcpy(nlmsg_data(nlh), &nlk->dump_done_errno, sizeof(nlk->dump_done_errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) if (extack->_msg && nlk->flags & NETLINK_F_EXT_ACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) nlh->nlmsg_flags |= NLM_F_ACK_TLVS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) if (!nla_put_string(skb, NLMSGERR_ATTR_MSG, extack->_msg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) static int netlink_dump(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) struct netlink_sock *nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) struct netlink_ext_ack extack = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) struct netlink_callback *cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) struct sk_buff *skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) struct module *module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) int err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) int alloc_min_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) int alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) mutex_lock(nlk->cb_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) if (!nlk->cb_running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) goto errout_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) goto errout_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) /* NLMSG_GOODSIZE is small to avoid high order allocations being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) * required, but it makes sense to _attempt_ a 16K bytes allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) * to reduce number of system calls on dump operations, if user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) * ever provided a big enough buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) cb = &nlk->cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) if (alloc_min_size < nlk->max_recvmsg_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) alloc_size = nlk->max_recvmsg_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) skb = alloc_skb(alloc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) __GFP_NOWARN | __GFP_NORETRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) alloc_size = alloc_min_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) skb = alloc_skb(alloc_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) goto errout_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) /* Trim skb to allocated size. User is expected to provide buffer as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) * netlink_recvmsg())). dump will pack as many smaller messages as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) * could fit within the allocated skb. skb is typically allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) * with larger space than required (could be as much as near 2x the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) * requested size with align to next power of 2 approach). Allowing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) * dump to use the excess space makes it difficult for a user to have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) * reasonable static buffer based on the expected largest dump of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) * single netdev. The outcome is MSG_TRUNC error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) skb_reserve(skb, skb_tailroom(skb) - alloc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) netlink_skb_set_owner_r(skb, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) if (nlk->dump_done_errno > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) cb->extack = &extack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) nlk->dump_done_errno = cb->dump(skb, cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) cb->extack = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) if (nlk->dump_done_errno > 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) skb_tailroom(skb) < nlmsg_total_size(sizeof(nlk->dump_done_errno))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) mutex_unlock(nlk->cb_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) if (sk_filter(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) __netlink_sendskb(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) if (netlink_dump_done(nlk, skb, cb, &extack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) goto errout_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) /* frag_list skb's data is used for compat tasks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) * and the regular skb's data for normal (non-compat) tasks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) * See netlink_recvmsg().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) if (unlikely(skb_shinfo(skb)->frag_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) if (netlink_dump_done(nlk, skb_shinfo(skb)->frag_list, cb, &extack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) goto errout_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) if (sk_filter(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) __netlink_sendskb(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) if (cb->done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) cb->done(cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) nlk->cb_running = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) module = cb->module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) skb = cb->skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) mutex_unlock(nlk->cb_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) module_put(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) errout_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) mutex_unlock(nlk->cb_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) const struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) struct netlink_dump_control *control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) struct netlink_sock *nlk, *nlk2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) struct netlink_callback *cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) refcount_inc(&skb->users);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) if (sk == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) ret = -ECONNREFUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) goto error_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) nlk = nlk_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) mutex_lock(nlk->cb_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) /* A dump is in progress... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) if (nlk->cb_running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) goto error_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) /* add reference of module which cb->dump belongs to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) if (!try_module_get(control->module)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) ret = -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) goto error_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) cb = &nlk->cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) memset(cb, 0, sizeof(*cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) cb->dump = control->dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) cb->done = control->done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) cb->nlh = nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) cb->data = control->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) cb->module = control->module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) cb->min_dump_alloc = control->min_dump_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) cb->skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) nlk2 = nlk_sk(NETLINK_CB(skb).sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) cb->strict_check = !!(nlk2->flags & NETLINK_F_STRICT_CHK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) if (control->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) ret = control->start(cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) goto error_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) nlk->cb_running = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) nlk->dump_done_errno = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) mutex_unlock(nlk->cb_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) ret = netlink_dump(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) /* We successfully started a dump, by returning -EINTR we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) * signal not to send ACK even if it was requested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) error_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) module_put(control->module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) error_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) mutex_unlock(nlk->cb_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) error_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) EXPORT_SYMBOL(__netlink_dump_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) const struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) struct nlmsghdr *rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) struct nlmsgerr *errmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) size_t payload = sizeof(*errmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) size_t tlvlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) unsigned int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) bool nlk_has_extack = nlk->flags & NETLINK_F_EXT_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) /* Error messages get the original request appened, unless the user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) * requests to cap the error message, and get extra error data if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) * requested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) if (nlk_has_extack && extack && extack->_msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) tlvlen += nla_total_size(strlen(extack->_msg) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) if (err && !(nlk->flags & NETLINK_F_CAP_ACK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) payload += nlmsg_len(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) flags |= NLM_F_CAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) if (err && nlk_has_extack && extack && extack->bad_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) tlvlen += nla_total_size(sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) if (nlk_has_extack && extack && extack->cookie_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) tlvlen += nla_total_size(extack->cookie_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) if (err && nlk_has_extack && extack && extack->policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) tlvlen += netlink_policy_dump_attr_size_estimate(extack->policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) if (tlvlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) flags |= NLM_F_ACK_TLVS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) skb = nlmsg_new(payload + tlvlen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) NETLINK_CB(in_skb).sk->sk_err = ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) NETLINK_CB(in_skb).sk->sk_error_report(NETLINK_CB(in_skb).sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) NLMSG_ERROR, payload, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) errmsg = nlmsg_data(rep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) errmsg->error = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) if (nlk_has_extack && extack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) if (extack->_msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) extack->_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) if (err && extack->bad_attr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) (u8 *)extack->bad_attr >= in_skb->data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) in_skb->len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) (u8 *)extack->bad_attr -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) (u8 *)nlh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) if (extack->cookie_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) extack->cookie_len, extack->cookie));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) if (extack->policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) netlink_policy_dump_write_attr(skb, extack->policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) NLMSGERR_ATTR_POLICY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) nlmsg_end(skb, rep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) EXPORT_SYMBOL(netlink_ack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) struct nlmsghdr *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) struct netlink_ext_ack *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) struct netlink_ext_ack extack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) while (skb->len >= nlmsg_total_size(0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) int msglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) memset(&extack, 0, sizeof(extack));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) nlh = nlmsg_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) /* Only requests are handled by the kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) goto ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) /* Skip control messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) goto ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) err = cb(skb, nlh, &extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) if (err == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) ack:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) if (nlh->nlmsg_flags & NLM_F_ACK || err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) netlink_ack(skb, nlh, err, &extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) msglen = NLMSG_ALIGN(nlh->nlmsg_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) if (msglen > skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) msglen = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) skb_pull(skb, msglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) EXPORT_SYMBOL(netlink_rcv_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) * nlmsg_notify - send a notification netlink message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) * @sk: netlink socket to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) * @skb: notification message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) * @portid: destination netlink portid for reports or 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) * @group: destination multicast group or 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) * @report: 1 to report back, 0 to disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) * @flags: allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) unsigned int group, int report, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) if (group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) int exclude_portid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) if (report) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) refcount_inc(&skb->users);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) exclude_portid = portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) /* errors reported via destination sk->sk_err, but propagate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) if (err == -ESRCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) if (report) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) int err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) err2 = nlmsg_unicast(sk, skb, portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) err = err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) EXPORT_SYMBOL(nlmsg_notify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) struct nl_seq_iter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) struct seq_net_private p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) struct rhashtable_iter hti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) int link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) static void netlink_walk_start(struct nl_seq_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) rhashtable_walk_enter(&nl_table[iter->link].hash, &iter->hti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) rhashtable_walk_start(&iter->hti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) static void netlink_walk_stop(struct nl_seq_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) rhashtable_walk_stop(&iter->hti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) rhashtable_walk_exit(&iter->hti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) static void *__netlink_seq_next(struct seq_file *seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) struct nl_seq_iter *iter = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) struct netlink_sock *nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) nlk = rhashtable_walk_next(&iter->hti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) if (IS_ERR(nlk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) if (PTR_ERR(nlk) == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) return nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) if (nlk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) netlink_walk_stop(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) if (++iter->link >= MAX_LINKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) netlink_walk_start(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) } while (sock_net(&nlk->sk) != seq_file_net(seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) return nlk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) __acquires(RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) struct nl_seq_iter *iter = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) void *obj = SEQ_START_TOKEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) loff_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) iter->link = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) netlink_walk_start(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) obj = __netlink_seq_next(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) ++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) return __netlink_seq_next(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) static void netlink_native_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) struct nl_seq_iter *iter = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) if (iter->link >= MAX_LINKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) netlink_walk_stop(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) static int netlink_native_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) if (v == SEQ_START_TOKEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) seq_puts(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) "sk Eth Pid Groups "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) "Rmem Wmem Dump Locks Drops Inode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) struct sock *s = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) struct netlink_sock *nlk = nlk_sk(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) seq_printf(seq, "%pK %-3d %-10u %08x %-8d %-8d %-5d %-8d %-8u %-8lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) s->sk_protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) nlk->portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) nlk->groups ? (u32)nlk->groups[0] : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) sk_rmem_alloc_get(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) sk_wmem_alloc_get(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) nlk->cb_running,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) refcount_read(&s->sk_refcnt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) atomic_read(&s->sk_drops),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) sock_i_ino(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) #ifdef CONFIG_BPF_SYSCALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) struct bpf_iter__netlink {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) __bpf_md_ptr(struct bpf_iter_meta *, meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) __bpf_md_ptr(struct netlink_sock *, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) DEFINE_BPF_ITER_FUNC(netlink, struct bpf_iter_meta *meta, struct netlink_sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) static int netlink_prog_seq_show(struct bpf_prog *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) struct bpf_iter_meta *meta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) struct bpf_iter__netlink ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) meta->seq_num--; /* skip SEQ_START_TOKEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) ctx.meta = meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) ctx.sk = nlk_sk((struct sock *)v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) return bpf_iter_run_prog(prog, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) static int netlink_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) struct bpf_iter_meta meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) struct bpf_prog *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) meta.seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) prog = bpf_iter_get_info(&meta, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) if (!prog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) return netlink_native_seq_show(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) if (v != SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) return netlink_prog_seq_show(prog, &meta, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) static void netlink_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) struct bpf_iter_meta meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) struct bpf_prog *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) if (!v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) meta.seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) prog = bpf_iter_get_info(&meta, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) if (prog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) (void)netlink_prog_seq_show(prog, &meta, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) netlink_native_seq_stop(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) static int netlink_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) return netlink_native_seq_show(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) static void netlink_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) netlink_native_seq_stop(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) static const struct seq_operations netlink_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) .start = netlink_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) .next = netlink_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) .stop = netlink_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) .show = netlink_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) int netlink_register_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) return blocking_notifier_chain_register(&netlink_chain, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) EXPORT_SYMBOL(netlink_register_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) int netlink_unregister_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) return blocking_notifier_chain_unregister(&netlink_chain, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) EXPORT_SYMBOL(netlink_unregister_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) static const struct proto_ops netlink_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) .family = PF_NETLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) .release = netlink_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) .bind = netlink_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) .connect = netlink_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) .accept = sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) .getname = netlink_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) .poll = datagram_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) .ioctl = netlink_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) .listen = sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) .shutdown = sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) .setsockopt = netlink_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) .getsockopt = netlink_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) .sendmsg = netlink_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) .recvmsg = netlink_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) .sendpage = sock_no_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) static const struct net_proto_family netlink_family_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) .family = PF_NETLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) .create = netlink_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) .owner = THIS_MODULE, /* for consistency 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) static int __net_init netlink_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) if (!proc_create_net("netlink", 0, net->proc_net, &netlink_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) sizeof(struct nl_seq_iter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) static void __net_exit netlink_net_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) remove_proc_entry("netlink", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) static void __init netlink_add_usersock_entry(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) struct listeners *listeners;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) int groups = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) if (!listeners)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) netlink_table_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) nl_table[NETLINK_USERSOCK].groups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) nl_table[NETLINK_USERSOCK].registered = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) netlink_table_ungrab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) static struct pernet_operations __net_initdata netlink_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) .init = netlink_net_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) .exit = netlink_net_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) const struct netlink_sock *nlk = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) struct netlink_compare_arg arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) static const struct rhashtable_params netlink_rhashtable_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) .head_offset = offsetof(struct netlink_sock, node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) .key_len = netlink_compare_arg_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) .obj_hashfn = netlink_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) .obj_cmpfn = netlink_compare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) .automatic_shrinking = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) BTF_ID_LIST(btf_netlink_sock_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) BTF_ID(struct, netlink_sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) static const struct bpf_iter_seq_info netlink_seq_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) .seq_ops = &netlink_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) .init_seq_private = bpf_iter_init_seq_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) .fini_seq_private = bpf_iter_fini_seq_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) .seq_priv_size = sizeof(struct nl_seq_iter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) static struct bpf_iter_reg netlink_reg_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) .target = "netlink",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) .ctx_arg_info_size = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) .ctx_arg_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) { offsetof(struct bpf_iter__netlink, sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) PTR_TO_BTF_ID_OR_NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) .seq_info = &netlink_seq_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) static int __init bpf_iter_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) netlink_reg_info.ctx_arg_info[0].btf_id = *btf_netlink_sock_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) return bpf_iter_reg_target(&netlink_reg_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) static int __init netlink_proto_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) int err = proto_register(&netlink_proto, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) err = bpf_iter_register();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof_field(struct sk_buff, cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) if (!nl_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) goto panic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) for (i = 0; i < MAX_LINKS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) if (rhashtable_init(&nl_table[i].hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) &netlink_rhashtable_params) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) while (--i > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) rhashtable_destroy(&nl_table[i].hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) kfree(nl_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) goto panic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) netlink_add_usersock_entry();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) sock_register(&netlink_family_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) register_pernet_subsys(&netlink_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) register_pernet_subsys(&netlink_tap_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) /* The netlink device handler may be needed early. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) rtnetlink_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) panic:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) panic("netlink_init: Cannot allocate nl_table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) core_initcall(netlink_proto_init);