blob: 447e1eafa1cbd70069173ca83b8b9ea39fef9afe [file] [log] [blame]
Harald Welte51b00a62014-04-03 09:37:38 -04001/* GTP specific Generic Netlink helper functions */
2
3/* (C) 2014 by sysmocom - s.f.m.c. GmbH
4 * Author: Pablo Neira Ayuso <pablo@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welted2bb0bc2016-07-28 20:34:45 +02009 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
Harald Welte51b00a62014-04-03 09:37:38 -040012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welted2bb0bc2016-07-28 20:34:45 +020016 * GNU Lesser General Public License for more details.
Harald Welte51b00a62014-04-03 09:37:38 -040017 *
Harald Welted2bb0bc2016-07-28 20:34:45 +020018 * You should have received a copy of the GNU Lesser General Public License
Harald Welte51b00a62014-04-03 09:37:38 -040019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <time.h>
28#include <arpa/inet.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <inttypes.h>
32
33#include <libmnl/libmnl.h>
34#include <linux/genetlink.h>
35
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010036#include <libgtpnl/gtp.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010037#include <libgtpnl/gtpnl.h>
38
39#include <net/if.h>
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020040#include <linux/gtp.h>
41#include <linux/if_link.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010042
43#include "internal.h"
44
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010045static void gtp_build_payload(struct nlmsghdr *nlh, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010046{
Oliver Smitha2956152023-10-19 14:11:19 +020047 mnl_attr_put_u8(nlh, GTPA_FAMILY, t->ms_addr.family);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010048 mnl_attr_put_u32(nlh, GTPA_VERSION, t->gtp_version);
Andreas Schultz49773302016-04-11 16:09:56 +020049 if (t->ifns >= 0)
50 mnl_attr_put_u32(nlh, GTPA_NET_NS_FD, t->ifns);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010051 mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx);
Oliver Smitha2956152023-10-19 14:11:19 +020052
53 switch (t->ms_addr.family) {
54 case AF_INET:
Oliver Smith717db092023-10-18 15:16:12 +020055 mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.ip4.s_addr);
Oliver Smitha2956152023-10-19 14:11:19 +020056 break;
57 case AF_INET6:
58 mnl_attr_put(nlh, GTPA_MS_ADDR6, sizeof(t->ms_addr.ip6), &t->ms_addr.ip6);
59 break;
60 default:
61 /* No addr is set when deleting a tunnel */
62 break;
63 }
64
65 switch (t->sgsn_addr.family) {
66 case AF_INET:
67 mnl_attr_put_u32(nlh, GTPA_PEER_ADDRESS, t->sgsn_addr.ip4.s_addr);
68 break;
69 case AF_INET6:
70 mnl_attr_put(nlh, GTPA_PEER_ADDR6, sizeof(t->sgsn_addr.ip6), &t->sgsn_addr.ip6);
71 break;
72 default:
73 /* No addr is set when deleting a tunnel */
74 break;
75 }
76
Andreas Schultz17c816f2016-04-11 16:10:03 +020077 if (t->gtp_version == GTP_V0) {
78 mnl_attr_put_u64(nlh, GTPA_TID, t->u.v0.tid);
79 mnl_attr_put_u16(nlh, GTPA_FLOW, t->u.v0.flowid);
80 } else if (t->gtp_version == GTP_V1) {
81 mnl_attr_put_u32(nlh, GTPA_I_TEI, t->u.v1.i_tei);
82 mnl_attr_put_u32(nlh, GTPA_O_TEI, t->u.v1.o_tei);
83 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010084}
85
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010086int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010087{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010088 struct nlmsghdr *nlh;
89 char buf[MNL_SOCKET_BUFFER_SIZE];
90 uint32_t seq = time(NULL);
91
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010092 if (t->gtp_version > GTP_V1) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010093 fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010094 t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010095 return -1;
96 }
97
98 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020099 GTP_CMD_NEWPDP);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +0100100 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100101
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100102 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100103 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100104 return -1;
105 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100106
107 return 0;
108}
109EXPORT_SYMBOL(gtp_add_tunnel);
110
Pablo Neira Ayuso18532952014-02-22 22:09:59 +0100111int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100112{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100113 char buf[MNL_SOCKET_BUFFER_SIZE];
114 struct nlmsghdr *nlh;
115 uint32_t seq = time(NULL);
116
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100117 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +0200118 GTP_CMD_DELPDP);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +0100119 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100120
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100121 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100122 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100123 return -1;
124 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100125
126 return 0;
127}
128EXPORT_SYMBOL(gtp_del_tunnel);
129
130struct gtp_pdp {
131 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200132 union {
133 struct {
134 uint64_t tid;
135 } v0;
136 struct {
137 uint32_t i_tei;
138 uint32_t o_tei;
139 } v1;
140 } u;
Oliver Smith717db092023-10-18 15:16:12 +0200141 struct gtp_addr sgsn_addr;
142 struct gtp_addr ms_addr;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100143};
144
145static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
146{
147 const struct nlattr **tb = data;
148 int type = mnl_attr_get_type(attr);
149
junpei yoshino345d6872016-08-25 13:06:53 +0900150 if (mnl_attr_type_valid(attr, GTPA_MAX) < 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100151 return MNL_CB_OK;
152
153 switch(type) {
Oliver Smitha2956152023-10-19 14:11:19 +0200154 case GTPA_FAMILY:
155 if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0) {
156 perror("mnl_attr_validate");
157 return MNL_CB_ERROR;
158 }
159 break;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100160 case GTPA_TID:
161 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
162 perror("mnl_attr_validate");
163 return MNL_CB_ERROR;
164 }
165 break;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200166 case GTPA_O_TEI:
167 case GTPA_I_TEI:
Jonas Bonn6e9afbb2017-03-24 15:19:19 +0100168 case GTPA_PEER_ADDRESS:
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100169 case GTPA_MS_ADDRESS:
170 case GTPA_VERSION:
171 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
172 perror("mnl_attr_validate");
173 return MNL_CB_ERROR;
174 }
175 break;
Oliver Smitha2956152023-10-19 14:11:19 +0200176 case GTPA_PEER_ADDR6:
177 case GTPA_MS_ADDR6:
178 if (mnl_attr_validate2(attr, MNL_TYPE_BINARY,
179 sizeof(struct in6_addr)) < 0) {
180 perror("mnl_attr_validate");
181 return MNL_CB_ERROR;
182 }
183 break;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100184 default:
185 break;
186 }
187 tb[type] = attr;
188 return MNL_CB_OK;
189}
190
191static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
192{
193 struct nlattr *tb[GTPA_MAX + 1] = {};
Pablo Neira Ayuso99a5c322023-10-05 20:50:17 +0200194 char buf[INET6_ADDRSTRLEN];
Pablo Neira Ayuso0829e0e2014-02-22 22:19:35 +0100195 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100196 struct genlmsghdr *genl;
197
198 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
Oliver Smitha2956152023-10-19 14:11:19 +0200199
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100200 if (tb[GTPA_TID])
Andreas Schultz17c816f2016-04-11 16:10:03 +0200201 pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
202 if (tb[GTPA_I_TEI])
203 pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
204 if (tb[GTPA_O_TEI])
205 pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
Oliver Smitha2956152023-10-19 14:11:19 +0200206
Jonas Bonn6e9afbb2017-03-24 15:19:19 +0100207 if (tb[GTPA_PEER_ADDRESS]) {
Oliver Smith717db092023-10-18 15:16:12 +0200208 pdp.sgsn_addr.family = AF_INET;
Oliver Smitha2956152023-10-19 14:11:19 +0200209 pdp.sgsn_addr.ip4.s_addr = mnl_attr_get_u32(tb[GTPA_PEER_ADDRESS]);
210 } else if (tb[GTPA_PEER_ADDR6]) {
211 pdp.sgsn_addr.family = AF_INET6;
212 memcpy(&pdp.sgsn_addr.ip6, mnl_attr_get_payload(tb[GTPA_PEER_ADDR6]),
213 sizeof(struct in6_addr));
214 } else {
215 fprintf(stderr, "sgsn_addr: no IPv4 nor IPv6 set\n");
216 return MNL_CB_ERROR;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100217 }
Oliver Smitha2956152023-10-19 14:11:19 +0200218
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100219 if (tb[GTPA_MS_ADDRESS]) {
Oliver Smith717db092023-10-18 15:16:12 +0200220 pdp.ms_addr.family = AF_INET;
221 pdp.ms_addr.ip4.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
Oliver Smitha2956152023-10-19 14:11:19 +0200222 } else if (tb[GTPA_MS_ADDR6]) {
223 pdp.ms_addr.family = AF_INET6;
224 memcpy(&pdp.ms_addr.ip6, mnl_attr_get_payload(tb[GTPA_MS_ADDR6]), sizeof(struct in6_addr));
225 } else {
226 fprintf(stderr, "ms_addr: no IPv4 nor IPv6 set\n");
227 return MNL_CB_ERROR;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100228 }
229
Oliver Smitha2956152023-10-19 14:11:19 +0200230 if (tb[GTPA_FAMILY] && mnl_attr_get_u32(tb[GTPA_FAMILY]) != pdp.ms_addr.family) {
231 fprintf(stderr, "ms_addr family does not match GTPA_FAMILY\n");
232 return MNL_CB_ERROR;
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200233 }
Oliver Smitha2956152023-10-19 14:11:19 +0200234
235 if (tb[GTPA_VERSION])
236 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
237
238 printf("version %u ", pdp.version);
239
240 if (pdp.version == GTP_V0)
241 printf("tid %"PRIu64" ", pdp.u.v0.tid);
242 else if (pdp.version == GTP_V1)
243 printf("tei %u/%u ", pdp.u.v1.i_tei, pdp.u.v1.o_tei);
244
245 inet_ntop(pdp.ms_addr.family, &pdp.ms_addr.ip4, buf, sizeof(buf));
246 printf("ms_addr %s ", buf);
247
248 inet_ntop(pdp.sgsn_addr.family, &pdp.sgsn_addr.ip4, buf, sizeof(buf));
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200249 printf("sgsn_addr %s\n", buf);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100250
251 return MNL_CB_OK;
252}
253
254int gtp_list_tunnel(int genl_id, struct mnl_socket *nl)
255{
256 char buf[MNL_SOCKET_BUFFER_SIZE];
257 struct nlmsghdr *nlh;
258 uint32_t seq = time(NULL);
259
260 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +0200261 GTP_CMD_GETPDP);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100262
263 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
264 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100265 return -1;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100266 }
267
268 return 0;
269}
270EXPORT_SYMBOL(gtp_list_tunnel);