Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame^] | 1 | /* 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 23 | #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 Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 36 | #include <libgtpnl/gtp.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 37 | #include <libgtpnl/gtpnl.h> |
| 38 | |
| 39 | #include <net/if.h> |
| 40 | #include <linux/gtp_nl.h> |
| 41 | |
| 42 | #include "internal.h" |
| 43 | |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 44 | static void gtp_build_payload(struct nlmsghdr *nlh, struct gtp_tunnel *t) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 45 | { |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 46 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 54 | int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 55 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 56 | struct nlmsghdr *nlh; |
| 57 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 58 | uint32_t seq = time(NULL); |
| 59 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 60 | if (t->gtp_version > GTP_V1) { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 61 | fprintf(stderr, "wrong GTP version %u, use v0 or v1\n", |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 62 | t->gtp_version); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 63 | 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 Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 68 | gtp_build_payload(nlh, t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 69 | |
| 70 | if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) |
| 71 | perror("genl_socket_talk"); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | EXPORT_SYMBOL(gtp_add_tunnel); |
| 76 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 77 | int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 78 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 79 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 80 | struct nlmsghdr *nlh; |
| 81 | uint32_t seq = time(NULL); |
| 82 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 83 | nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq, |
| 84 | GTP_CMD_TUNNEL_DELETE); |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 85 | gtp_build_payload(nlh, t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 86 | |
| 87 | if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) |
| 88 | perror("genl_socket_talk"); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | EXPORT_SYMBOL(gtp_del_tunnel); |
| 93 | |
| 94 | struct gtp_pdp { |
| 95 | uint32_t version; |
| 96 | uint64_t tid; |
| 97 | struct in_addr sgsn_addr; |
| 98 | struct in_addr ms_addr; |
| 99 | }; |
| 100 | |
| 101 | static 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 | |
| 131 | static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data) |
| 132 | { |
| 133 | struct nlattr *tb[GTPA_MAX + 1] = {}; |
Pablo Neira Ayuso | 0829e0e | 2014-02-22 22:19:35 +0100 | [diff] [blame] | 134 | struct gtp_pdp pdp = {}; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 135 | 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 | |
| 158 | int 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 | } |
| 174 | EXPORT_SYMBOL(gtp_list_tunnel); |