Skip to content

Include NLA header on mcast groups and ops. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 9, 2023

Conversation

dylanswiggett
Copy link

The emit logic for McastGroups doesn't include the required NLA header on nested NLAs. Without this change, netlink-packet-generic emits incorrect buffers if an mcast group is included, and cannot parse the bytes it writes.

This is a nested list of NLAs, so we need to include a header on each entry
to properly parse. The new test verifies that emit and parse now both
expect the same mcast group format.
@codecov
Copy link

codecov bot commented Jun 26, 2023

Codecov Report

Merging #8 (11994fb) into main (9c9b712) will increase coverage by 22.19%.
The diff coverage is 98.83%.

@@             Coverage Diff             @@
##             main       #8       +/-   ##
===========================================
+ Coverage   34.20%   56.40%   +22.19%     
===========================================
  Files          11       11               
  Lines         573      734      +161     
===========================================
+ Hits          196      414      +218     
+ Misses        377      320       -57     
Impacted Files Coverage Δ
src/ctrl/nlas/ops.rs 90.90% <96.00%> (+67.57%) ⬆️
src/ctrl/nlas/mcast.rs 91.66% <96.15%> (+68.13%) ⬆️
src/ctrl/nlas/mod.rs 86.23% <100.00%> (+30.68%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@cathay4t cathay4t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not elegant and prone to errors when there is padding.

How about:

  • GenlCtrlAttrs::McastGroups(Vec<McastGroup>)
  • struct McastGroup { nlas: Vec<McastGrpAttrs>}
  • impl Nla for McastGroup

With this, we can have nlas.as_slice().buffer_len() and nlas.as_slice().emit(buffer) saving us from worry about headers.

Meanwhile, please fix CTRL_ATTR_OPS also.

Updates vec-based ctrl NLAs to use an inner NLA with its own header.
@dylanswiggett
Copy link
Author

Sure, pushed an updated version and fixed ops too.

Copy link
Member

@cathay4t cathay4t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kind() implementation is wrong.

Meanwhile, please also include unit test case to test the emit and parse using the binary captured by strace or nlmon.


impl Nla for McastGroup {
fn value_len(&self) -> usize {
self.nlas.iter().map(|nla| nla.buffer_len()).sum()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use self.nlas.as_slice().buffer_len()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, plus same for Op

}

fn kind(&self) -> u16 {
NLA_F_NESTED
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kind() is not NLA_F_NESTED. It is index + 1. The kernel code is:

nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
if (nla_grps == NULL)
	goto nla_put_failure;

for (i = 0; i < family->n_mcgrps; i++) {
	struct nlattr *nest;
	const struct genl_multicast_group *grp;

	grp = &family->mcgrps[i];

	nest = nla_nest_start_noflag(skb, i + 1); // <<<<<< HERE
	if (nest == NULL)
		goto nla_put_failure;

Please check BondIpAddrNlaList in netlink-packet-route as an example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for the reference. Updated based on your example.

}

fn kind(&self) -> u16 {
NLA_F_NESTED
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be index of Op. This is kernel code:

nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS);
if (nla_ops == NULL)
	goto nla_put_failure;

while (genl_op_iter_next(&i)) {
	struct nlattr *nest;
	u32 op_flags;

	op_flags = i.flags;
	if (i.doit.policy || i.dumpit.policy)
		op_flags |= GENL_CMD_CAP_HASPOL;

	nest = nla_nest_start_noflag(skb, genl_op_iter_idx(&i));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@dylanswiggett
Copy link
Author

I've addressed your comments and updated the test cases to include proper indices and padding based on strace output.

@dylanswiggett dylanswiggett changed the title Include NLA header on mcast groups. Include NLA header on mcast groups and ops. Jun 28, 2023
Copy link
Member

@cathay4t cathay4t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I will fix the lint by append a patch.

@cathay4t cathay4t merged commit 3b7ee9e into rust-netlink:main Jul 9, 2023
gnoliyil pushed a commit to gnoliyil/fuchsia that referenced this pull request Jan 27, 2024
This change is in a pull request upstream:
rust-netlink/netlink-packet-generic#8

Bug: 128857
Test: New unit test.

Change-Id: Id3ad21a119a2a77e6c272b55957e0c4df5a32c1d
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/870539
Commit-Queue: Dylan Swiggett <swiggett@google.com>
Reviewed-by: Jeff Martin <martinjeffrey@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants