Page MenuHomeFreeBSD

linprocfs(5): Add /proc/net/route
ClosedPublic

Authored by john.grafton_runbox.com on Oct 12 2022, 2:43 PM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Jan 24, 2:51 PM
Unknown Object (File)
Tue, Jan 17, 2:22 AM
Unknown Object (File)
Fri, Jan 6, 6:48 PM
Unknown Object (File)
Fri, Jan 6, 2:24 PM
Unknown Object (File)
Jan 1 2023, 2:48 AM
Unknown Object (File)
Dec 31 2022, 1:01 AM
Unknown Object (File)
Dec 30 2022, 12:03 PM
Unknown Object (File)
Dec 20 2022, 2:26 AM
Subscribers

Details

Summary

Some Linux programs require it to exist before installation.

Test Plan

Load the new linprocfs module, mount it and cat {mountpoint}/proc/net/route.

The content should be very similar to Linux proc/net/route.

I've tested this module in both 14.0-CURRENT and 13.1-RELEASE with success.

Verified with GitHub pre-commit checks here: https://github.com/freebsd/freebsd-src/pull/616

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

dchagin added inline comments.
sys/compat/linprocfs/linprocfs.c
1605

after malloc?

1611

please, move decl before code

1625

malloc in case of M_WAITOK can't return NULL

1627

error = kern_sysctl
if (error != 0)

goto out;
1646

out before free()

1651

return (error);

sys/compat/linprocfs/linprocfs.c
1608

btw, Is that enough? rtsock messages can be up to 64k

john.grafton_runbox.com marked 7 inline comments as done.

Updated source from dchagin comments

Ideally, I'd like to only malloc only the amount of memory needed for the sysctl results. This works in user sysctl by passing a NULL to oldp but the kernel_sysctl function doesn't appear to work the same way.

looks ok to me, Alex, could you take a look at network part of code?

melifaro added inline comments.
sys/compat/linprocfs/linprocfs.c
1614

I’d rather use ‘rib_walk(fibnum, af, linux_route_print)’ public KPI instead of using it indirectly via sysctl.

This revision now requires changes to proceed.Oct 17 2022, 11:41 AM
sys/compat/linprocfs/linprocfs.c
1614

Ah, thank you. rib_walk was my first inclination but I wasn't sure if it'd return the information I needed. netstat(1) uses sysctl and thus provided an example on how to decode the results.

sys/compat/linprocfs/linprocfs.c
1614

It does.

You'll get 'struct rtentry' in the iterator, which allows extracting prefix data via
rt_get_inet_prefix_pmask().
Nexthop is slightly more tricky, as there can be multiple paths attached to a single prefix, so you need something like sysctl_dumpentry() code.
Finally, you'll get the nexthop pointer, where you can easily get ifp (nh_ifp), mtu (nh_mtu), and gateway (gw_sa). Note that the gateway is only relevant if (nh_flags & NHF_GATEWAY) is true. Also, the gateway can be IPv6 for 4over6 routes.

Thanks again for the reviews. The rib_walk function is indeed much better than a kernel sysctl! Not sure if Linux outputs multipath routes in proc/net/route, I'll take a look at its source.

Thank you for providing the updated version w/ rib_walk!
Looks good to me, please see some minor comments inline. Once addressed, it should be good to land.

sys/compat/linprocfs/linprocfs.c
94

You shouldn’t include it, that’s a private header for the routing code.

1557

Probably worth using ‘struct in_addr’ here

1575

Re multipath: to be safe probably worth using nhop_select(raw_nhop, 0) to ensure it doesnt crash with multipath routes

1578
1587

I’d suggest defining 0xF explicitely as a flag combination

1588

You shouldn’t access rtentry fields directly, use rt_get_weight().

1605

Worth using c11 initialisers:

1620

You don’t need to hold ifnet lock, but you need to be in network epoch (NET_EPOCH_ENTER())

john.grafton_runbox.com added inline comments.
sys/compat/linprocfs/linprocfs.c
1620

Without the ifnet lock, linux_ifname() panics due to IFNET_RLOCK_ASSERT()

panic: Lock (sx) ifnet_sx not locked

@dchagin: want to commit it, or should I do do it?

sys/compat/linprocfs/linprocfs.c
1557

Nit: any reason not to have dst and mask as stack variables in linux_route_print() ?

1592
This revision is now accepted and ready to land.Oct 19 2022, 7:32 PM
john.grafton_runbox.com added inline comments.
sys/compat/linprocfs/linprocfs.c
1557

No good reason. :)

john.grafton_runbox.com marked an inline comment as done.
  • dst and mask as stack variables
  • simpler flag calculation
This revision now requires review to proceed.Oct 19 2022, 10:10 PM

@dchagin: want to commit it, or should I do do it?

@melifaro, as you say, I will build universe in the night, can commit it.

This revision was not accepted when it landed; it landed in state Needs Review.Oct 22 2022, 10:54 AM
This revision was automatically updated to reflect the committed changes.