blob: 5d8b9af9a43d8ebd66a2733a9cf0b5bb2da07b87 [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
Harald Welte4e3960c2017-11-08 16:04:00 +0900109void genl_socket_close(struct mnl_socket *nl)
110{
111 mnl_socket_close(nl);
112}
113EXPORT_SYMBOL(genl_socket_close);
114
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100115int genl_socket_talk(struct mnl_socket *nl, struct nlmsghdr *nlh, uint32_t seq,
116 int (*cb)(const struct nlmsghdr *nlh, void *data),
117 void *data)
118{
119 char buf[MNL_SOCKET_BUFFER_SIZE];
120 int ret;
121
122 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
123 perror("mnl_socket_send");
124 return -1;
125 }
126
127 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
128 while (ret > 0) {
129 ret = mnl_cb_run(buf, ret, seq, mnl_socket_get_portid(nl),
130 cb, data);
131 if (ret <= 0)
132 break;
133 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
134 }
135
136 return ret;
137}
138EXPORT_SYMBOL(genl_socket_talk);
139
140static struct nlmsghdr *
141genl_nlmsg_build_lookup(char *buf, const char *subsys_name)
142{
143 struct nlmsghdr *nlh;
144 struct genlmsghdr *genl;
145
146 nlh = mnl_nlmsg_put_header(buf);
147 nlh->nlmsg_type = GENL_ID_CTRL;
148 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
149 nlh->nlmsg_seq = time(NULL);
150
151 genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
152 genl->cmd = CTRL_CMD_GETFAMILY;
153 genl->version = 1;
154
Harald Weltef86d9b42018-04-26 21:46:04 +0200155 mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100156 mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, subsys_name);
157
158 return nlh;
159}
160
161int genl_lookup_family(struct mnl_socket *nl, const char *family)
162{
163 int32_t genl_id;
164 char buf[MNL_SOCKET_BUFFER_SIZE];
165 struct nlmsghdr *nlh = genl_nlmsg_build_lookup(buf, "gtp");
166 int err;
167
168 err = genl_socket_talk(nl, nlh, nlh->nlmsg_seq, genl_ctrl_cb, &genl_id);
169 if (err < 0)
170 return -1;
171
172 return genl_id;
173}
174EXPORT_SYMBOL(genl_lookup_family);