blob: 311480cdf6387aceb6eff3d781066123ca0c6ac6 [file] [log] [blame]
Harald Welte51b00a62014-04-03 09:37:38 -04001/* Generic Netlink helper functions */
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
Harald Welted2bb0bc2016-07-28 20:34:45 +02009 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
Harald Welte51b00a62014-04-03 09:37:38 -040012 *
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
Harald Welted2bb0bc2016-07-28 20:34:45 +020016 * GNU Lesser General Public License for more details.
Harald Welte51b00a62014-04-03 09:37:38 -040017 *
Harald Welted2bb0bc2016-07-28 20:34:45 +020018 * You should have received a copy of the GNU Lesser General Public License
Harald Welte51b00a62014-04-03 09:37:38 -040019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010023#include <stdio.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <time.h>
27
28#include <libmnl/libmnl.h>
29#include <linux/genetlink.h>
30
31#include <libgtpnl/gtpnl.h>
32
33#include "internal.h"
34
35struct nlmsghdr *
36genl_nlmsg_build_hdr(char *buf, uint16_t type, uint16_t flags, uint32_t seq,
37 uint8_t cmd)
38{
39 struct nlmsghdr *nlh;
40 struct genlmsghdr *genl;
41
42 nlh = mnl_nlmsg_put_header(buf);
43 nlh->nlmsg_type = type;
44 nlh->nlmsg_flags = NLM_F_REQUEST | flags;
45 nlh->nlmsg_seq = seq;
46
47 genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
48 genl->cmd = cmd;
49 genl->version = 0;
50
51 return nlh;
52}
53EXPORT_SYMBOL(genl_nlmsg_build_hdr);
54
55static int genl_ctrl_validate_cb(const struct nlattr *attr, void *data)
56{
57 const struct nlattr **tb = data;
58 int type = mnl_attr_get_type(attr);
59
60 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
61 return MNL_CB_OK;
62
63 switch(type) {
64 case CTRL_ATTR_FAMILY_ID:
65 if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0) {
66 perror("mnl_attr_validate");
67 return MNL_CB_ERROR;
68 }
69 break;
70 }
71 tb[type] = attr;
72 return MNL_CB_OK;
73}
74
75static int genl_ctrl_cb(const struct nlmsghdr *nlh, void *data)
76{
77 struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
78 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
79 int32_t *genl_id = data;
80
81 mnl_attr_parse(nlh, sizeof(*genl), genl_ctrl_validate_cb, tb);
82 if (tb[CTRL_ATTR_FAMILY_ID])
83 *genl_id = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
84 else
85 *genl_id = -1;
86
87 return MNL_CB_OK;
88}
89
90struct mnl_socket *genl_socket_open(void)
91{
92 struct mnl_socket *nl;
93
94 nl = mnl_socket_open(NETLINK_GENERIC);
95 if (nl == NULL) {
96 perror("mnl_socket_open");
97 return NULL;
98 }
99
100 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
101 perror("mnl_socket_bind");
102 return NULL;
103 }
104
105 return nl;
106}
107EXPORT_SYMBOL(genl_socket_open);
108
109int genl_socket_talk(struct mnl_socket *nl, struct nlmsghdr *nlh, uint32_t seq,
110 int (*cb)(const struct nlmsghdr *nlh, void *data),
111 void *data)
112{
113 char buf[MNL_SOCKET_BUFFER_SIZE];
114 int ret;
115
116 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
117 perror("mnl_socket_send");
118 return -1;
119 }
120
121 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
122 while (ret > 0) {
123 ret = mnl_cb_run(buf, ret, seq, mnl_socket_get_portid(nl),
124 cb, data);
125 if (ret <= 0)
126 break;
127 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
128 }
129
130 return ret;
131}
132EXPORT_SYMBOL(genl_socket_talk);
133
134static struct nlmsghdr *
135genl_nlmsg_build_lookup(char *buf, const char *subsys_name)
136{
137 struct nlmsghdr *nlh;
138 struct genlmsghdr *genl;
139
140 nlh = mnl_nlmsg_put_header(buf);
141 nlh->nlmsg_type = GENL_ID_CTRL;
142 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
143 nlh->nlmsg_seq = time(NULL);
144
145 genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
146 genl->cmd = CTRL_CMD_GETFAMILY;
147 genl->version = 1;
148
149 mnl_attr_put_u32(nlh, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL);
150 mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, subsys_name);
151
152 return nlh;
153}
154
155int genl_lookup_family(struct mnl_socket *nl, const char *family)
156{
157 int32_t genl_id;
158 char buf[MNL_SOCKET_BUFFER_SIZE];
159 struct nlmsghdr *nlh = genl_nlmsg_build_lookup(buf, "gtp");
160 int err;
161
162 err = genl_socket_talk(nl, nlh, nlh->nlmsg_seq, genl_ctrl_cb, &genl_id);
163 if (err < 0)
164 return -1;
165
166 return genl_id;
167}
168EXPORT_SYMBOL(genl_lookup_family);