summaryrefslogtreecommitdiff
path: root/compat/backport-4.12.c
blob: d28edeae93b48700a4c212e5b9b99ce4dc1b5054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright 2017 Intel Deutschland GmbH
 */
#include <net/genetlink.h>
#include <net/sock.h>

enum nlmsgerr_attrs {
	NLMSGERR_ATTR_UNUSED,
	NLMSGERR_ATTR_MSG,
	NLMSGERR_ATTR_OFFS,
	NLMSGERR_ATTR_COOKIE,
	__NLMSGERR_ATTR_MAX,
	NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
};

#define NLM_F_CAPPED	0x100	/* request was capped */
#define NLM_F_ACK_TLVS	0x200	/* extended ACK TVLs were included */

struct bp_extack_genl_family {
	struct genl_family family;
	struct genl_family *real_family;
	struct list_head list;

	struct genl_ops ops[];
};

static LIST_HEAD(copies_list);
static DEFINE_MUTEX(copies_mutex);

static const struct nla_policy extack_dummy_policy[1] = {};

static struct bp_extack_genl_family *get_copy(__genl_const struct genl_ops *op)
{
	do {
		op--;
	} while (op->policy != extack_dummy_policy);

	return container_of(op, struct bp_extack_genl_family, ops[0]);
}

static int extack_pre_doit(__genl_const struct genl_ops *ops,
			   struct sk_buff *skb,
			   struct genl_info *info)
{
	struct netlink_ext_ack *extack = kzalloc(sizeof(*extack), GFP_KERNEL);
	struct bp_extack_genl_family *copy = get_copy(ops);
	struct genl_ops *real_ops;
	int err;

	__bp_genl_info_userhdr_set(info, extack);

	if (!extack) {
		__bp_genl_info_userhdr_set(info, ERR_PTR(-ENOMEM));
		return -ENOMEM;
	}

	real_ops = (void *)&copy->real_family->ops[ops - &copy->ops[1]];
	extack->__bp_genl_real_ops = real_ops;

	if (copy->real_family->pre_doit)
		err = copy->real_family->pre_doit(real_ops, skb, info);
	else
		err = 0;

	if (err) {
		__bp_genl_info_userhdr_set(info, ERR_PTR(err));
		kfree(extack);
	}

	return err;
}

static void extack_netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh,
			       int err, const struct netlink_ext_ack *extack)
{
	struct sk_buff *skb;
	struct nlmsghdr *rep;
	struct nlmsgerr *errmsg;
	size_t payload = sizeof(*errmsg);
	size_t tlvlen = 0;
	unsigned int flags = 0;
	/* backports assumes everyone supports this - libnl does so it's true */
	bool nlk_has_extack = true;

	/* Error messages get the original request appened, unless the user
	 * requests to cap the error message, and get extra error data if
	 * requested.
	 * (ignored in backports)
	 */
	if (nlk_has_extack && extack && extack->_msg)
		tlvlen += nla_total_size(strlen(extack->_msg) + 1);

	if (err) {
		if (1)
			payload += nlmsg_len(nlh);
		else
			flags |= NLM_F_CAPPED;
		if (nlk_has_extack && extack && extack->bad_attr)
			tlvlen += nla_total_size(sizeof(u32));
	} else {
		flags |= NLM_F_CAPPED;

		if (nlk_has_extack && extack && extack->cookie_len)
			tlvlen += nla_total_size(extack->cookie_len);
	}

	if (tlvlen)
		flags |= NLM_F_ACK_TLVS;

	skb = nlmsg_new(payload + tlvlen, GFP_KERNEL);
	if (!skb) {
#if LINUX_VERSION_IS_GEQ(3,10,0)
		NETLINK_CB(in_skb).sk->sk_err = ENOBUFS;
		NETLINK_CB(in_skb).sk->sk_error_report(NETLINK_CB(in_skb).sk);
#endif
		return;
	}

	rep = __nlmsg_put(skb, NETLINK_CB_PORTID(in_skb), nlh->nlmsg_seq,
			  NLMSG_ERROR, payload, flags);
	errmsg = nlmsg_data(rep);
	errmsg->error = err;
	memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));

	if (nlk_has_extack && extack) {
		if (extack->_msg) {
			WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
					       extack->_msg));
		}
		if (err) {
			if (extack->bad_attr &&
			    !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
				     (u8 *)extack->bad_attr >= in_skb->data +
							       in_skb->len))
				WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
						    (u8 *)extack->bad_attr -
						    in_skb->data));
		} else {
			if (extack->cookie_len)
				WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
						extack->cookie_len,
						extack->cookie));
		}
	}

	nlmsg_end(skb, rep);

	netlink_unicast(in_skb->sk, skb, NETLINK_CB_PORTID(in_skb),
			MSG_DONTWAIT);
}

static int extack_doit(struct sk_buff *skb, struct genl_info *info)
{
	struct genl_ops *real_ops;
	int err;

	/* older kernels have a bug here */
	if (IS_ERR(__bp_genl_info_userhdr(info))) {
		extack_netlink_ack(skb, info->nlhdr,
				   PTR_ERR(__bp_genl_info_userhdr(info)),
				   genl_info_extack(info));
		goto out;
	}

	real_ops = genl_info_extack(info)->__bp_genl_real_ops;
	err = real_ops->doit(skb, info);

	if (err == -EINTR)
		return err;

	if (info->nlhdr->nlmsg_flags & NLM_F_ACK || err)
		extack_netlink_ack(skb, info->nlhdr, err,
				   genl_info_extack(info));

out:
	/* suppress sending ACK from normal netlink code */
	info->nlhdr->nlmsg_flags &= ~NLM_F_ACK;
	return 0;
}

static void extack_post_doit(__genl_const struct genl_ops *ops,
			     struct sk_buff *skb,
			     struct genl_info *info)
{
	void (*post_doit)(__genl_const struct genl_ops *ops,
			  struct sk_buff *skb,
			  struct genl_info *info);

	post_doit = get_copy(ops)->real_family->post_doit;

	if (post_doit)
		post_doit(ops, skb, info);
	kfree(__bp_genl_info_userhdr(info));
}

int bp_extack_genl_register_family(struct genl_family *family)
{
	unsigned int size = sizeof(struct bp_extack_genl_family) +
			    sizeof(family->ops[0]) * (family->n_ops + 1);
	struct bp_extack_genl_family *copy;
	int i, err;

	copy = kzalloc(size, GFP_KERNEL);
	if (!copy)
		return -ENOMEM;

	copy->family = *family;
	copy->real_family = family;
	copy->family.ops = &copy->ops[1];

	for (i = 0; i < family->n_ops; i++) {
		copy->ops[i + 1] = family->ops[i];
		if (family->ops[i].doit)
			copy->ops[i + 1].doit = extack_doit;
	}

	copy->ops[0].policy = extack_dummy_policy;

	copy->family.pre_doit = extack_pre_doit;
	copy->family.post_doit = extack_post_doit;

	err = __real_bp_extack_genl_register_family(&copy->family);
	if (err) {
		kfree(copy);
		return err;
	}

	/* copy this since the family might access it directly */
	family->attrbuf = copy->family.attrbuf;
#if LINUX_VERSION_IS_GEQ(3,13,0)
	family->mcgrp_offset = copy->family.mcgrp_offset;
#endif

	mutex_lock(&copies_mutex);
	list_add_tail(&copy->list, &copies_list);
	mutex_unlock(&copies_mutex);

	return 0;
}
EXPORT_SYMBOL_GPL(bp_extack_genl_register_family);

int bp_extack_genl_unregister_family(struct genl_family *family)
{
	struct bp_extack_genl_family *tmp, *copy = NULL;
	int err;

	mutex_lock(&copies_mutex);
	list_for_each_entry(tmp, &copies_list, list) {
		if (tmp->real_family == family) {
			copy = tmp;
			break;
		}
	}
	if (copy)
		list_del(&copy->list);
	mutex_unlock(&copies_mutex);

	if (!copy)
		return -ENOENT;

	err = __real_bp_extack_genl_unregister_family(&copy->family);
	WARN_ON(err);
	kfree(copy);

	return 0;
}
EXPORT_SYMBOL_GPL(bp_extack_genl_unregister_family);