Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | #include <time.h> |
| 6 | #include <arpa/inet.h> |
| 7 | #include <sys/socket.h> |
| 8 | #include <netinet/in.h> |
| 9 | #include <inttypes.h> |
| 10 | |
| 11 | #include <libmnl/libmnl.h> |
| 12 | #include <linux/genetlink.h> |
| 13 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 14 | #include <libgtpnl/gtp.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 15 | #include <libgtpnl/gtpnl.h> |
| 16 | |
| 17 | #include <net/if.h> |
| 18 | #include <linux/gtp_nl.h> |
| 19 | |
| 20 | #include "internal.h" |
| 21 | |
| 22 | static void gtp_build_payload(struct nlmsghdr *nlh, uint64_t tid, |
| 23 | uint32_t ifidx, uint32_t sgsn_addr, |
| 24 | uint32_t ms_addr, uint32_t version) |
| 25 | { |
| 26 | mnl_attr_put_u32(nlh, GTPA_VERSION, version); |
| 27 | mnl_attr_put_u32(nlh, GTPA_LINK, ifidx); |
| 28 | mnl_attr_put_u32(nlh, GTPA_SGSN_ADDRESS, sgsn_addr); |
| 29 | mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, ms_addr); |
| 30 | mnl_attr_put_u64(nlh, GTPA_TID, tid); |
| 31 | } |
| 32 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 33 | 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] | 34 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 35 | struct nlmsghdr *nlh; |
| 36 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 37 | uint32_t seq = time(NULL); |
| 38 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 39 | if (t->gtp_version > GTP_V1) { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 40 | fprintf(stderr, "wrong GTP version %u, use v0 or v1\n", |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 41 | t->gtp_version); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq, |
| 46 | GTP_CMD_TUNNEL_NEW); |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 47 | gtp_build_payload(nlh, t->tid, t->ifidx, t->sgsn_addr.s_addr, |
| 48 | t->ms_addr.s_addr, t->gtp_version); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 49 | |
| 50 | if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) |
| 51 | perror("genl_socket_talk"); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | EXPORT_SYMBOL(gtp_add_tunnel); |
| 56 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 57 | 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] | 58 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 59 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 60 | struct nlmsghdr *nlh; |
| 61 | uint32_t seq = time(NULL); |
| 62 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 63 | nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq, |
| 64 | GTP_CMD_TUNNEL_DELETE); |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 65 | gtp_build_payload(nlh, t->tid, t->ifidx, 0, 0, t->gtp_version); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 66 | |
| 67 | if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) |
| 68 | perror("genl_socket_talk"); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | EXPORT_SYMBOL(gtp_del_tunnel); |
| 73 | |
| 74 | struct gtp_pdp { |
| 75 | uint32_t version; |
| 76 | uint64_t tid; |
| 77 | struct in_addr sgsn_addr; |
| 78 | struct in_addr ms_addr; |
| 79 | }; |
| 80 | |
| 81 | static int genl_gtp_validate_cb(const struct nlattr *attr, void *data) |
| 82 | { |
| 83 | const struct nlattr **tb = data; |
| 84 | int type = mnl_attr_get_type(attr); |
| 85 | |
| 86 | if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0) |
| 87 | return MNL_CB_OK; |
| 88 | |
| 89 | switch(type) { |
| 90 | case GTPA_TID: |
| 91 | if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) { |
| 92 | perror("mnl_attr_validate"); |
| 93 | return MNL_CB_ERROR; |
| 94 | } |
| 95 | break; |
| 96 | case GTPA_SGSN_ADDRESS: |
| 97 | case GTPA_MS_ADDRESS: |
| 98 | case GTPA_VERSION: |
| 99 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { |
| 100 | perror("mnl_attr_validate"); |
| 101 | return MNL_CB_ERROR; |
| 102 | } |
| 103 | break; |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | tb[type] = attr; |
| 108 | return MNL_CB_OK; |
| 109 | } |
| 110 | |
| 111 | static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data) |
| 112 | { |
| 113 | struct nlattr *tb[GTPA_MAX + 1] = {}; |
Pablo Neira Ayuso | 0829e0e | 2014-02-22 22:19:35 +0100 | [diff] [blame] | 114 | struct gtp_pdp pdp = {}; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 115 | struct genlmsghdr *genl; |
| 116 | |
| 117 | mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb); |
| 118 | if (tb[GTPA_TID]) |
| 119 | pdp.tid = mnl_attr_get_u64(tb[GTPA_TID]); |
| 120 | if (tb[GTPA_SGSN_ADDRESS]) { |
| 121 | pdp.sgsn_addr.s_addr = |
| 122 | mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]); |
| 123 | } |
| 124 | if (tb[GTPA_MS_ADDRESS]) { |
| 125 | pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]); |
| 126 | } |
| 127 | if (tb[GTPA_VERSION]) { |
| 128 | pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]); |
| 129 | } |
| 130 | |
| 131 | printf("version %u ", pdp.version); |
| 132 | printf("tid %"PRIu64" ms_addr %s ", pdp.tid, inet_ntoa(pdp.sgsn_addr)); |
| 133 | printf("sgsn_addr %s\n", inet_ntoa(pdp.ms_addr)); |
| 134 | |
| 135 | return MNL_CB_OK; |
| 136 | } |
| 137 | |
| 138 | int gtp_list_tunnel(int genl_id, struct mnl_socket *nl) |
| 139 | { |
| 140 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 141 | struct nlmsghdr *nlh; |
| 142 | uint32_t seq = time(NULL); |
| 143 | |
| 144 | nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0, |
| 145 | GTP_CMD_TUNNEL_GET); |
| 146 | |
| 147 | if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) { |
| 148 | perror("genl_socket_talk"); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | EXPORT_SYMBOL(gtp_list_tunnel); |