0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 ================
0004 Kernel Connector
0005 ================
0006
0007 Kernel connector - new netlink based userspace <-> kernel space easy
0008 to use communication module.
0009
0010 The Connector driver makes it easy to connect various agents using a
0011 netlink based network. One must register a callback and an identifier.
0012 When the driver receives a special netlink message with the appropriate
0013 identifier, the appropriate callback will be called.
0014
0015 From the userspace point of view it's quite straightforward:
0016
0017 - socket();
0018 - bind();
0019 - send();
0020 - recv();
0021
0022 But if kernelspace wants to use the full power of such connections, the
0023 driver writer must create special sockets, must know about struct sk_buff
0024 handling, etc... The Connector driver allows any kernelspace agents to use
0025 netlink based networking for inter-process communication in a significantly
0026 easier way::
0027
0028 int cn_add_callback(const struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));
0029 void cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group, int gfp_mask);
0030 void cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group, int gfp_mask);
0031
0032 struct cb_id
0033 {
0034 __u32 idx;
0035 __u32 val;
0036 };
0037
0038 idx and val are unique identifiers which must be registered in the
0039 connector.h header for in-kernel usage. `void (*callback) (void *)` is a
0040 callback function which will be called when a message with above idx.val
0041 is received by the connector core. The argument for that function must
0042 be dereferenced to `struct cn_msg *`::
0043
0044 struct cn_msg
0045 {
0046 struct cb_id id;
0047
0048 __u32 seq;
0049 __u32 ack;
0050
0051 __u16 len; /* Length of the following data */
0052 __u16 flags;
0053 __u8 data[0];
0054 };
0055
0056 Connector interfaces
0057 ====================
0058
0059 .. kernel-doc:: include/linux/connector.h
0060
0061 Note:
0062 When registering new callback user, connector core assigns
0063 netlink group to the user which is equal to its id.idx.
0064
0065 Protocol description
0066 ====================
0067
0068 The current framework offers a transport layer with fixed headers. The
0069 recommended protocol which uses such a header is as following:
0070
0071 msg->seq and msg->ack are used to determine message genealogy. When
0072 someone sends a message, they use a locally unique sequence and random
0073 acknowledge number. The sequence number may be copied into
0074 nlmsghdr->nlmsg_seq too.
0075
0076 The sequence number is incremented with each message sent.
0077
0078 If you expect a reply to the message, then the sequence number in the
0079 received message MUST be the same as in the original message, and the
0080 acknowledge number MUST be the same + 1.
0081
0082 If we receive a message and its sequence number is not equal to one we
0083 are expecting, then it is a new message. If we receive a message and
0084 its sequence number is the same as one we are expecting, but its
0085 acknowledge is not equal to the sequence number in the original
0086 message + 1, then it is a new message.
0087
0088 Obviously, the protocol header contains the above id.
0089
0090 The connector allows event notification in the following form: kernel
0091 driver or userspace process can ask connector to notify it when
0092 selected ids will be turned on or off (registered or unregistered its
0093 callback). It is done by sending a special command to the connector
0094 driver (it also registers itself with id={-1, -1}).
0095
0096 As example of this usage can be found in the cn_test.c module which
0097 uses the connector to request notification and to send messages.
0098
0099 Reliability
0100 ===========
0101
0102 Netlink itself is not a reliable protocol. That means that messages can
0103 be lost due to memory pressure or process' receiving queue overflowed,
0104 so caller is warned that it must be prepared. That is why the struct
0105 cn_msg [main connector's message header] contains u32 seq and u32 ack
0106 fields.
0107
0108 Userspace usage
0109 ===============
0110
0111 2.6.14 has a new netlink socket implementation, which by default does not
0112 allow people to send data to netlink groups other than 1.
0113 So, if you wish to use a netlink socket (for example using connector)
0114 with a different group number, the userspace application must subscribe to
0115 that group first. It can be achieved by the following pseudocode::
0116
0117 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
0118
0119 l_local.nl_family = AF_NETLINK;
0120 l_local.nl_groups = 12345;
0121 l_local.nl_pid = 0;
0122
0123 if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
0124 perror("bind");
0125 close(s);
0126 return -1;
0127 }
0128
0129 {
0130 int on = l_local.nl_groups;
0131 setsockopt(s, 270, 1, &on, sizeof(on));
0132 }
0133
0134 Where 270 above is SOL_NETLINK, and 1 is a NETLINK_ADD_MEMBERSHIP socket
0135 option. To drop a multicast subscription, one should call the above socket
0136 option with the NETLINK_DROP_MEMBERSHIP parameter which is defined as 0.
0137
0138 2.6.14 netlink code only allows to select a group which is less or equal to
0139 the maximum group number, which is used at netlink_kernel_create() time.
0140 In case of connector it is CN_NETLINK_USERS + 0xf, so if you want to use
0141 group number 12345, you must increment CN_NETLINK_USERS to that number.
0142 Additional 0xf numbers are allocated to be used by non-in-kernel users.
0143
0144 Due to this limitation, group 0xffffffff does not work now, so one can
0145 not use add/remove connector's group notifications, but as far as I know,
0146 only cn_test.c test module used it.
0147
0148 Some work in netlink area is still being done, so things can be changed in
0149 2.6.15 timeframe, if it will happen, documentation will be updated for that
0150 kernel.
0151
0152 Code samples
0153 ============
0154
0155 Sample code for a connector test module and user space can be found
0156 in samples/connector/. To build this code, enable CONFIG_CONNECTOR
0157 and CONFIG_SAMPLES.