Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 1 | /* Command line utility to create GTP link */ |
| 2 | |
| 3 | /* (C) 2014 by sysmocom - s.f.m.c. GmbH |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 4 | * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org> |
| 5 | * |
Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 6 | * Author: Pablo Neira Ayuso <pablo@gnumonks.org> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Affero General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <string.h> |
| 29 | #include <time.h> |
| 30 | |
| 31 | #include <libmnl/libmnl.h> |
| 32 | #include <linux/if.h> |
| 33 | #include <linux/if_link.h> |
| 34 | #include <linux/rtnetlink.h> |
| 35 | |
Pablo Neira Ayuso | b9f6ffe | 2016-05-08 18:27:55 +0200 | [diff] [blame] | 36 | #include <linux/gtp.h> |
| 37 | #include <linux/if_link.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 38 | |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 39 | #include <libgtpnl/gtpnl.h> |
| 40 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 41 | int main(int argc, char *argv[]) |
| 42 | { |
| 43 | struct mnl_socket *nl; |
| 44 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 45 | struct nlmsghdr *nlh; |
| 46 | struct ifinfomsg *ifm; |
| 47 | int ret; |
Pablo Neira Ayuso | 7b30c55 | 2016-05-08 21:50:05 +0200 | [diff] [blame^] | 48 | unsigned int seq, portid; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 49 | struct nlattr *nest, *nest2; |
| 50 | |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 51 | if (argc != 3) { |
| 52 | printf("Usage: %s <add|del> <device>\n", argv[0]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 53 | exit(EXIT_FAILURE); |
| 54 | } |
| 55 | |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 56 | if (!strcmp(argv[1], "del")) { |
| 57 | printf("destroying gtp interface...\n"); |
| 58 | if (gtp_dev_destroy(argv[2]) < 0) |
| 59 | perror("gtp_dev_destroy"); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 64 | nlh = mnl_nlmsg_put_header(buf); |
| 65 | nlh->nlmsg_type = RTM_NEWLINK; |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 66 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL |NLM_F_ACK; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 67 | nlh->nlmsg_seq = seq = time(NULL); |
| 68 | ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm)); |
| 69 | ifm->ifi_family = AF_INET; |
| 70 | ifm->ifi_change |= IFF_UP; |
| 71 | ifm->ifi_flags |= IFF_UP; |
| 72 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 73 | int fd1 = socket(AF_INET, SOCK_DGRAM, 0); |
| 74 | int fd2 = socket(AF_INET, SOCK_DGRAM, 0); |
| 75 | |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 76 | mnl_attr_put_str(nlh, IFLA_IFNAME, argv[2]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 77 | nest = mnl_attr_nest_start(nlh, IFLA_LINKINFO); |
| 78 | mnl_attr_put_str(nlh, IFLA_INFO_KIND, "gtp"); |
| 79 | nest2 = mnl_attr_nest_start(nlh, IFLA_INFO_DATA); |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 80 | mnl_attr_put_u32(nlh, IFLA_GTP_FD0, fd1); |
| 81 | mnl_attr_put_u32(nlh, IFLA_GTP_FD1, fd2); |
Pablo Neira Ayuso | b9f6ffe | 2016-05-08 18:27:55 +0200 | [diff] [blame] | 82 | mnl_attr_put_u32(nlh, IFLA_GTP_PDP_HASHSIZE, 131072); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 83 | mnl_attr_nest_end(nlh, nest2); |
| 84 | mnl_attr_nest_end(nlh, nest); |
| 85 | |
| 86 | nl = mnl_socket_open(NETLINK_ROUTE); |
| 87 | if (nl == NULL) { |
| 88 | perror("mnl_socket_open"); |
| 89 | exit(EXIT_FAILURE); |
| 90 | } |
| 91 | |
| 92 | if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 93 | perror("mnl_socket_bind"); |
| 94 | exit(EXIT_FAILURE); |
| 95 | } |
| 96 | portid = mnl_socket_get_portid(nl); |
| 97 | |
| 98 | mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len, |
| 99 | sizeof(struct ifinfomsg)); |
| 100 | |
| 101 | if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { |
| 102 | perror("mnl_socket_send"); |
| 103 | exit(EXIT_FAILURE); |
| 104 | } |
| 105 | |
| 106 | ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 107 | if (ret == -1) { |
| 108 | perror("read"); |
| 109 | exit(EXIT_FAILURE); |
| 110 | } |
| 111 | |
| 112 | ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL); |
| 113 | if (ret == -1){ |
| 114 | perror("callback"); |
| 115 | exit(EXIT_FAILURE); |
| 116 | } |
| 117 | |
| 118 | mnl_socket_close(nl); |
| 119 | |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 120 | fprintf(stderr, "WARNING: attaching dummy socket descriptors. Keep " |
| 121 | "this process running for testing purposes.\n"); |
| 122 | pause(); |
| 123 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 124 | return 0; |
| 125 | } |