forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
kernel/drivers/net/gtp.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /* GTP according to GSM TS 09.60 / 3GPP TS 29.060
23 *
34 * (C) 2012-2014 by sysmocom - s.f.m.c. GmbH
....@@ -6,11 +7,6 @@
67 * Author: Harald Welte <hwelte@sysmocom.de>
78 * Pablo Neira Ayuso <pablo@netfilter.org>
89 * Andreas Schultz <aschultz@travelping.com>
9
- *
10
- * This program is free software; you can redistribute it and/or
11
- * modify it under the terms of the GNU General Public License
12
- * as published by the Free Software Foundation; either version
13
- * 2 of the License, or (at your option) any later version.
1410 */
1511
1612 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -186,8 +182,6 @@
186182 static int gtp_rx(struct pdp_ctx *pctx, struct sk_buff *skb,
187183 unsigned int hdrlen, unsigned int role)
188184 {
189
- struct pcpu_sw_netstats *stats;
190
-
191185 if (!gtp_check_ms(skb, pctx, hdrlen, role)) {
192186 netdev_dbg(pctx->dev, "No PDP ctx for this MS\n");
193187 return 1;
....@@ -208,11 +202,7 @@
208202
209203 skb->dev = pctx->dev;
210204
211
- stats = this_cpu_ptr(pctx->dev->tstats);
212
- u64_stats_update_begin(&stats->syncp);
213
- stats->rx_packets++;
214
- stats->rx_bytes += skb->len;
215
- u64_stats_update_end(&stats->syncp);
205
+ dev_sw_netstats_rx_add(pctx->dev, skb->len);
216206
217207 netif_rx(skb);
218208 return 0;
....@@ -714,7 +704,6 @@
714704 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
715705 pdp_context_delete(pctx);
716706
717
- gtp_encap_disable(gtp);
718707 list_del_rcu(&gtp->list);
719708 unregister_netdevice_queue(dev, head);
720709 }
....@@ -858,8 +847,7 @@
858847
859848 sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
860849 if (IS_ERR(sk1u)) {
861
- if (sk0)
862
- gtp_encap_disable_sock(sk0);
850
+ gtp_encap_disable_sock(sk0);
863851 return PTR_ERR(sk1u);
864852 }
865853 }
....@@ -867,10 +855,8 @@
867855 if (data[IFLA_GTP_ROLE]) {
868856 role = nla_get_u32(data[IFLA_GTP_ROLE]);
869857 if (role > GTP_ROLE_SGSN) {
870
- if (sk0)
871
- gtp_encap_disable_sock(sk0);
872
- if (sk1u)
873
- gtp_encap_disable_sock(sk1u);
858
+ gtp_encap_disable_sock(sk0);
859
+ gtp_encap_disable_sock(sk1u);
874860 return -EINVAL;
875861 }
876862 }
....@@ -935,8 +921,8 @@
935921 }
936922 }
937923
938
-static int gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
939
- struct genl_info *info)
924
+static struct pdp_ctx *gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
925
+ struct genl_info *info)
940926 {
941927 struct pdp_ctx *pctx, *pctx_tid = NULL;
942928 struct net_device *dev = gtp->dev;
....@@ -963,12 +949,12 @@
963949
964950 if (found) {
965951 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
966
- return -EEXIST;
952
+ return ERR_PTR(-EEXIST);
967953 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
968
- return -EOPNOTSUPP;
954
+ return ERR_PTR(-EOPNOTSUPP);
969955
970956 if (pctx && pctx_tid)
971
- return -EEXIST;
957
+ return ERR_PTR(-EEXIST);
972958 if (!pctx)
973959 pctx = pctx_tid;
974960
....@@ -981,13 +967,13 @@
981967 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
982968 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
983969
984
- return 0;
970
+ return pctx;
985971
986972 }
987973
988974 pctx = kmalloc(sizeof(*pctx), GFP_ATOMIC);
989975 if (pctx == NULL)
990
- return -ENOMEM;
976
+ return ERR_PTR(-ENOMEM);
991977
992978 sock_hold(sk);
993979 pctx->sk = sk;
....@@ -1025,7 +1011,7 @@
10251011 break;
10261012 }
10271013
1028
- return 0;
1014
+ return pctx;
10291015 }
10301016
10311017 static void pdp_context_free(struct rcu_head *head)
....@@ -1043,9 +1029,12 @@
10431029 call_rcu(&pctx->rcu_head, pdp_context_free);
10441030 }
10451031
1032
+static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation);
1033
+
10461034 static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
10471035 {
10481036 unsigned int version;
1037
+ struct pdp_ctx *pctx;
10491038 struct gtp_dev *gtp;
10501039 struct sock *sk;
10511040 int err;
....@@ -1075,7 +1064,6 @@
10751064 }
10761065
10771066 rtnl_lock();
1078
- rcu_read_lock();
10791067
10801068 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
10811069 if (!gtp) {
....@@ -1095,10 +1083,15 @@
10951083 goto out_unlock;
10961084 }
10971085
1098
- err = gtp_pdp_add(gtp, sk, info);
1086
+ pctx = gtp_pdp_add(gtp, sk, info);
1087
+ if (IS_ERR(pctx)) {
1088
+ err = PTR_ERR(pctx);
1089
+ } else {
1090
+ gtp_tunnel_notify(pctx, GTP_CMD_NEWPDP, GFP_KERNEL);
1091
+ err = 0;
1092
+ }
10991093
11001094 out_unlock:
1101
- rcu_read_unlock();
11021095 rtnl_unlock();
11031096 return err;
11041097 }
....@@ -1166,6 +1159,7 @@
11661159 netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
11671160 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
11681161
1162
+ gtp_tunnel_notify(pctx, GTP_CMD_DELPDP, GFP_ATOMIC);
11691163 pdp_context_delete(pctx);
11701164
11711165 out_unlock:
....@@ -1174,6 +1168,14 @@
11741168 }
11751169
11761170 static struct genl_family gtp_genl_family;
1171
+
1172
+enum gtp_multicast_groups {
1173
+ GTP_GENL_MCGRP,
1174
+};
1175
+
1176
+static const struct genl_multicast_group gtp_genl_mcgrps[] = {
1177
+ [GTP_GENL_MCGRP] = { .name = GTP_GENL_MCGRP_NAME },
1178
+};
11771179
11781180 static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
11791181 int flags, u32 type, struct pdp_ctx *pctx)
....@@ -1210,6 +1212,26 @@
12101212 nla_put_failure:
12111213 genlmsg_cancel(skb, genlh);
12121214 return -EMSGSIZE;
1215
+}
1216
+
1217
+static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation)
1218
+{
1219
+ struct sk_buff *msg;
1220
+ int ret;
1221
+
1222
+ msg = nlmsg_new(NLMSG_DEFAULT_SIZE, allocation);
1223
+ if (!msg)
1224
+ return -ENOMEM;
1225
+
1226
+ ret = gtp_genl_fill_info(msg, 0, 0, 0, cmd, pctx);
1227
+ if (ret < 0) {
1228
+ nlmsg_free(msg);
1229
+ return ret;
1230
+ }
1231
+
1232
+ ret = genlmsg_multicast_netns(&gtp_genl_family, dev_net(pctx->dev), msg,
1233
+ 0, GTP_GENL_MCGRP, GFP_ATOMIC);
1234
+ return ret;
12131235 }
12141236
12151237 static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
....@@ -1310,24 +1332,24 @@
13101332 [GTPA_O_TEI] = { .type = NLA_U32, },
13111333 };
13121334
1313
-static const struct genl_ops gtp_genl_ops[] = {
1335
+static const struct genl_small_ops gtp_genl_ops[] = {
13141336 {
13151337 .cmd = GTP_CMD_NEWPDP,
1338
+ .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13161339 .doit = gtp_genl_new_pdp,
1317
- .policy = gtp_genl_policy,
13181340 .flags = GENL_ADMIN_PERM,
13191341 },
13201342 {
13211343 .cmd = GTP_CMD_DELPDP,
1344
+ .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13221345 .doit = gtp_genl_del_pdp,
1323
- .policy = gtp_genl_policy,
13241346 .flags = GENL_ADMIN_PERM,
13251347 },
13261348 {
13271349 .cmd = GTP_CMD_GETPDP,
1350
+ .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13281351 .doit = gtp_genl_get_pdp,
13291352 .dumpit = gtp_genl_dump_pdp,
1330
- .policy = gtp_genl_policy,
13311353 .flags = GENL_ADMIN_PERM,
13321354 },
13331355 };
....@@ -1337,10 +1359,13 @@
13371359 .version = 0,
13381360 .hdrsize = 0,
13391361 .maxattr = GTPA_MAX,
1362
+ .policy = gtp_genl_policy,
13401363 .netnsok = true,
13411364 .module = THIS_MODULE,
1342
- .ops = gtp_genl_ops,
1343
- .n_ops = ARRAY_SIZE(gtp_genl_ops),
1365
+ .small_ops = gtp_genl_ops,
1366
+ .n_small_ops = ARRAY_SIZE(gtp_genl_ops),
1367
+ .mcgrps = gtp_genl_mcgrps,
1368
+ .n_mcgrps = ARRAY_SIZE(gtp_genl_mcgrps),
13441369 };
13451370
13461371 static int __net_init gtp_net_init(struct net *net)