When sending a netlink message, I guess both nl_pid
in sockaddr_nl
structure and nlmsg_pid
in nlmsghdr
structure are used to show the origin of message. Isn't it redundant? Or it has another purpose?
2 Answers
According to netlink man page: "Both nlmsg_seq and nlmsg_pid are opaque to netlink core." I guess these two are just for user to track messages and acknoledments. still I'm wondering why this is not handled just with nlmsg_seq and what is the case which nlmsg_pid would be useful?

- 49
- 4
When sending a netlink message, I guess both nl_pid in sockaddr_nl structure and nlmsg_pid in nlmsghdr structure are used to show the origin of message
Partially you're right. It's indeed represents the origin of message in both cases.
But not only when sending to kernel - also when the netlink engine handling netlink requests internally.
Isn't it redundant?
It is naive to think that so many clever people (Linux kernel developers) have done something stupid and left something unnecessary in the kernel.
Netlink is an API. So there is an API which could be used through the manipulations with netlink messages. Message is represented via struct nlmsghdr
. It's an API - just use it.
You will see some examples of using this API if you'll grep in kernel sources such a way:
grep -rnI nlmsg_pid
(it's used e.g. from within infiniband and xfrm subsystems).
But struct sockaddr_nl
is used for internal representation of netlink address (for netlink socket's dst_portid
). It's just for its own purposes inside af_netlink. Obviously "netlink address" should has "Port ID".

- 4,840
- 3
- 17
- 44
-
Dear @red0ct, Thanks for your explanation. Of course linux developers are so clever:). I was just looking for the reason and difference between nl_pid and nlmsg_pid. Though, in all examples I have seen, these two are equal for user messages and are zero for kernel messages. I am wondering is there a case where these two are different? – shahriar Feb 27 '21 at 07:06
-
1There are a number of mistakes in the Linux APIs. It is naive to think that just because the Linux developers are clever they don't make mistakes. – jch Feb 28 '21 at 17:13