blob: 099b3172c22f71e7ad24d77ba93289fd309923c2 [file] [log] [blame]
Pablo Neira Ayuso14506662014-02-20 18:43:15 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <time.h>
5
6#include <libmnl/libmnl.h>
7#include <linux/genetlink.h>
8
9#include <libgtpnl/gtpnl.h>
10
11#include "internal.h"
12
13struct nlmsghdr *
14genl_nlmsg_build_hdr(char *buf, uint16_t type, uint16_t flags, uint32_t seq,
15 uint8_t cmd)
16{
17 struct nlmsghdr *nlh;
18 struct genlmsghdr *genl;
19
20 nlh = mnl_nlmsg_put_header(buf);
21 nlh->nlmsg_type = type;
22 nlh->nlmsg_flags = NLM_F_REQUEST | flags;
23 nlh->nlmsg_seq = seq;
24
25 genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
26 genl->cmd = cmd;
27 genl->version = 0;
28
29 return nlh;
30}
31EXPORT_SYMBOL(genl_nlmsg_build_hdr);
32
33static int genl_ctrl_validate_cb(const struct nlattr *attr, void *data)
34{
35 const struct nlattr **tb = data;
36 int type = mnl_attr_get_type(attr);
37
38 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
39 return MNL_CB_OK;
40
41 switch(type) {
42 case CTRL_ATTR_FAMILY_ID:
43 if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0) {
44 perror("mnl_attr_validate");
45 return MNL_CB_ERROR;
46 }
47 break;
48 }
49 tb[type] = attr;
50 return MNL_CB_OK;
51}
52
53static int genl_ctrl_cb(const struct nlmsghdr *nlh, void *data)
54{
55 struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
56 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
57 int32_t *genl_id = data;
58
59 mnl_attr_parse(nlh, sizeof(*genl), genl_ctrl_validate_cb, tb);
60 if (tb[CTRL_ATTR_FAMILY_ID])
61 *genl_id = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
62 else
63 *genl_id = -1;
64
65 return MNL_CB_OK;
66}
67
68struct mnl_socket *genl_socket_open(void)
69{
70 struct mnl_socket *nl;
71
72 nl = mnl_socket_open(NETLINK_GENERIC);
73 if (nl == NULL) {
74 perror("mnl_socket_open");
75 return NULL;
76 }
77
78 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
79 perror("mnl_socket_bind");
80 return NULL;
81 }
82
83 return nl;
84}
85EXPORT_SYMBOL(genl_socket_open);
86
87int genl_socket_talk(struct mnl_socket *nl, struct nlmsghdr *nlh, uint32_t seq,
88 int (*cb)(const struct nlmsghdr *nlh, void *data),
89 void *data)
90{
91 char buf[MNL_SOCKET_BUFFER_SIZE];
92 int ret;
93
94 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
95 perror("mnl_socket_send");
96 return -1;
97 }
98
99 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
100 while (ret > 0) {
101 ret = mnl_cb_run(buf, ret, seq, mnl_socket_get_portid(nl),
102 cb, data);
103 if (ret <= 0)
104 break;
105 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
106 }
107
108 return ret;
109}
110EXPORT_SYMBOL(genl_socket_talk);
111
112static struct nlmsghdr *
113genl_nlmsg_build_lookup(char *buf, const char *subsys_name)
114{
115 struct nlmsghdr *nlh;
116 struct genlmsghdr *genl;
117
118 nlh = mnl_nlmsg_put_header(buf);
119 nlh->nlmsg_type = GENL_ID_CTRL;
120 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
121 nlh->nlmsg_seq = time(NULL);
122
123 genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
124 genl->cmd = CTRL_CMD_GETFAMILY;
125 genl->version = 1;
126
127 mnl_attr_put_u32(nlh, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL);
128 mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, subsys_name);
129
130 return nlh;
131}
132
133int genl_lookup_family(struct mnl_socket *nl, const char *family)
134{
135 int32_t genl_id;
136 char buf[MNL_SOCKET_BUFFER_SIZE];
137 struct nlmsghdr *nlh = genl_nlmsg_build_lookup(buf, "gtp");
138 int err;
139
140 err = genl_socket_talk(nl, nlh, nlh->nlmsg_seq, genl_ctrl_cb, &genl_id);
141 if (err < 0)
142 return -1;
143
144 return genl_id;
145}
146EXPORT_SYMBOL(genl_lookup_family);