Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netlink: allow extack cookie also for error messages

Commit ba0dc5f6e0ba ("netlink: allow sending extended ACK with cookie on
success") introduced a cookie which can be sent to userspace as part of
extended ack message in the form of NLMSGERR_ATTR_COOKIE attribute.
Currently the cookie is ignored if error code is non-zero but there is
no technical reason for such limitation and it can be useful to provide
machine parseable information as part of an error message.

Include NLMSGERR_ATTR_COOKIE whenever the cookie has been set,
regardless of error code.

Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Michal Kubecek and committed by
David S. Miller
fe2a31d7 ef299cc3

+17 -26
+17 -26
net/netlink/af_netlink.c
··· 2392 2392 if (nlk_has_extack && extack && extack->_msg) 2393 2393 tlvlen += nla_total_size(strlen(extack->_msg) + 1); 2394 2394 2395 - if (err) { 2396 - if (!(nlk->flags & NETLINK_F_CAP_ACK)) 2397 - payload += nlmsg_len(nlh); 2398 - else 2399 - flags |= NLM_F_CAPPED; 2400 - if (nlk_has_extack && extack && extack->bad_attr) 2401 - tlvlen += nla_total_size(sizeof(u32)); 2402 - } else { 2395 + if (err && !(nlk->flags & NETLINK_F_CAP_ACK)) 2396 + payload += nlmsg_len(nlh); 2397 + else 2403 2398 flags |= NLM_F_CAPPED; 2404 - 2405 - if (nlk_has_extack && extack && extack->cookie_len) 2406 - tlvlen += nla_total_size(extack->cookie_len); 2407 - } 2399 + if (err && nlk_has_extack && extack && extack->bad_attr) 2400 + tlvlen += nla_total_size(sizeof(u32)); 2401 + if (nlk_has_extack && extack && extack->cookie_len) 2402 + tlvlen += nla_total_size(extack->cookie_len); 2408 2403 2409 2404 if (tlvlen) 2410 2405 flags |= NLM_F_ACK_TLVS; ··· 2422 2427 WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG, 2423 2428 extack->_msg)); 2424 2429 } 2425 - if (err) { 2426 - if (extack->bad_attr && 2427 - !WARN_ON((u8 *)extack->bad_attr < in_skb->data || 2428 - (u8 *)extack->bad_attr >= in_skb->data + 2429 - in_skb->len)) 2430 - WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS, 2431 - (u8 *)extack->bad_attr - 2432 - (u8 *)nlh)); 2433 - } else { 2434 - if (extack->cookie_len) 2435 - WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE, 2436 - extack->cookie_len, 2437 - extack->cookie)); 2438 - } 2430 + if (err && extack->bad_attr && 2431 + !WARN_ON((u8 *)extack->bad_attr < in_skb->data || 2432 + (u8 *)extack->bad_attr >= in_skb->data + 2433 + in_skb->len)) 2434 + WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS, 2435 + (u8 *)extack->bad_attr - 2436 + (u8 *)nlh)); 2437 + if (extack->cookie_len) 2438 + WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE, 2439 + extack->cookie_len, extack->cookie)); 2439 2440 } 2440 2441 2441 2442 nlmsg_end(skb, rep);