hc
2024-10-16 50a212ec906f7524620675f0c57357691c26c81f
kernel/net/netlink/af_netlink.c
....@@ -1,14 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * NETLINK Kernel-user communication protocol.
34 *
45 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
56 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
67 * Patrick McHardy <kaber@trash.net>
7
- *
8
- * This program is free software; you can redistribute it and/or
9
- * modify it under the terms of the GNU General Public License
10
- * as published by the Free Software Foundation; either version
11
- * 2 of the License, or (at your option) any later version.
128 *
139 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
1410 * added netlink_proto_exit
....@@ -64,6 +60,7 @@
6460 #include <linux/genetlink.h>
6561 #include <linux/net_namespace.h>
6662 #include <linux/nospec.h>
63
+#include <linux/btf_ids.h>
6764
6865 #include <net/net_namespace.h>
6966 #include <net/netns/generic.h>
....@@ -75,7 +72,7 @@
7572
7673 struct listeners {
7774 struct rcu_head rcu;
78
- unsigned long masks[0];
75
+ unsigned long masks[];
7976 };
8077
8178 /* state bits */
....@@ -152,6 +149,8 @@
152149
153150 static inline u32 netlink_group_mask(u32 group)
154151 {
152
+ if (group > 32)
153
+ return 0;
155154 return group ? 1 << (group - 1) : 0;
156155 }
157156
....@@ -245,13 +244,8 @@
245244 return 0;
246245 }
247246
248
-static void __net_exit netlink_tap_exit_net(struct net *net)
249
-{
250
-}
251
-
252247 static struct pernet_operations netlink_tap_net_ops = {
253248 .init = netlink_tap_init_net,
254
- .exit = netlink_tap_exit_net,
255249 .id = &netlink_tap_net_id,
256250 .size = sizeof(struct netlink_tap_net),
257251 };
....@@ -361,7 +355,7 @@
361355 {
362356 struct netlink_sock *nlk = nlk_sk(sk);
363357
364
- if (skb_queue_empty(&sk->sk_receive_queue))
358
+ if (skb_queue_empty_lockless(&sk->sk_receive_queue))
365359 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
366360 if (!test_bit(NETLINK_S_CONGESTED, &nlk->state))
367361 wake_up_interruptible(&nlk->wait);
....@@ -576,12 +570,9 @@
576570 if (nlk_sk(sk)->bound)
577571 goto err;
578572
579
- err = -ENOMEM;
580
- if (BITS_PER_LONG > 32 &&
581
- unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
582
- goto err;
573
+ /* portid can be read locklessly from netlink_getname(). */
574
+ WRITE_ONCE(nlk_sk(sk)->portid, portid);
583575
584
- nlk_sk(sk)->portid = portid;
585576 sock_hold(sk);
586577
587578 err = __netlink_insert(table, sk);
....@@ -866,7 +857,7 @@
866857 *
867858 * Test to see if the opener of the socket we received the message
868859 * from had when the netlink socket was created and the sender of the
869
- * message has has the capability @cap in the user namespace @user_ns.
860
+ * message has the capability @cap in the user namespace @user_ns.
870861 */
871862 bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
872863 struct user_namespace *user_ns, int cap)
....@@ -885,7 +876,7 @@
885876 *
886877 * Test to see if the opener of the socket we received the message
887878 * from had when the netlink socket was created and the sender of the
888
- * message has has the capability @cap in the user namespace @user_ns.
879
+ * message has the capability @cap in the user namespace @user_ns.
889880 */
890881 bool netlink_ns_capable(const struct sk_buff *skb,
891882 struct user_namespace *user_ns, int cap)
....@@ -901,7 +892,7 @@
901892 *
902893 * Test to see if the opener of the socket we received the message
903894 * from had when the netlink socket was created and the sender of the
904
- * message has has the capability @cap in all user namespaces.
895
+ * message has the capability @cap in all user namespaces.
905896 */
906897 bool netlink_capable(const struct sk_buff *skb, int cap)
907898 {
....@@ -916,7 +907,7 @@
916907 *
917908 * Test to see if the opener of the socket we received the message
918909 * from had when the netlink socket was created and the sender of the
919
- * message has has the capability @cap over the network namespace of
910
+ * message has the capability @cap over the network namespace of
920911 * the socket we received the message from.
921912 */
922913 bool netlink_net_capable(const struct sk_buff *skb, int cap)
....@@ -998,7 +989,7 @@
998989 struct netlink_sock *nlk = nlk_sk(sk);
999990 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1000991 int err = 0;
1001
- long unsigned int groups = nladdr->nl_groups;
992
+ unsigned long groups;
1002993 bool bound;
1003994
1004995 if (addr_len < sizeof(struct sockaddr_nl))
....@@ -1006,6 +997,7 @@
1006997
1007998 if (nladdr->nl_family != AF_NETLINK)
1008999 return -EINVAL;
1000
+ groups = nladdr->nl_groups;
10091001
10101002 /* Only superuser is allowed to listen multicasts */
10111003 if (groups) {
....@@ -1016,9 +1008,7 @@
10161008 return err;
10171009 }
10181010
1019
- if (nlk->ngroups == 0)
1020
- groups = 0;
1021
- else if (nlk->ngroups < 8*sizeof(groups))
1011
+ if (nlk->ngroups < BITS_PER_LONG)
10221012 groups &= (1UL << nlk->ngroups) - 1;
10231013
10241014 /* Paired with WRITE_ONCE() in netlink_insert() */
....@@ -1091,9 +1081,11 @@
10911081 return -EINVAL;
10921082
10931083 if (addr->sa_family == AF_UNSPEC) {
1094
- sk->sk_state = NETLINK_UNCONNECTED;
1095
- nlk->dst_portid = 0;
1096
- nlk->dst_group = 0;
1084
+ /* paired with READ_ONCE() in netlink_getsockbyportid() */
1085
+ WRITE_ONCE(sk->sk_state, NETLINK_UNCONNECTED);
1086
+ /* dst_portid and dst_group can be read locklessly */
1087
+ WRITE_ONCE(nlk->dst_portid, 0);
1088
+ WRITE_ONCE(nlk->dst_group, 0);
10971089 return 0;
10981090 }
10991091 if (addr->sa_family != AF_NETLINK)
....@@ -1114,9 +1106,11 @@
11141106 err = netlink_autobind(sock);
11151107
11161108 if (err == 0) {
1117
- sk->sk_state = NETLINK_CONNECTED;
1118
- nlk->dst_portid = nladdr->nl_pid;
1119
- nlk->dst_group = ffs(nladdr->nl_groups);
1109
+ /* paired with READ_ONCE() in netlink_getsockbyportid() */
1110
+ WRITE_ONCE(sk->sk_state, NETLINK_CONNECTED);
1111
+ /* dst_portid and dst_group can be read locklessly */
1112
+ WRITE_ONCE(nlk->dst_portid, nladdr->nl_pid);
1113
+ WRITE_ONCE(nlk->dst_group, ffs(nladdr->nl_groups));
11201114 }
11211115
11221116 return err;
....@@ -1133,10 +1127,12 @@
11331127 nladdr->nl_pad = 0;
11341128
11351129 if (peer) {
1136
- nladdr->nl_pid = nlk->dst_portid;
1137
- nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
1130
+ /* Paired with WRITE_ONCE() in netlink_connect() */
1131
+ nladdr->nl_pid = READ_ONCE(nlk->dst_portid);
1132
+ nladdr->nl_groups = netlink_group_mask(READ_ONCE(nlk->dst_group));
11381133 } else {
1139
- nladdr->nl_pid = nlk->portid;
1134
+ /* Paired with WRITE_ONCE() in netlink_insert() */
1135
+ nladdr->nl_pid = READ_ONCE(nlk->portid);
11401136 netlink_lock_table();
11411137 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
11421138 netlink_unlock_table();
....@@ -1163,8 +1159,9 @@
11631159
11641160 /* Don't bother queuing skb if kernel socket has no input function */
11651161 nlk = nlk_sk(sock);
1166
- if (sock->sk_state == NETLINK_CONNECTED &&
1167
- nlk->dst_portid != nlk_sk(ssk)->portid) {
1162
+ /* dst_portid and sk_state can be changed in netlink_connect() */
1163
+ if (READ_ONCE(sock->sk_state) == NETLINK_CONNECTED &&
1164
+ READ_ONCE(nlk->dst_portid) != nlk_sk(ssk)->portid) {
11681165 sock_put(sock);
11691166 return ERR_PTR(-ECONNREFUSED);
11701167 }
....@@ -1386,6 +1383,14 @@
13861383 }
13871384 EXPORT_SYMBOL_GPL(netlink_has_listeners);
13881385
1386
+bool netlink_strict_get_check(struct sk_buff *skb)
1387
+{
1388
+ const struct netlink_sock *nlk = nlk_sk(NETLINK_CB(skb).sk);
1389
+
1390
+ return nlk->flags & NETLINK_F_STRICT_CHK;
1391
+}
1392
+EXPORT_SYMBOL_GPL(netlink_strict_get_check);
1393
+
13891394 static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
13901395 {
13911396 struct netlink_sock *nlk = nlk_sk(sk);
....@@ -1597,6 +1602,7 @@
15971602 int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
15981603 {
15991604 struct netlink_set_err_data info;
1605
+ unsigned long flags;
16001606 struct sock *sk;
16011607 int ret = 0;
16021608
....@@ -1606,12 +1612,12 @@
16061612 /* sk->sk_err wants a positive error value */
16071613 info.code = -code;
16081614
1609
- read_lock(&nl_table_lock);
1615
+ read_lock_irqsave(&nl_table_lock, flags);
16101616
16111617 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
16121618 ret += do_one_set_err(sk, &info);
16131619
1614
- read_unlock(&nl_table_lock);
1620
+ read_unlock_irqrestore(&nl_table_lock, flags);
16151621 return ret;
16161622 }
16171623 EXPORT_SYMBOL(netlink_set_err);
....@@ -1634,7 +1640,7 @@
16341640 }
16351641
16361642 static int netlink_setsockopt(struct socket *sock, int level, int optname,
1637
- char __user *optval, unsigned int optlen)
1643
+ sockptr_t optval, unsigned int optlen)
16381644 {
16391645 struct sock *sk = sock->sk;
16401646 struct netlink_sock *nlk = nlk_sk(sk);
....@@ -1645,7 +1651,7 @@
16451651 return -ENOPROTOOPT;
16461652
16471653 if (optlen >= sizeof(int) &&
1648
- get_user(val, (unsigned int __user *)optval))
1654
+ copy_from_sockptr(&val, optval, sizeof(val)))
16491655 return -EFAULT;
16501656
16511657 switch (optname) {
....@@ -1721,6 +1727,13 @@
17211727 nlk->flags &= ~NETLINK_F_EXT_ACK;
17221728 err = 0;
17231729 break;
1730
+ case NETLINK_GET_STRICT_CHK:
1731
+ if (val)
1732
+ nlk->flags |= NETLINK_F_STRICT_CHK;
1733
+ else
1734
+ nlk->flags &= ~NETLINK_F_STRICT_CHK;
1735
+ err = 0;
1736
+ break;
17241737 default:
17251738 err = -ENOPROTOOPT;
17261739 }
....@@ -1732,7 +1745,8 @@
17321745 {
17331746 struct sock *sk = sock->sk;
17341747 struct netlink_sock *nlk = nlk_sk(sk);
1735
- int len, val, err;
1748
+ unsigned int flag;
1749
+ int len, val;
17361750
17371751 if (level != SOL_NETLINK)
17381752 return -ENOPROTOOPT;
....@@ -1744,39 +1758,17 @@
17441758
17451759 switch (optname) {
17461760 case NETLINK_PKTINFO:
1747
- if (len < sizeof(int))
1748
- return -EINVAL;
1749
- len = sizeof(int);
1750
- val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
1751
- if (put_user(len, optlen) ||
1752
- put_user(val, optval))
1753
- return -EFAULT;
1754
- err = 0;
1761
+ flag = NETLINK_F_RECV_PKTINFO;
17551762 break;
17561763 case NETLINK_BROADCAST_ERROR:
1757
- if (len < sizeof(int))
1758
- return -EINVAL;
1759
- len = sizeof(int);
1760
- val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
1761
- if (put_user(len, optlen) ||
1762
- put_user(val, optval))
1763
- return -EFAULT;
1764
- err = 0;
1764
+ flag = NETLINK_F_BROADCAST_SEND_ERROR;
17651765 break;
17661766 case NETLINK_NO_ENOBUFS:
1767
- if (len < sizeof(int))
1768
- return -EINVAL;
1769
- len = sizeof(int);
1770
- val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
1771
- if (put_user(len, optlen) ||
1772
- put_user(val, optval))
1773
- return -EFAULT;
1774
- err = 0;
1767
+ flag = NETLINK_F_RECV_NO_ENOBUFS;
17751768 break;
17761769 case NETLINK_LIST_MEMBERSHIPS: {
1777
- int pos, idx, shift;
1770
+ int pos, idx, shift, err = 0;
17781771
1779
- err = 0;
17801772 netlink_lock_table();
17811773 for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
17821774 if (len - pos < sizeof(u32))
....@@ -1790,34 +1782,35 @@
17901782 break;
17911783 }
17921784 }
1793
- if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
1785
+ if (put_user(ALIGN(BITS_TO_BYTES(nlk->ngroups), sizeof(u32)), optlen))
17941786 err = -EFAULT;
17951787 netlink_unlock_table();
1796
- break;
1788
+ return err;
17971789 }
17981790 case NETLINK_CAP_ACK:
1799
- if (len < sizeof(int))
1800
- return -EINVAL;
1801
- len = sizeof(int);
1802
- val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
1803
- if (put_user(len, optlen) ||
1804
- put_user(val, optval))
1805
- return -EFAULT;
1806
- err = 0;
1791
+ flag = NETLINK_F_CAP_ACK;
18071792 break;
18081793 case NETLINK_EXT_ACK:
1809
- if (len < sizeof(int))
1810
- return -EINVAL;
1811
- len = sizeof(int);
1812
- val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
1813
- if (put_user(len, optlen) || put_user(val, optval))
1814
- return -EFAULT;
1815
- err = 0;
1794
+ flag = NETLINK_F_EXT_ACK;
1795
+ break;
1796
+ case NETLINK_GET_STRICT_CHK:
1797
+ flag = NETLINK_F_STRICT_CHK;
18161798 break;
18171799 default:
1818
- err = -ENOPROTOOPT;
1800
+ return -ENOPROTOOPT;
18191801 }
1820
- return err;
1802
+
1803
+ if (len < sizeof(int))
1804
+ return -EINVAL;
1805
+
1806
+ len = sizeof(int);
1807
+ val = nlk->flags & flag ? 1 : 0;
1808
+
1809
+ if (put_user(len, optlen) ||
1810
+ copy_to_user(optval, &val, len))
1811
+ return -EFAULT;
1812
+
1813
+ return 0;
18211814 }
18221815
18231816 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
....@@ -1850,7 +1843,7 @@
18501843 struct scm_cookie scm;
18511844 u32 netlink_skb_flags = 0;
18521845
1853
- if (msg->msg_flags&MSG_OOB)
1846
+ if (msg->msg_flags & MSG_OOB)
18541847 return -EOPNOTSUPP;
18551848
18561849 if (len == 0) {
....@@ -1876,8 +1869,9 @@
18761869 goto out;
18771870 netlink_skb_flags |= NETLINK_SKB_DST;
18781871 } else {
1879
- dst_portid = nlk->dst_portid;
1880
- dst_group = nlk->dst_group;
1872
+ /* Paired with WRITE_ONCE() in netlink_connect() */
1873
+ dst_portid = READ_ONCE(nlk->dst_portid);
1874
+ dst_group = READ_ONCE(nlk->dst_group);
18811875 }
18821876
18831877 /* Paired with WRITE_ONCE() in netlink_insert() */
....@@ -1919,7 +1913,7 @@
19191913 refcount_inc(&skb->users);
19201914 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
19211915 }
1922
- err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1916
+ err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags & MSG_DONTWAIT);
19231917
19241918 out:
19251919 scm_destroy(&scm);
....@@ -1932,12 +1926,12 @@
19321926 struct scm_cookie scm;
19331927 struct sock *sk = sock->sk;
19341928 struct netlink_sock *nlk = nlk_sk(sk);
1935
- int noblock = flags&MSG_DONTWAIT;
1929
+ int noblock = flags & MSG_DONTWAIT;
19361930 size_t copied;
19371931 struct sk_buff *skb, *data_skb;
19381932 int err, ret;
19391933
1940
- if (flags&MSG_OOB)
1934
+ if (flags & MSG_OOB)
19411935 return -EOPNOTSUPP;
19421936
19431937 copied = 0;
....@@ -1976,7 +1970,6 @@
19761970 copied = len;
19771971 }
19781972
1979
- skb_reset_transport_header(data_skb);
19801973 err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
19811974
19821975 if (msg->msg_name) {
....@@ -2000,7 +1993,7 @@
20001993
20011994 skb_free_datagram(sk, skb);
20021995
2003
- if (nlk->cb_running &&
1996
+ if (READ_ONCE(nlk->cb_running) &&
20041997 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
20051998 ret = netlink_dump(sk);
20061999 if (ret) {
....@@ -2189,12 +2182,35 @@
21892182 * It would be better to create kernel thread.
21902183 */
21912184
2185
+static int netlink_dump_done(struct netlink_sock *nlk, struct sk_buff *skb,
2186
+ struct netlink_callback *cb,
2187
+ struct netlink_ext_ack *extack)
2188
+{
2189
+ struct nlmsghdr *nlh;
2190
+
2191
+ nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(nlk->dump_done_errno),
2192
+ NLM_F_MULTI | cb->answer_flags);
2193
+ if (WARN_ON(!nlh))
2194
+ return -ENOBUFS;
2195
+
2196
+ nl_dump_check_consistent(cb, nlh);
2197
+ memcpy(nlmsg_data(nlh), &nlk->dump_done_errno, sizeof(nlk->dump_done_errno));
2198
+
2199
+ if (extack->_msg && nlk->flags & NETLINK_F_EXT_ACK) {
2200
+ nlh->nlmsg_flags |= NLM_F_ACK_TLVS;
2201
+ if (!nla_put_string(skb, NLMSGERR_ATTR_MSG, extack->_msg))
2202
+ nlmsg_end(skb, nlh);
2203
+ }
2204
+
2205
+ return 0;
2206
+}
2207
+
21922208 static int netlink_dump(struct sock *sk)
21932209 {
21942210 struct netlink_sock *nlk = nlk_sk(sk);
2211
+ struct netlink_ext_ack extack = {};
21952212 struct netlink_callback *cb;
21962213 struct sk_buff *skb = NULL;
2197
- struct nlmsghdr *nlh;
21982214 struct module *module;
21992215 int err = -ENOBUFS;
22002216 int alloc_min_size;
....@@ -2241,10 +2257,20 @@
22412257 * single netdev. The outcome is MSG_TRUNC error.
22422258 */
22432259 skb_reserve(skb, skb_tailroom(skb) - alloc_size);
2260
+
2261
+ /* Make sure malicious BPF programs can not read unitialized memory
2262
+ * from skb->head -> skb->data
2263
+ */
2264
+ skb_reset_network_header(skb);
2265
+ skb_reset_mac_header(skb);
2266
+
22442267 netlink_skb_set_owner_r(skb, sk);
22452268
2246
- if (nlk->dump_done_errno > 0)
2269
+ if (nlk->dump_done_errno > 0) {
2270
+ cb->extack = &extack;
22472271 nlk->dump_done_errno = cb->dump(skb, cb);
2272
+ cb->extack = NULL;
2273
+ }
22482274
22492275 if (nlk->dump_done_errno > 0 ||
22502276 skb_tailroom(skb) < nlmsg_total_size(sizeof(nlk->dump_done_errno))) {
....@@ -2257,15 +2283,19 @@
22572283 return 0;
22582284 }
22592285
2260
- nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE,
2261
- sizeof(nlk->dump_done_errno), NLM_F_MULTI);
2262
- if (WARN_ON(!nlh))
2286
+ if (netlink_dump_done(nlk, skb, cb, &extack))
22632287 goto errout_skb;
22642288
2265
- nl_dump_check_consistent(cb, nlh);
2266
-
2267
- memcpy(nlmsg_data(nlh), &nlk->dump_done_errno,
2268
- sizeof(nlk->dump_done_errno));
2289
+#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2290
+ /* frag_list skb's data is used for compat tasks
2291
+ * and the regular skb's data for normal (non-compat) tasks.
2292
+ * See netlink_recvmsg().
2293
+ */
2294
+ if (unlikely(skb_shinfo(skb)->frag_list)) {
2295
+ if (netlink_dump_done(nlk, skb_shinfo(skb)->frag_list, cb, &extack))
2296
+ goto errout_skb;
2297
+ }
2298
+#endif
22692299
22702300 if (sk_filter(sk, skb))
22712301 kfree_skb(skb);
....@@ -2275,7 +2305,7 @@
22752305 if (cb->done)
22762306 cb->done(cb);
22772307
2278
- nlk->cb_running = false;
2308
+ WRITE_ONCE(nlk->cb_running, false);
22792309 module = cb->module;
22802310 skb = cb->skb;
22812311 mutex_unlock(nlk->cb_mutex);
....@@ -2293,9 +2323,9 @@
22932323 const struct nlmsghdr *nlh,
22942324 struct netlink_dump_control *control)
22952325 {
2326
+ struct netlink_sock *nlk, *nlk2;
22962327 struct netlink_callback *cb;
22972328 struct sock *sk;
2298
- struct netlink_sock *nlk;
22992329 int ret;
23002330
23012331 refcount_inc(&skb->users);
....@@ -2329,13 +2359,16 @@
23292359 cb->min_dump_alloc = control->min_dump_alloc;
23302360 cb->skb = skb;
23312361
2362
+ nlk2 = nlk_sk(NETLINK_CB(skb).sk);
2363
+ cb->strict_check = !!(nlk2->flags & NETLINK_F_STRICT_CHK);
2364
+
23322365 if (control->start) {
23332366 ret = control->start(cb);
23342367 if (ret)
23352368 goto error_put;
23362369 }
23372370
2338
- nlk->cb_running = true;
2371
+ WRITE_ONCE(nlk->cb_running, true);
23392372 nlk->dump_done_errno = INT_MAX;
23402373
23412374 mutex_unlock(nlk->cb_mutex);
....@@ -2382,19 +2415,16 @@
23822415 if (nlk_has_extack && extack && extack->_msg)
23832416 tlvlen += nla_total_size(strlen(extack->_msg) + 1);
23842417
2385
- if (err) {
2386
- if (!(nlk->flags & NETLINK_F_CAP_ACK))
2387
- payload += nlmsg_len(nlh);
2388
- else
2389
- flags |= NLM_F_CAPPED;
2390
- if (nlk_has_extack && extack && extack->bad_attr)
2391
- tlvlen += nla_total_size(sizeof(u32));
2392
- } else {
2418
+ if (err && !(nlk->flags & NETLINK_F_CAP_ACK))
2419
+ payload += nlmsg_len(nlh);
2420
+ else
23932421 flags |= NLM_F_CAPPED;
2394
-
2395
- if (nlk_has_extack && extack && extack->cookie_len)
2396
- tlvlen += nla_total_size(extack->cookie_len);
2397
- }
2422
+ if (err && nlk_has_extack && extack && extack->bad_attr)
2423
+ tlvlen += nla_total_size(sizeof(u32));
2424
+ if (nlk_has_extack && extack && extack->cookie_len)
2425
+ tlvlen += nla_total_size(extack->cookie_len);
2426
+ if (err && nlk_has_extack && extack && extack->policy)
2427
+ tlvlen += netlink_policy_dump_attr_size_estimate(extack->policy);
23982428
23992429 if (tlvlen)
24002430 flags |= NLM_F_ACK_TLVS;
....@@ -2417,20 +2447,19 @@
24172447 WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
24182448 extack->_msg));
24192449 }
2420
- if (err) {
2421
- if (extack->bad_attr &&
2422
- !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
2423
- (u8 *)extack->bad_attr >= in_skb->data +
2424
- in_skb->len))
2425
- WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
2426
- (u8 *)extack->bad_attr -
2427
- (u8 *)nlh));
2428
- } else {
2429
- if (extack->cookie_len)
2430
- WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
2431
- extack->cookie_len,
2432
- extack->cookie));
2433
- }
2450
+ if (err && extack->bad_attr &&
2451
+ !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
2452
+ (u8 *)extack->bad_attr >= in_skb->data +
2453
+ in_skb->len))
2454
+ WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
2455
+ (u8 *)extack->bad_attr -
2456
+ (u8 *)nlh));
2457
+ if (extack->cookie_len)
2458
+ WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
2459
+ extack->cookie_len, extack->cookie));
2460
+ if (extack->policy)
2461
+ netlink_policy_dump_write_attr(skb, extack->policy,
2462
+ NLMSGERR_ATTR_POLICY);
24342463 }
24352464
24362465 nlmsg_end(skb, rep);
....@@ -2532,20 +2561,10 @@
25322561 int link;
25332562 };
25342563
2535
-static int netlink_walk_start(struct nl_seq_iter *iter)
2564
+static void netlink_walk_start(struct nl_seq_iter *iter)
25362565 {
2537
- int err;
2538
-
2539
- err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
2540
- GFP_KERNEL);
2541
- if (err) {
2542
- iter->link = MAX_LINKS;
2543
- return err;
2544
- }
2545
-
2566
+ rhashtable_walk_enter(&nl_table[iter->link].hash, &iter->hti);
25462567 rhashtable_walk_start(&iter->hti);
2547
-
2548
- return 0;
25492568 }
25502569
25512570 static void netlink_walk_stop(struct nl_seq_iter *iter)
....@@ -2561,8 +2580,6 @@
25612580
25622581 do {
25632582 for (;;) {
2564
- int err;
2565
-
25662583 nlk = rhashtable_walk_next(&iter->hti);
25672584
25682585 if (IS_ERR(nlk)) {
....@@ -2579,9 +2596,7 @@
25792596 if (++iter->link >= MAX_LINKS)
25802597 return NULL;
25812598
2582
- err = netlink_walk_start(iter);
2583
- if (err)
2584
- return ERR_PTR(err);
2599
+ netlink_walk_start(iter);
25852600 }
25862601 } while (sock_net(&nlk->sk) != seq_file_net(seq));
25872602
....@@ -2589,17 +2604,15 @@
25892604 }
25902605
25912606 static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
2607
+ __acquires(RCU)
25922608 {
25932609 struct nl_seq_iter *iter = seq->private;
25942610 void *obj = SEQ_START_TOKEN;
25952611 loff_t pos;
2596
- int err;
25972612
25982613 iter->link = 0;
25992614
2600
- err = netlink_walk_start(iter);
2601
- if (err)
2602
- return ERR_PTR(err);
2615
+ netlink_walk_start(iter);
26032616
26042617 for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
26052618 obj = __netlink_seq_next(seq);
....@@ -2613,7 +2626,7 @@
26132626 return __netlink_seq_next(seq);
26142627 }
26152628
2616
-static void netlink_seq_stop(struct seq_file *seq, void *v)
2629
+static void netlink_native_seq_stop(struct seq_file *seq, void *v)
26172630 {
26182631 struct nl_seq_iter *iter = seq->private;
26192632
....@@ -2624,7 +2637,7 @@
26242637 }
26252638
26262639
2627
-static int netlink_seq_show(struct seq_file *seq, void *v)
2640
+static int netlink_native_seq_show(struct seq_file *seq, void *v)
26282641 {
26292642 if (v == SEQ_START_TOKEN) {
26302643 seq_puts(seq,
....@@ -2634,14 +2647,14 @@
26342647 struct sock *s = v;
26352648 struct netlink_sock *nlk = nlk_sk(s);
26362649
2637
- seq_printf(seq, "%pK %-3d %-10u %08x %-8d %-8d %-5d %-8d %-8d %-8lu\n",
2650
+ seq_printf(seq, "%pK %-3d %-10u %08x %-8d %-8d %-5d %-8d %-8u %-8lu\n",
26382651 s,
26392652 s->sk_protocol,
26402653 nlk->portid,
26412654 nlk->groups ? (u32)nlk->groups[0] : 0,
26422655 sk_rmem_alloc_get(s),
26432656 sk_wmem_alloc_get(s),
2644
- nlk->cb_running,
2657
+ READ_ONCE(nlk->cb_running),
26452658 refcount_read(&s->sk_refcnt),
26462659 atomic_read(&s->sk_drops),
26472660 sock_i_ino(s)
....@@ -2650,6 +2663,68 @@
26502663 }
26512664 return 0;
26522665 }
2666
+
2667
+#ifdef CONFIG_BPF_SYSCALL
2668
+struct bpf_iter__netlink {
2669
+ __bpf_md_ptr(struct bpf_iter_meta *, meta);
2670
+ __bpf_md_ptr(struct netlink_sock *, sk);
2671
+};
2672
+
2673
+DEFINE_BPF_ITER_FUNC(netlink, struct bpf_iter_meta *meta, struct netlink_sock *sk)
2674
+
2675
+static int netlink_prog_seq_show(struct bpf_prog *prog,
2676
+ struct bpf_iter_meta *meta,
2677
+ void *v)
2678
+{
2679
+ struct bpf_iter__netlink ctx;
2680
+
2681
+ meta->seq_num--; /* skip SEQ_START_TOKEN */
2682
+ ctx.meta = meta;
2683
+ ctx.sk = nlk_sk((struct sock *)v);
2684
+ return bpf_iter_run_prog(prog, &ctx);
2685
+}
2686
+
2687
+static int netlink_seq_show(struct seq_file *seq, void *v)
2688
+{
2689
+ struct bpf_iter_meta meta;
2690
+ struct bpf_prog *prog;
2691
+
2692
+ meta.seq = seq;
2693
+ prog = bpf_iter_get_info(&meta, false);
2694
+ if (!prog)
2695
+ return netlink_native_seq_show(seq, v);
2696
+
2697
+ if (v != SEQ_START_TOKEN)
2698
+ return netlink_prog_seq_show(prog, &meta, v);
2699
+
2700
+ return 0;
2701
+}
2702
+
2703
+static void netlink_seq_stop(struct seq_file *seq, void *v)
2704
+{
2705
+ struct bpf_iter_meta meta;
2706
+ struct bpf_prog *prog;
2707
+
2708
+ if (!v) {
2709
+ meta.seq = seq;
2710
+ prog = bpf_iter_get_info(&meta, true);
2711
+ if (prog)
2712
+ (void)netlink_prog_seq_show(prog, &meta, v);
2713
+ }
2714
+
2715
+ netlink_native_seq_stop(seq, v);
2716
+}
2717
+#else
2718
+static int netlink_seq_show(struct seq_file *seq, void *v)
2719
+{
2720
+ return netlink_native_seq_show(seq, v);
2721
+}
2722
+
2723
+static void netlink_seq_stop(struct seq_file *seq, void *v)
2724
+{
2725
+ netlink_native_seq_stop(seq, v);
2726
+}
2727
+#endif
26532728
26542729 static const struct seq_operations netlink_seq_ops = {
26552730 .start = netlink_seq_start,
....@@ -2757,6 +2832,34 @@
27572832 .automatic_shrinking = true,
27582833 };
27592834
2835
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
2836
+BTF_ID_LIST(btf_netlink_sock_id)
2837
+BTF_ID(struct, netlink_sock)
2838
+
2839
+static const struct bpf_iter_seq_info netlink_seq_info = {
2840
+ .seq_ops = &netlink_seq_ops,
2841
+ .init_seq_private = bpf_iter_init_seq_net,
2842
+ .fini_seq_private = bpf_iter_fini_seq_net,
2843
+ .seq_priv_size = sizeof(struct nl_seq_iter),
2844
+};
2845
+
2846
+static struct bpf_iter_reg netlink_reg_info = {
2847
+ .target = "netlink",
2848
+ .ctx_arg_info_size = 1,
2849
+ .ctx_arg_info = {
2850
+ { offsetof(struct bpf_iter__netlink, sk),
2851
+ PTR_TO_BTF_ID_OR_NULL },
2852
+ },
2853
+ .seq_info = &netlink_seq_info,
2854
+};
2855
+
2856
+static int __init bpf_iter_register(void)
2857
+{
2858
+ netlink_reg_info.ctx_arg_info[0].btf_id = *btf_netlink_sock_id;
2859
+ return bpf_iter_reg_target(&netlink_reg_info);
2860
+}
2861
+#endif
2862
+
27602863 static int __init netlink_proto_init(void)
27612864 {
27622865 int i;
....@@ -2765,7 +2868,13 @@
27652868 if (err != 0)
27662869 goto out;
27672870
2768
- BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
2871
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
2872
+ err = bpf_iter_register();
2873
+ if (err)
2874
+ goto out;
2875
+#endif
2876
+
2877
+ BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof_field(struct sk_buff, cb));
27692878
27702879 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
27712880 if (!nl_table)
....@@ -2794,8 +2903,4 @@
27942903 panic("netlink_init: Cannot allocate nl_table\n");
27952904 }
27962905
2797
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
2798
-core_initcall_sync(netlink_proto_init);
2799
-#else
28002906 core_initcall(netlink_proto_init);
2801
-#endif