Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <string.h> |
| 5 | #include <time.h> |
| 6 | |
| 7 | #include <libmnl/libmnl.h> |
| 8 | #include <net/if.h> |
| 9 | #include <linux/if_link.h> |
| 10 | #include <linux/rtnetlink.h> |
| 11 | |
| 12 | #include <libgtpnl/gtpnl.h> |
| 13 | |
| 14 | #include <linux/gtp_nl.h> |
| 15 | |
| 16 | #include "internal.h" |
| 17 | |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 18 | static struct nlmsghdr * |
| 19 | gtp_put_nlmsg(char *buf, uint16_t type, uint16_t nl_flags, uint32_t seq) |
| 20 | { |
| 21 | struct nlmsghdr *nlh; |
| 22 | |
| 23 | nlh = mnl_nlmsg_put_header(buf); |
| 24 | nlh->nlmsg_type = type; |
| 25 | nlh->nlmsg_flags = NLM_F_REQUEST | nl_flags; |
| 26 | nlh->nlmsg_seq = seq; |
| 27 | |
| 28 | return nlh; |
| 29 | } |
| 30 | |
| 31 | static int gtp_dev_talk(struct nlmsghdr *nlh, uint32_t seq) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 32 | { |
| 33 | struct mnl_socket *nl; |
| 34 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 35 | int ret; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 36 | |
| 37 | nl = mnl_socket_open(NETLINK_ROUTE); |
| 38 | if (nl == NULL) { |
| 39 | perror("mnl_socket_open"); |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 44 | perror("mnl_socket_bind"); |
Pablo Neira Ayuso | 3876ef6 | 2014-02-22 22:50:00 +0100 | [diff] [blame] | 45 | goto err; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 46 | } |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 47 | |
| 48 | mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len, |
| 49 | sizeof(struct ifinfomsg)); |
| 50 | |
| 51 | if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { |
| 52 | perror("mnl_socket_send"); |
Pablo Neira Ayuso | 3876ef6 | 2014-02-22 22:50:00 +0100 | [diff] [blame] | 53 | goto err; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 57 | if (ret == -1) { |
| 58 | perror("read"); |
Pablo Neira Ayuso | 3876ef6 | 2014-02-22 22:50:00 +0100 | [diff] [blame] | 59 | goto err; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 62 | ret = mnl_cb_run(buf, ret, seq, mnl_socket_get_portid(nl), NULL, NULL); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 63 | if (ret == -1){ |
| 64 | perror("callback"); |
Pablo Neira Ayuso | 3876ef6 | 2014-02-22 22:50:00 +0100 | [diff] [blame] | 65 | goto err; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | mnl_socket_close(nl); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 69 | return 0; |
Pablo Neira Ayuso | 3876ef6 | 2014-02-22 22:50:00 +0100 | [diff] [blame] | 70 | err: |
| 71 | mnl_socket_close(nl); |
| 72 | return -1; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 73 | } |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 74 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame^] | 75 | int gtp_dev_create(const char *gtp_ifname, const char *real_ifname, |
| 76 | int fd0, int fd1) |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 77 | { |
| 78 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 79 | struct nlmsghdr *nlh; |
| 80 | struct ifinfomsg *ifm; |
| 81 | unsigned int seq = time(NULL); |
| 82 | struct nlattr *nest, *nest2; |
| 83 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame^] | 84 | nlh = gtp_put_nlmsg(buf, RTM_NEWLINK, |
| 85 | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK, seq); |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 86 | ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm)); |
| 87 | ifm->ifi_family = AF_INET; |
| 88 | ifm->ifi_change |= IFF_UP; |
| 89 | ifm->ifi_flags |= IFF_UP; |
| 90 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame^] | 91 | mnl_attr_put_u32(nlh, IFLA_LINK, if_nametoindex(real_ifname)); |
| 92 | mnl_attr_put_str(nlh, IFLA_IFNAME, gtp_ifname); |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 93 | nest = mnl_attr_nest_start(nlh, IFLA_LINKINFO); |
| 94 | mnl_attr_put_str(nlh, IFLA_INFO_KIND, "gtp"); |
| 95 | nest2 = mnl_attr_nest_start(nlh, IFLA_INFO_DATA); |
Pablo Neira Ayuso | dea76a0 | 2014-02-23 21:55:42 +0100 | [diff] [blame] | 96 | mnl_attr_put_u32(nlh, IFLA_GTP_FD0, fd0); |
| 97 | mnl_attr_put_u32(nlh, IFLA_GTP_FD1, fd1); |
Pablo Neira Ayuso | 10df059 | 2014-02-22 22:57:07 +0100 | [diff] [blame] | 98 | mnl_attr_put_u32(nlh, IFLA_GTP_HASHSIZE, 131072); |
| 99 | mnl_attr_nest_end(nlh, nest2); |
| 100 | mnl_attr_nest_end(nlh, nest); |
| 101 | |
| 102 | return gtp_dev_talk(nlh, seq); |
| 103 | } |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 104 | EXPORT_SYMBOL(gtp_dev_create); |
Pablo Neira Ayuso | 41ff538 | 2014-02-22 23:07:41 +0100 | [diff] [blame] | 105 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame^] | 106 | int gtp_dev_destroy(const char *gtp_ifname) |
Pablo Neira Ayuso | 41ff538 | 2014-02-22 23:07:41 +0100 | [diff] [blame] | 107 | { |
| 108 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 109 | struct nlmsghdr *nlh; |
| 110 | struct ifinfomsg *ifm; |
| 111 | unsigned int seq = time(NULL); |
| 112 | |
| 113 | nlh = gtp_put_nlmsg(buf, RTM_DELLINK, NLM_F_ACK, seq); |
| 114 | ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm)); |
| 115 | ifm->ifi_family = AF_INET; |
| 116 | ifm->ifi_change |= IFF_UP; |
| 117 | ifm->ifi_flags &= ~IFF_UP; |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame^] | 118 | ifm->ifi_index = if_nametoindex(gtp_ifname); |
Pablo Neira Ayuso | 41ff538 | 2014-02-22 23:07:41 +0100 | [diff] [blame] | 119 | |
| 120 | return gtp_dev_talk(nlh, seq); |
| 121 | } |
| 122 | EXPORT_SYMBOL(gtp_dev_destroy); |