hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
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;
....@@ -301,7 +291,9 @@
301291 gtp->sk1u = NULL;
302292 udp_sk(sk)->encap_type = 0;
303293 rcu_assign_sk_user_data(sk, NULL);
294
+ release_sock(sk);
304295 sock_put(sk);
296
+ return;
305297 }
306298 release_sock(sk);
307299 }
....@@ -714,7 +706,6 @@
714706 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
715707 pdp_context_delete(pctx);
716708
717
- gtp_encap_disable(gtp);
718709 list_del_rcu(&gtp->list);
719710 unregister_netdevice_queue(dev, head);
720711 }
....@@ -858,8 +849,7 @@
858849
859850 sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
860851 if (IS_ERR(sk1u)) {
861
- if (sk0)
862
- gtp_encap_disable_sock(sk0);
852
+ gtp_encap_disable_sock(sk0);
863853 return PTR_ERR(sk1u);
864854 }
865855 }
....@@ -867,10 +857,8 @@
867857 if (data[IFLA_GTP_ROLE]) {
868858 role = nla_get_u32(data[IFLA_GTP_ROLE]);
869859 if (role > GTP_ROLE_SGSN) {
870
- if (sk0)
871
- gtp_encap_disable_sock(sk0);
872
- if (sk1u)
873
- gtp_encap_disable_sock(sk1u);
860
+ gtp_encap_disable_sock(sk0);
861
+ gtp_encap_disable_sock(sk1u);
874862 return -EINVAL;
875863 }
876864 }
....@@ -935,8 +923,8 @@
935923 }
936924 }
937925
938
-static int gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
939
- struct genl_info *info)
926
+static struct pdp_ctx *gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
927
+ struct genl_info *info)
940928 {
941929 struct pdp_ctx *pctx, *pctx_tid = NULL;
942930 struct net_device *dev = gtp->dev;
....@@ -963,12 +951,12 @@
963951
964952 if (found) {
965953 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
966
- return -EEXIST;
954
+ return ERR_PTR(-EEXIST);
967955 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
968
- return -EOPNOTSUPP;
956
+ return ERR_PTR(-EOPNOTSUPP);
969957
970958 if (pctx && pctx_tid)
971
- return -EEXIST;
959
+ return ERR_PTR(-EEXIST);
972960 if (!pctx)
973961 pctx = pctx_tid;
974962
....@@ -981,13 +969,13 @@
981969 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
982970 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
983971
984
- return 0;
972
+ return pctx;
985973
986974 }
987975
988976 pctx = kmalloc(sizeof(*pctx), GFP_ATOMIC);
989977 if (pctx == NULL)
990
- return -ENOMEM;
978
+ return ERR_PTR(-ENOMEM);
991979
992980 sock_hold(sk);
993981 pctx->sk = sk;
....@@ -1025,7 +1013,7 @@
10251013 break;
10261014 }
10271015
1028
- return 0;
1016
+ return pctx;
10291017 }
10301018
10311019 static void pdp_context_free(struct rcu_head *head)
....@@ -1043,9 +1031,12 @@
10431031 call_rcu(&pctx->rcu_head, pdp_context_free);
10441032 }
10451033
1034
+static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation);
1035
+
10461036 static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
10471037 {
10481038 unsigned int version;
1039
+ struct pdp_ctx *pctx;
10491040 struct gtp_dev *gtp;
10501041 struct sock *sk;
10511042 int err;
....@@ -1075,7 +1066,6 @@
10751066 }
10761067
10771068 rtnl_lock();
1078
- rcu_read_lock();
10791069
10801070 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
10811071 if (!gtp) {
....@@ -1095,10 +1085,15 @@
10951085 goto out_unlock;
10961086 }
10971087
1098
- err = gtp_pdp_add(gtp, sk, info);
1088
+ pctx = gtp_pdp_add(gtp, sk, info);
1089
+ if (IS_ERR(pctx)) {
1090
+ err = PTR_ERR(pctx);
1091
+ } else {
1092
+ gtp_tunnel_notify(pctx, GTP_CMD_NEWPDP, GFP_KERNEL);
1093
+ err = 0;
1094
+ }
10991095
11001096 out_unlock:
1101
- rcu_read_unlock();
11021097 rtnl_unlock();
11031098 return err;
11041099 }
....@@ -1166,6 +1161,7 @@
11661161 netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
11671162 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
11681163
1164
+ gtp_tunnel_notify(pctx, GTP_CMD_DELPDP, GFP_ATOMIC);
11691165 pdp_context_delete(pctx);
11701166
11711167 out_unlock:
....@@ -1174,6 +1170,14 @@
11741170 }
11751171
11761172 static struct genl_family gtp_genl_family;
1173
+
1174
+enum gtp_multicast_groups {
1175
+ GTP_GENL_MCGRP,
1176
+};
1177
+
1178
+static const struct genl_multicast_group gtp_genl_mcgrps[] = {
1179
+ [GTP_GENL_MCGRP] = { .name = GTP_GENL_MCGRP_NAME },
1180
+};
11771181
11781182 static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
11791183 int flags, u32 type, struct pdp_ctx *pctx)
....@@ -1210,6 +1214,26 @@
12101214 nla_put_failure:
12111215 genlmsg_cancel(skb, genlh);
12121216 return -EMSGSIZE;
1217
+}
1218
+
1219
+static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation)
1220
+{
1221
+ struct sk_buff *msg;
1222
+ int ret;
1223
+
1224
+ msg = nlmsg_new(NLMSG_DEFAULT_SIZE, allocation);
1225
+ if (!msg)
1226
+ return -ENOMEM;
1227
+
1228
+ ret = gtp_genl_fill_info(msg, 0, 0, 0, cmd, pctx);
1229
+ if (ret < 0) {
1230
+ nlmsg_free(msg);
1231
+ return ret;
1232
+ }
1233
+
1234
+ ret = genlmsg_multicast_netns(&gtp_genl_family, dev_net(pctx->dev), msg,
1235
+ 0, GTP_GENL_MCGRP, GFP_ATOMIC);
1236
+ return ret;
12131237 }
12141238
12151239 static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
....@@ -1310,24 +1334,24 @@
13101334 [GTPA_O_TEI] = { .type = NLA_U32, },
13111335 };
13121336
1313
-static const struct genl_ops gtp_genl_ops[] = {
1337
+static const struct genl_small_ops gtp_genl_ops[] = {
13141338 {
13151339 .cmd = GTP_CMD_NEWPDP,
1340
+ .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13161341 .doit = gtp_genl_new_pdp,
1317
- .policy = gtp_genl_policy,
13181342 .flags = GENL_ADMIN_PERM,
13191343 },
13201344 {
13211345 .cmd = GTP_CMD_DELPDP,
1346
+ .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13221347 .doit = gtp_genl_del_pdp,
1323
- .policy = gtp_genl_policy,
13241348 .flags = GENL_ADMIN_PERM,
13251349 },
13261350 {
13271351 .cmd = GTP_CMD_GETPDP,
1352
+ .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13281353 .doit = gtp_genl_get_pdp,
13291354 .dumpit = gtp_genl_dump_pdp,
1330
- .policy = gtp_genl_policy,
13311355 .flags = GENL_ADMIN_PERM,
13321356 },
13331357 };
....@@ -1337,10 +1361,13 @@
13371361 .version = 0,
13381362 .hdrsize = 0,
13391363 .maxattr = GTPA_MAX,
1364
+ .policy = gtp_genl_policy,
13401365 .netnsok = true,
13411366 .module = THIS_MODULE,
1342
- .ops = gtp_genl_ops,
1343
- .n_ops = ARRAY_SIZE(gtp_genl_ops),
1367
+ .small_ops = gtp_genl_ops,
1368
+ .n_small_ops = ARRAY_SIZE(gtp_genl_ops),
1369
+ .mcgrps = gtp_genl_mcgrps,
1370
+ .n_mcgrps = ARRAY_SIZE(gtp_genl_mcgrps),
13441371 };
13451372
13461373 static int __net_init gtp_net_init(struct net *net)