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 |
| 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 <linux/if.h> |
| 31 | #include <linux/if_link.h> |
| 32 | #include <linux/rtnetlink.h> |
| 33 | |
| 34 | #include <linux/gtp_nl.h> |
| 35 | |
| 36 | int main(int argc, char *argv[]) |
| 37 | { |
| 38 | struct mnl_socket *nl; |
| 39 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 40 | struct nlmsghdr *nlh; |
| 41 | struct ifinfomsg *ifm; |
| 42 | int ret; |
| 43 | unsigned int seq, portid, change = 0, flags = 0; |
| 44 | struct nlattr *nest, *nest2; |
| 45 | |
Andreas Schultz | af422a4 | 2016-04-11 16:10:02 +0200 | [diff] [blame] | 46 | if (argc != 1) { |
| 47 | printf("Usage: %s\n", argv[0]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 48 | exit(EXIT_FAILURE); |
| 49 | } |
| 50 | |
| 51 | nlh = mnl_nlmsg_put_header(buf); |
| 52 | nlh->nlmsg_type = RTM_NEWLINK; |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 53 | 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] | 54 | nlh->nlmsg_seq = seq = time(NULL); |
| 55 | ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm)); |
| 56 | ifm->ifi_family = AF_INET; |
| 57 | ifm->ifi_change |= IFF_UP; |
| 58 | ifm->ifi_flags |= IFF_UP; |
| 59 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 60 | fprintf(stderr, "WARNING: attaching dummy socket descriptors. Use " |
Pablo Neira Ayuso | 3a7dd0a | 2014-03-18 18:24:40 +0100 | [diff] [blame] | 61 | "this command for testing purposes only.\n"); |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 62 | int fd1 = socket(AF_INET, SOCK_DGRAM, 0); |
| 63 | int fd2 = socket(AF_INET, SOCK_DGRAM, 0); |
| 64 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 65 | mnl_attr_put_str(nlh, IFLA_IFNAME, "gtp0"); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 66 | nest = mnl_attr_nest_start(nlh, IFLA_LINKINFO); |
| 67 | mnl_attr_put_str(nlh, IFLA_INFO_KIND, "gtp"); |
| 68 | nest2 = mnl_attr_nest_start(nlh, IFLA_INFO_DATA); |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 69 | mnl_attr_put_u32(nlh, IFLA_GTP_FD0, fd1); |
| 70 | mnl_attr_put_u32(nlh, IFLA_GTP_FD1, fd2); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 71 | mnl_attr_put_u32(nlh, IFLA_GTP_HASHSIZE, 131072); |
| 72 | mnl_attr_nest_end(nlh, nest2); |
| 73 | mnl_attr_nest_end(nlh, nest); |
| 74 | |
| 75 | nl = mnl_socket_open(NETLINK_ROUTE); |
| 76 | if (nl == NULL) { |
| 77 | perror("mnl_socket_open"); |
| 78 | exit(EXIT_FAILURE); |
| 79 | } |
| 80 | |
| 81 | if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 82 | perror("mnl_socket_bind"); |
| 83 | exit(EXIT_FAILURE); |
| 84 | } |
| 85 | portid = mnl_socket_get_portid(nl); |
| 86 | |
| 87 | mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len, |
| 88 | sizeof(struct ifinfomsg)); |
| 89 | |
| 90 | if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { |
| 91 | perror("mnl_socket_send"); |
| 92 | exit(EXIT_FAILURE); |
| 93 | } |
| 94 | |
| 95 | ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 96 | if (ret == -1) { |
| 97 | perror("read"); |
| 98 | exit(EXIT_FAILURE); |
| 99 | } |
| 100 | |
| 101 | ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL); |
| 102 | if (ret == -1){ |
| 103 | perror("callback"); |
| 104 | exit(EXIT_FAILURE); |
| 105 | } |
| 106 | |
| 107 | mnl_socket_close(nl); |
| 108 | |
| 109 | return 0; |
| 110 | } |