Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 1 | /* 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 23 | #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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 33 | #include <libgtpnl/gtpnl.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 34 | #include <linux/gtp_nl.h> |
Pablo Neira Ayuso | deb5408 | 2014-03-20 16:23:18 +0100 | [diff] [blame] | 35 | #include <netinet/in.h> |
| 36 | #include <arpa/inet.h> |
| 37 | #include <time.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 38 | |
| 39 | #include "internal.h" |
| 40 | |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 41 | static struct nlmsghdr * |
| 42 | gtp_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 Ayuso | 1f68329 | 2014-03-20 16:21:29 +0100 | [diff] [blame] | 54 | static struct mnl_socket *rtnl_open(void) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 55 | { |
| 56 | struct mnl_socket *nl; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 57 | |
| 58 | nl = mnl_socket_open(NETLINK_ROUTE); |
| 59 | if (nl == NULL) { |
| 60 | perror("mnl_socket_open"); |
Pablo Neira Ayuso | 1f68329 | 2014-03-20 16:21:29 +0100 | [diff] [blame] | 61 | return NULL; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 65 | perror("mnl_socket_bind"); |
Pablo Neira Ayuso | 3876ef6 | 2014-02-22 22:50:00 +0100 | [diff] [blame] | 66 | goto err; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 67 | } |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 68 | |
Pablo Neira Ayuso | 1f68329 | 2014-03-20 16:21:29 +0100 | [diff] [blame] | 69 | return nl; |
Pablo Neira Ayuso | 3876ef6 | 2014-02-22 22:50:00 +0100 | [diff] [blame] | 70 | err: |
| 71 | mnl_socket_close(nl); |
Pablo Neira Ayuso | 1f68329 | 2014-03-20 16:21:29 +0100 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | static 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 | |
| 92 | static 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 105 | } |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 106 | |
Andreas Schultz | af422a4 | 2016-04-11 16:10:02 +0200 | [diff] [blame] | 107 | int gtp_dev_create(int dest_ns, const char *gtp_ifname, int fd0, int fd1) |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 108 | { |
| 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 Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 115 | nlh = gtp_put_nlmsg(buf, RTM_NEWLINK, |
| 116 | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK, seq); |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 117 | 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 Schultz | 4977330 | 2016-04-11 16:09:56 +0200 | [diff] [blame] | 122 | if (dest_ns >= 0) |
| 123 | mnl_attr_put_u32(nlh, IFLA_NET_NS_FD, dest_ns); |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 124 | mnl_attr_put_str(nlh, IFLA_IFNAME, gtp_ifname); |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 125 | 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 Ayuso | dea76a0 | 2014-02-23 21:55:42 +0100 | [diff] [blame] | 128 | mnl_attr_put_u32(nlh, IFLA_GTP_FD0, fd0); |
| 129 | mnl_attr_put_u32(nlh, IFLA_GTP_FD1, fd1); |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 130 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 136 | EXPORT_SYMBOL(gtp_dev_create); |
Pablo Neira Ayuso | 41ff538 | 2014-02-22 23:07:41 +0100 | [diff] [blame] | 137 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 138 | int gtp_dev_destroy(const char *gtp_ifname) |
Pablo Neira Ayuso | 41ff538 | 2014-02-22 23:07:41 +0100 | [diff] [blame] | 139 | { |
| 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 Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 150 | ifm->ifi_index = if_nametoindex(gtp_ifname); |
Pablo Neira Ayuso | 41ff538 | 2014-02-22 23:07:41 +0100 | [diff] [blame] | 151 | |
| 152 | return gtp_dev_talk(nlh, seq); |
| 153 | } |
| 154 | EXPORT_SYMBOL(gtp_dev_destroy); |
Pablo Neira Ayuso | deb5408 | 2014-03-20 16:23:18 +0100 | [diff] [blame] | 155 | |
| 156 | int 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 | } |
| 199 | EXPORT_SYMBOL(gtp_dev_config); |