blob: c1f60ab29bd3493e58abf077e7c8db210502a70f [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
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
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
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * 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>
40#include <linux/gtp_nl.h>
41
42#include "internal.h"
43
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010044static void gtp_build_payload(struct nlmsghdr *nlh, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010045{
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010046 mnl_attr_put_u32(nlh, GTPA_VERSION, t->gtp_version);
47 mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx);
48 mnl_attr_put_u32(nlh, GTPA_SGSN_ADDRESS, t->sgsn_addr.s_addr);
49 mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.s_addr);
50 mnl_attr_put_u64(nlh, GTPA_TID, t->tid);
51 mnl_attr_put_u16(nlh, GTPA_FLOWID, t->flowid);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010052}
53
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010054int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010055{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010056 struct nlmsghdr *nlh;
57 char buf[MNL_SOCKET_BUFFER_SIZE];
58 uint32_t seq = time(NULL);
59
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010060 if (t->gtp_version > GTP_V1) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010061 fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010062 t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010063 return -1;
64 }
65
66 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
67 GTP_CMD_TUNNEL_NEW);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010068 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010069
70 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
71 perror("genl_socket_talk");
72
73 return 0;
74}
75EXPORT_SYMBOL(gtp_add_tunnel);
76
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010077int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010078{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010079 char buf[MNL_SOCKET_BUFFER_SIZE];
80 struct nlmsghdr *nlh;
81 uint32_t seq = time(NULL);
82
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010083 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
84 GTP_CMD_TUNNEL_DELETE);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010085 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010086
87 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
88 perror("genl_socket_talk");
89
90 return 0;
91}
92EXPORT_SYMBOL(gtp_del_tunnel);
93
94struct gtp_pdp {
95 uint32_t version;
96 uint64_t tid;
97 struct in_addr sgsn_addr;
98 struct in_addr ms_addr;
99};
100
101static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
102{
103 const struct nlattr **tb = data;
104 int type = mnl_attr_get_type(attr);
105
106 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
107 return MNL_CB_OK;
108
109 switch(type) {
110 case GTPA_TID:
111 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
112 perror("mnl_attr_validate");
113 return MNL_CB_ERROR;
114 }
115 break;
116 case GTPA_SGSN_ADDRESS:
117 case GTPA_MS_ADDRESS:
118 case GTPA_VERSION:
119 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
120 perror("mnl_attr_validate");
121 return MNL_CB_ERROR;
122 }
123 break;
124 default:
125 break;
126 }
127 tb[type] = attr;
128 return MNL_CB_OK;
129}
130
131static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
132{
133 struct nlattr *tb[GTPA_MAX + 1] = {};
Pablo Neira Ayuso0829e0e2014-02-22 22:19:35 +0100134 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100135 struct genlmsghdr *genl;
136
137 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
138 if (tb[GTPA_TID])
139 pdp.tid = mnl_attr_get_u64(tb[GTPA_TID]);
140 if (tb[GTPA_SGSN_ADDRESS]) {
141 pdp.sgsn_addr.s_addr =
142 mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
143 }
144 if (tb[GTPA_MS_ADDRESS]) {
145 pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
146 }
147 if (tb[GTPA_VERSION]) {
148 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
149 }
150
151 printf("version %u ", pdp.version);
152 printf("tid %"PRIu64" ms_addr %s ", pdp.tid, inet_ntoa(pdp.sgsn_addr));
153 printf("sgsn_addr %s\n", inet_ntoa(pdp.ms_addr));
154
155 return MNL_CB_OK;
156}
157
158int gtp_list_tunnel(int genl_id, struct mnl_socket *nl)
159{
160 char buf[MNL_SOCKET_BUFFER_SIZE];
161 struct nlmsghdr *nlh;
162 uint32_t seq = time(NULL);
163
164 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
165 GTP_CMD_TUNNEL_GET);
166
167 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
168 perror("genl_socket_talk");
169 return 0;
170 }
171
172 return 0;
173}
174EXPORT_SYMBOL(gtp_list_tunnel);