blob: 1e77e06d9686fcbce6e62df903260d2f59d65efb [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{
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010047 mnl_attr_put_u32(nlh, GTPA_VERSION, t->gtp_version);
Andreas Schultz49773302016-04-11 16:09:56 +020048 if (t->ifns >= 0)
49 mnl_attr_put_u32(nlh, GTPA_NET_NS_FD, t->ifns);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010050 mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx);
51 mnl_attr_put_u32(nlh, GTPA_SGSN_ADDRESS, t->sgsn_addr.s_addr);
52 mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.s_addr);
Andreas Schultz17c816f2016-04-11 16:10:03 +020053 if (t->gtp_version == GTP_V0) {
54 mnl_attr_put_u64(nlh, GTPA_TID, t->u.v0.tid);
55 mnl_attr_put_u16(nlh, GTPA_FLOW, t->u.v0.flowid);
56 } else if (t->gtp_version == GTP_V1) {
57 mnl_attr_put_u32(nlh, GTPA_I_TEI, t->u.v1.i_tei);
58 mnl_attr_put_u32(nlh, GTPA_O_TEI, t->u.v1.o_tei);
59 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010060}
61
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010062int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010063{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010064 struct nlmsghdr *nlh;
65 char buf[MNL_SOCKET_BUFFER_SIZE];
66 uint32_t seq = time(NULL);
67
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010068 if (t->gtp_version > GTP_V1) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010069 fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010070 t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010071 return -1;
72 }
73
74 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020075 GTP_CMD_NEWPDP);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010076 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010077
78 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
79 perror("genl_socket_talk");
80
81 return 0;
82}
83EXPORT_SYMBOL(gtp_add_tunnel);
84
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010085int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010086{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010087 char buf[MNL_SOCKET_BUFFER_SIZE];
88 struct nlmsghdr *nlh;
89 uint32_t seq = time(NULL);
90
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010091 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020092 GTP_CMD_DELPDP);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010093 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010094
95 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
96 perror("genl_socket_talk");
97
98 return 0;
99}
100EXPORT_SYMBOL(gtp_del_tunnel);
101
102struct gtp_pdp {
103 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200104 union {
105 struct {
106 uint64_t tid;
107 } v0;
108 struct {
109 uint32_t i_tei;
110 uint32_t o_tei;
111 } v1;
112 } u;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100113 struct in_addr sgsn_addr;
114 struct in_addr ms_addr;
115};
116
117static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
118{
119 const struct nlattr **tb = data;
120 int type = mnl_attr_get_type(attr);
121
122 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
123 return MNL_CB_OK;
124
125 switch(type) {
126 case GTPA_TID:
127 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
128 perror("mnl_attr_validate");
129 return MNL_CB_ERROR;
130 }
131 break;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200132 case GTPA_O_TEI:
133 case GTPA_I_TEI:
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100134 case GTPA_SGSN_ADDRESS:
135 case GTPA_MS_ADDRESS:
136 case GTPA_VERSION:
137 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
138 perror("mnl_attr_validate");
139 return MNL_CB_ERROR;
140 }
141 break;
142 default:
143 break;
144 }
145 tb[type] = attr;
146 return MNL_CB_OK;
147}
148
149static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
150{
151 struct nlattr *tb[GTPA_MAX + 1] = {};
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200152 char buf[INET_ADDRSTRLEN];
Pablo Neira Ayuso0829e0e2014-02-22 22:19:35 +0100153 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100154 struct genlmsghdr *genl;
155
156 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
157 if (tb[GTPA_TID])
Andreas Schultz17c816f2016-04-11 16:10:03 +0200158 pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
159 if (tb[GTPA_I_TEI])
160 pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
161 if (tb[GTPA_O_TEI])
162 pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100163 if (tb[GTPA_SGSN_ADDRESS]) {
164 pdp.sgsn_addr.s_addr =
165 mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
166 }
167 if (tb[GTPA_MS_ADDRESS]) {
168 pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
169 }
170 if (tb[GTPA_VERSION]) {
171 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
172 }
173
174 printf("version %u ", pdp.version);
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200175 if (pdp.version == GTP_V0) {
176 inet_ntop(AF_INET, &pdp.ms_addr, buf, sizeof(buf));
Andreas Schultz17c816f2016-04-11 16:10:03 +0200177 printf("tid %"PRIu64" ms_addr %s ",
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200178 pdp.u.v0.tid, buf);
179 } else if (pdp.version == GTP_V1) {
180 inet_ntop(AF_INET, &pdp.ms_addr, buf, sizeof(buf));
Andreas Schultz17c816f2016-04-11 16:10:03 +0200181 printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei,
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200182 pdp.u.v1.o_tei, buf);
183 }
184 inet_ntop(AF_INET, &pdp.sgsn_addr, buf, sizeof(buf));
185 printf("sgsn_addr %s\n", buf);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100186
187 return MNL_CB_OK;
188}
189
190int gtp_list_tunnel(int genl_id, struct mnl_socket *nl)
191{
192 char buf[MNL_SOCKET_BUFFER_SIZE];
193 struct nlmsghdr *nlh;
194 uint32_t seq = time(NULL);
195
196 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +0200197 GTP_CMD_GETPDP);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100198
199 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
200 perror("genl_socket_talk");
201 return 0;
202 }
203
204 return 0;
205}
206EXPORT_SYMBOL(gtp_list_tunnel);