forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
kernel/net/key/af_key.c
....@@ -1,10 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
3
- *
4
- * This program is free software; you can redistribute it and/or
5
- * modify it under the terms of the GNU General Public License
6
- * as published by the Free Software Foundation; either version
7
- * 2 of the License, or (at your option) any later version.
84 *
95 * Authors: Maxim Giryaev <gem@asplinux.ru>
106 * David S. Miller <davem@redhat.com>
....@@ -932,8 +928,7 @@
932928 pfkey_sockaddr_fill(&x->props.saddr, 0,
933929 (struct sockaddr *) (addr + 1),
934930 x->props.family);
935
- if (!addr->sadb_address_prefixlen)
936
- BUG();
931
+ BUG_ON(!addr->sadb_address_prefixlen);
937932
938933 /* dst address */
939934 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
....@@ -948,8 +943,7 @@
948943 pfkey_sockaddr_fill(&x->id.daddr, 0,
949944 (struct sockaddr *) (addr + 1),
950945 x->props.family);
951
- if (!addr->sadb_address_prefixlen)
952
- BUG();
946
+ BUG_ON(!addr->sadb_address_prefixlen);
953947
954948 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
955949 x->props.family)) {
....@@ -1707,9 +1701,12 @@
17071701 pfk->registered |= (1<<hdr->sadb_msg_satype);
17081702 }
17091703
1704
+ mutex_lock(&pfkey_mutex);
17101705 xfrm_probe_algs();
17111706
1712
- supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1707
+ supp_skb = compose_sadb_supported(hdr, GFP_KERNEL | __GFP_ZERO);
1708
+ mutex_unlock(&pfkey_mutex);
1709
+
17131710 if (!supp_skb) {
17141711 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
17151712 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
....@@ -2019,7 +2016,7 @@
20192016
20202017 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
20212018 {
2022
- struct xfrm_sec_ctx *xfrm_ctx = xp->security;
2019
+ struct xfrm_sec_ctx *xfrm_ctx = xp->security;
20232020
20242021 if (xfrm_ctx) {
20252022 int len = sizeof(struct sadb_x_sec_ctx);
....@@ -2413,7 +2410,7 @@
24132410 return err;
24142411 }
24152412
2416
- xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, 0, XFRM_POLICY_TYPE_MAIN,
2413
+ xp = xfrm_policy_bysel_ctx(net, &dummy_mark, 0, XFRM_POLICY_TYPE_MAIN,
24172414 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
24182415 1, &err);
24192416 security_xfrm_policy_free(pol_ctx);
....@@ -2633,7 +2630,7 @@
26332630 }
26342631
26352632 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2636
- kma ? &k : NULL, net, NULL);
2633
+ kma ? &k : NULL, net, NULL, 0);
26372634
26382635 out:
26392636 return err;
....@@ -2664,7 +2661,7 @@
26642661 return -EINVAL;
26652662
26662663 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2667
- xp = xfrm_policy_byid(net, DUMMY_MARK, 0, XFRM_POLICY_TYPE_MAIN,
2664
+ xp = xfrm_policy_byid(net, &dummy_mark, 0, XFRM_POLICY_TYPE_MAIN,
26682665 dir, pol->sadb_x_policy_id, delete, &err);
26692666 if (xp == NULL)
26702667 return -ENOENT;
....@@ -2836,6 +2833,10 @@
28362833 void *ext_hdrs[SADB_EXT_MAX];
28372834 int err;
28382835
2836
+ /* Non-zero return value of pfkey_broadcast() does not always signal
2837
+ * an error and even on an actual error we may still want to process
2838
+ * the message so rather ignore the return value.
2839
+ */
28392840 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
28402841 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
28412842
....@@ -2944,9 +2945,10 @@
29442945 return sz + sizeof(struct sadb_prop);
29452946 }
29462947
2947
-static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2948
+static int dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
29482949 {
29492950 struct sadb_prop *p;
2951
+ int sz = 0;
29502952 int i;
29512953
29522954 p = skb_put(skb, sizeof(struct sadb_prop));
....@@ -2974,13 +2976,17 @@
29742976 c->sadb_comb_soft_addtime = 20*60*60;
29752977 c->sadb_comb_hard_usetime = 8*60*60;
29762978 c->sadb_comb_soft_usetime = 7*60*60;
2979
+ sz += sizeof(*c);
29772980 }
29782981 }
2982
+
2983
+ return sz + sizeof(*p);
29792984 }
29802985
2981
-static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2986
+static int dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
29822987 {
29832988 struct sadb_prop *p;
2989
+ int sz = 0;
29842990 int i, k;
29852991
29862992 p = skb_put(skb, sizeof(struct sadb_prop));
....@@ -3022,8 +3028,11 @@
30223028 c->sadb_comb_soft_addtime = 20*60*60;
30233029 c->sadb_comb_hard_usetime = 8*60*60;
30243030 c->sadb_comb_soft_usetime = 7*60*60;
3031
+ sz += sizeof(*c);
30253032 }
30263033 }
3034
+
3035
+ return sz + sizeof(*p);
30273036 }
30283037
30293038 static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
....@@ -3153,6 +3162,7 @@
31533162 struct sadb_x_sec_ctx *sec_ctx;
31543163 struct xfrm_sec_ctx *xfrm_ctx;
31553164 int ctx_size = 0;
3165
+ int alg_size = 0;
31563166
31573167 sockaddr_size = pfkey_sockaddr_size(x->props.family);
31583168 if (!sockaddr_size)
....@@ -3164,16 +3174,16 @@
31643174 sizeof(struct sadb_x_policy);
31653175
31663176 if (x->id.proto == IPPROTO_AH)
3167
- size += count_ah_combs(t);
3177
+ alg_size = count_ah_combs(t);
31683178 else if (x->id.proto == IPPROTO_ESP)
3169
- size += count_esp_combs(t);
3179
+ alg_size = count_esp_combs(t);
31703180
31713181 if ((xfrm_ctx = x->security)) {
31723182 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
31733183 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
31743184 }
31753185
3176
- skb = alloc_skb(size + 16, GFP_ATOMIC);
3186
+ skb = alloc_skb(size + alg_size + 16, GFP_ATOMIC);
31773187 if (skb == NULL)
31783188 return -ENOMEM;
31793189
....@@ -3227,10 +3237,13 @@
32273237 pol->sadb_x_policy_priority = xp->priority;
32283238
32293239 /* Set sadb_comb's. */
3240
+ alg_size = 0;
32303241 if (x->id.proto == IPPROTO_AH)
3231
- dump_ah_combs(skb, t);
3242
+ alg_size = dump_ah_combs(skb, t);
32323243 else if (x->id.proto == IPPROTO_ESP)
3233
- dump_esp_combs(skb, t);
3244
+ alg_size = dump_esp_combs(skb, t);
3245
+
3246
+ hdr->sadb_msg_len += alg_size / 8;
32343247
32353248 /* security context */
32363249 if (xfrm_ctx) {
....@@ -3747,8 +3760,6 @@
37473760 .ioctl = sock_no_ioctl,
37483761 .listen = sock_no_listen,
37493762 .shutdown = sock_no_shutdown,
3750
- .setsockopt = sock_no_setsockopt,
3751
- .getsockopt = sock_no_getsockopt,
37523763 .mmap = sock_no_mmap,
37533764 .sendpage = sock_no_sendpage,
37543765