blob: 527015b22e8ed71d84991420b263692df0522491 [file] [log] [blame]
Harald Welte51b00a62014-04-03 09:37:38 -04001/* GTP specific RTNetlink 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 <unistd.h>
26#include <string.h>
27#include <time.h>
28
29#include <libmnl/libmnl.h>
30#include <net/if.h>
31#include <linux/if_link.h>
32#include <linux/rtnetlink.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010033#include <libgtpnl/gtpnl.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010034#include <linux/gtp_nl.h>
Pablo Neira Ayusodeb54082014-03-20 16:23:18 +010035#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <time.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010038
39#include "internal.h"
40
Pablo Neira Ayuso10df0592014-02-22 22:57:07 +010041static struct nlmsghdr *
42gtp_put_nlmsg(char *buf, uint16_t type, uint16_t nl_flags, uint32_t seq)
43{
44 struct nlmsghdr *nlh;
45
46 nlh = mnl_nlmsg_put_header(buf);
47 nlh->nlmsg_type = type;
48 nlh->nlmsg_flags = NLM_F_REQUEST | nl_flags;
49 nlh->nlmsg_seq = seq;
50
51 return nlh;
52}
53
Pablo Neira Ayuso1f683292014-03-20 16:21:29 +010054static struct mnl_socket *rtnl_open(void)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010055{
56 struct mnl_socket *nl;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010057
58 nl = mnl_socket_open(NETLINK_ROUTE);
59 if (nl == NULL) {
60 perror("mnl_socket_open");
Pablo Neira Ayuso1f683292014-03-20 16:21:29 +010061 return NULL;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010062 }
63
64 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
65 perror("mnl_socket_bind");
Pablo Neira Ayuso3876ef62014-02-22 22:50:00 +010066 goto err;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010067 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010068
Pablo Neira Ayuso1f683292014-03-20 16:21:29 +010069 return nl;
Pablo Neira Ayuso3876ef62014-02-22 22:50:00 +010070err:
71 mnl_socket_close(nl);
Pablo Neira Ayuso1f683292014-03-20 16:21:29 +010072 return NULL;
73}
74
75static int rtnl_talk(struct mnl_socket *nl, struct nlmsghdr *nlh)
76{
77 char buf[MNL_SOCKET_BUFFER_SIZE];
78 int ret;
79
80 ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
81 if (ret < 0)
82 return ret;
83
84 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
85 if (ret < 0)
86 return ret;
87
88 return mnl_cb_run(buf, ret, nlh->nlmsg_seq, mnl_socket_get_portid(nl),
89 NULL, NULL);
90}
91
92static int gtp_dev_talk(struct nlmsghdr *nlh, uint32_t seq)
93{
94 struct mnl_socket *nl;
95 int ret;
96
97 nl = rtnl_open();
98 if (nl == NULL)
99 return -1;
100
101 ret = rtnl_talk(nl, nlh);
102
103 mnl_socket_close(nl);
104 return ret;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100105}
Pablo Neira Ayuso10df0592014-02-22 22:57:07 +0100106
Andreas Schultzaf422a42016-04-11 16:10:02 +0200107int gtp_dev_create(int dest_ns, const char *gtp_ifname, int fd0, int fd1)
Pablo Neira Ayuso10df0592014-02-22 22:57:07 +0100108{
109 char buf[MNL_SOCKET_BUFFER_SIZE];
110 struct nlmsghdr *nlh;
111 struct ifinfomsg *ifm;
112 unsigned int seq = time(NULL);
113 struct nlattr *nest, *nest2;
114
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +0100115 nlh = gtp_put_nlmsg(buf, RTM_NEWLINK,
116 NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK, seq);
Pablo Neira Ayuso10df0592014-02-22 22:57:07 +0100117 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
118 ifm->ifi_family = AF_INET;
119 ifm->ifi_change |= IFF_UP;
120 ifm->ifi_flags |= IFF_UP;
121
Andreas Schultz49773302016-04-11 16:09:56 +0200122 if (dest_ns >= 0)
123 mnl_attr_put_u32(nlh, IFLA_NET_NS_FD, dest_ns);
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +0100124 mnl_attr_put_str(nlh, IFLA_IFNAME, gtp_ifname);
Pablo Neira Ayuso10df0592014-02-22 22:57:07 +0100125 nest = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
126 mnl_attr_put_str(nlh, IFLA_INFO_KIND, "gtp");
127 nest2 = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
Pablo Neira Ayusodea76a02014-02-23 21:55:42 +0100128 mnl_attr_put_u32(nlh, IFLA_GTP_FD0, fd0);
129 mnl_attr_put_u32(nlh, IFLA_GTP_FD1, fd1);
Pablo Neira Ayuso10df0592014-02-22 22:57:07 +0100130 mnl_attr_put_u32(nlh, IFLA_GTP_HASHSIZE, 131072);
131 mnl_attr_nest_end(nlh, nest2);
132 mnl_attr_nest_end(nlh, nest);
133
134 return gtp_dev_talk(nlh, seq);
135}
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100136EXPORT_SYMBOL(gtp_dev_create);
Pablo Neira Ayuso41ff5382014-02-22 23:07:41 +0100137
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +0100138int gtp_dev_destroy(const char *gtp_ifname)
Pablo Neira Ayuso41ff5382014-02-22 23:07:41 +0100139{
140 char buf[MNL_SOCKET_BUFFER_SIZE];
141 struct nlmsghdr *nlh;
142 struct ifinfomsg *ifm;
143 unsigned int seq = time(NULL);
144
145 nlh = gtp_put_nlmsg(buf, RTM_DELLINK, NLM_F_ACK, seq);
146 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
147 ifm->ifi_family = AF_INET;
148 ifm->ifi_change |= IFF_UP;
149 ifm->ifi_flags &= ~IFF_UP;
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +0100150 ifm->ifi_index = if_nametoindex(gtp_ifname);
Pablo Neira Ayuso41ff5382014-02-22 23:07:41 +0100151
152 return gtp_dev_talk(nlh, seq);
153}
154EXPORT_SYMBOL(gtp_dev_destroy);
Pablo Neira Ayusodeb54082014-03-20 16:23:18 +0100155
156int gtp_dev_config(const char *ifname, struct in_addr *dst, uint32_t prefix)
157{
158 struct mnl_socket *nl;
159 char buf[MNL_SOCKET_BUFFER_SIZE];
160 struct nlmsghdr *nlh;
161 struct rtmsg *rtm;
162 int iface, ret;
163
164 iface = if_nametoindex(ifname);
165 if (iface == 0) {
166 perror("if_nametoindex");
167 return -1;
168 }
169
170 nlh = mnl_nlmsg_put_header(buf);
171 nlh->nlmsg_type = RTM_NEWROUTE;
172 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
173 nlh->nlmsg_seq = time(NULL);
174
175 rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg));
176 rtm->rtm_family = AF_INET;
177 rtm->rtm_dst_len = prefix;
178 rtm->rtm_src_len = 0;
179 rtm->rtm_tos = 0;
180 rtm->rtm_protocol = RTPROT_STATIC;
181 rtm->rtm_table = RT_TABLE_MAIN;
182 rtm->rtm_type = RTN_UNICAST;
183 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
184 rtm->rtm_flags = 0;
185
186 mnl_attr_put_u32(nlh, RTA_DST, dst->s_addr);
187 mnl_attr_put_u32(nlh, RTA_OIF, iface);
188
189 nl = rtnl_open();
190 if (nl == NULL)
191 return -1;
192
193 ret = rtnl_talk(nl, nlh);
194
195 mnl_socket_close(nl);
196
197 return ret;
198}
199EXPORT_SYMBOL(gtp_dev_config);