1*4dc78e53SAndroid Build Coastguard Worker//// 2*4dc78e53SAndroid Build Coastguard Worker vim.syntax: asciidoc 3*4dc78e53SAndroid Build Coastguard Worker 4*4dc78e53SAndroid Build Coastguard Worker Copyright (c) 2011 Thomas Graf <[email protected]> 5*4dc78e53SAndroid Build Coastguard Worker//// 6*4dc78e53SAndroid Build Coastguard Worker 7*4dc78e53SAndroid Build Coastguard WorkerNetlink Library (libnl) 8*4dc78e53SAndroid Build Coastguard Worker======================= 9*4dc78e53SAndroid Build Coastguard WorkerThomas Graf <[email protected]> 10*4dc78e53SAndroid Build Coastguard Worker3.2, May 9 2011: 11*4dc78e53SAndroid Build Coastguard Worker:numbered: 12*4dc78e53SAndroid Build Coastguard Worker 13*4dc78e53SAndroid Build Coastguard Worker== Introduction 14*4dc78e53SAndroid Build Coastguard Worker 15*4dc78e53SAndroid Build Coastguard WorkerThe core library contains the fundamentals required to communicate 16*4dc78e53SAndroid Build Coastguard Workerover netlink sockets. It deals with connecting and disconnecting of 17*4dc78e53SAndroid Build Coastguard Workersockets, sending and receiving of data, construction and parsing of 18*4dc78e53SAndroid Build Coastguard Workermessages, provides a customizable receiving state machine, and 19*4dc78e53SAndroid Build Coastguard Workerprovides an abstract data type framework which eases the implementation 20*4dc78e53SAndroid Build Coastguard Workerof object based netlink protocols where objects are added, removed, or 21*4dc78e53SAndroid Build Coastguard Workermodified using a netlink based protocol. 22*4dc78e53SAndroid Build Coastguard Worker 23*4dc78e53SAndroid Build Coastguard Worker.Library Hierarchy 24*4dc78e53SAndroid Build Coastguard Worker 25*4dc78e53SAndroid Build Coastguard WorkerThe suite is split into multiple libraries: 26*4dc78e53SAndroid Build Coastguard Worker 27*4dc78e53SAndroid Build Coastguard Workerimage:library_overview.png["Library Hierarchy"] 28*4dc78e53SAndroid Build Coastguard Worker 29*4dc78e53SAndroid Build Coastguard Workerlink:core.html[Netlink Library] (libnl):: 30*4dc78e53SAndroid Build Coastguard WorkerSocket handling, sending and receiving, message construction and parsing, ... 31*4dc78e53SAndroid Build Coastguard Worker 32*4dc78e53SAndroid Build Coastguard Workerlink:route.html[Routing Family Library] (libnl-route):: 33*4dc78e53SAndroid Build Coastguard WorkerAdresses, links, neighbours, routing, traffic control, neighbour tables, ... 34*4dc78e53SAndroid Build Coastguard Worker 35*4dc78e53SAndroid Build Coastguard WorkerNetfilter Library (libnl-nf):: 36*4dc78e53SAndroid Build Coastguard WorkerConnection tracking, logging, queueing 37*4dc78e53SAndroid Build Coastguard Worker 38*4dc78e53SAndroid Build Coastguard WorkerGeneric Netlink Library (libnl-genl):: 39*4dc78e53SAndroid Build Coastguard WorkerController API, family and command registration 40*4dc78e53SAndroid Build Coastguard Worker 41*4dc78e53SAndroid Build Coastguard Worker 42*4dc78e53SAndroid Build Coastguard Worker=== How To Read This Documentation 43*4dc78e53SAndroid Build Coastguard Worker 44*4dc78e53SAndroid Build Coastguard WorkerThe libraries provide a broad set of APIs of which most applications only 45*4dc78e53SAndroid Build Coastguard Workerrequire a small subset of it. Depending on the type of application, some 46*4dc78e53SAndroid Build Coastguard Workerusers may only be interested in the low level netlink messaging API while 47*4dc78e53SAndroid Build Coastguard Workerothers wish to make heavy use of the high level API. 48*4dc78e53SAndroid Build Coastguard Worker 49*4dc78e53SAndroid Build Coastguard WorkerIn any case it is recommended to get familiar with the netlink protocol 50*4dc78e53SAndroid Build Coastguard Workerfirst. 51*4dc78e53SAndroid Build Coastguard Worker 52*4dc78e53SAndroid Build Coastguard Worker- <<core_netlink_fundamentals>> 53*4dc78e53SAndroid Build Coastguard Worker 54*4dc78e53SAndroid Build Coastguard WorkerThe low level APIs are described in: 55*4dc78e53SAndroid Build Coastguard Worker 56*4dc78e53SAndroid Build Coastguard Worker- <<core_sockets>> 57*4dc78e53SAndroid Build Coastguard Worker- <<core_send_recv>> 58*4dc78e53SAndroid Build Coastguard Worker 59*4dc78e53SAndroid Build Coastguard Worker 60*4dc78e53SAndroid Build Coastguard Worker=== Linking to this Library 61*4dc78e53SAndroid Build Coastguard Worker 62*4dc78e53SAndroid Build Coastguard Worker.Checking the presence of the library using autoconf 63*4dc78e53SAndroid Build Coastguard Worker 64*4dc78e53SAndroid Build Coastguard WorkerProjects using autoconf may use +PKG_CHECK_MODULES()+ to check if 65*4dc78e53SAndroid Build Coastguard Workera specific version of libnl is available on the system. The example 66*4dc78e53SAndroid Build Coastguard Workerbelow also shows how to retrieve the +CFLAGS+ and linking dependencies 67*4dc78e53SAndroid Build Coastguard Workerrequired to link against the library. 68*4dc78e53SAndroid Build Coastguard Worker 69*4dc78e53SAndroid Build Coastguard WorkerThe following example shows how to check for a specific version of libnl. If 70*4dc78e53SAndroid Build Coastguard Workerfound, it extends the `CFLAGS` and `LIBS` variable appropriately: 71*4dc78e53SAndroid Build Coastguard Worker 72*4dc78e53SAndroid Build Coastguard Worker[source] 73*4dc78e53SAndroid Build Coastguard Worker---- 74*4dc78e53SAndroid Build Coastguard WorkerPKG_CHECK_MODULES(LIBNL3, libnl-3.0 >= 3.1, [have_libnl3=yes], [have_libnl3=no]) 75*4dc78e53SAndroid Build Coastguard Workerif (test "${have_libnl3}" = "yes"); then 76*4dc78e53SAndroid Build Coastguard Worker CFLAGS+="$LIBNL3_CFLAGS" 77*4dc78e53SAndroid Build Coastguard Worker LIBS+="$LIBNL3_LIBS" 78*4dc78e53SAndroid Build Coastguard Workerfi 79*4dc78e53SAndroid Build Coastguard Worker---- 80*4dc78e53SAndroid Build Coastguard Worker 81*4dc78e53SAndroid Build Coastguard WorkerNOTE: The pkgconfig file is named +libnl-3.0.pc+ for historic reasons, it also 82*4dc78e53SAndroid Build Coastguard Worker covers library versions >= 3.1. 83*4dc78e53SAndroid Build Coastguard Worker 84*4dc78e53SAndroid Build Coastguard Worker.Header Files 85*4dc78e53SAndroid Build Coastguard Worker 86*4dc78e53SAndroid Build Coastguard WorkerThe main header file is `<netlink/netlink.h>`. Additional headers may need to 87*4dc78e53SAndroid Build Coastguard Workerbe included in your sources depending on the subsystems and components your 88*4dc78e53SAndroid Build Coastguard Workerprogram makes use of. 89*4dc78e53SAndroid Build Coastguard Worker 90*4dc78e53SAndroid Build Coastguard Worker[source,c] 91*4dc78e53SAndroid Build Coastguard Worker----- 92*4dc78e53SAndroid Build Coastguard Worker#include <netlink/netlink.h> 93*4dc78e53SAndroid Build Coastguard Worker#include <netlink/cache.h> 94*4dc78e53SAndroid Build Coastguard Worker#include <netlink/route/link.h> 95*4dc78e53SAndroid Build Coastguard Worker----- 96*4dc78e53SAndroid Build Coastguard Worker 97*4dc78e53SAndroid Build Coastguard Worker.Version Dependent Code 98*4dc78e53SAndroid Build Coastguard Worker 99*4dc78e53SAndroid Build Coastguard WorkerIf your code wishes to be capable to link against multiple versions of libnl 100*4dc78e53SAndroid Build Coastguard Workeryou may have direct the compiler to only include portions on the code depending 101*4dc78e53SAndroid Build Coastguard Workeron the version of libnl that it is compiled against. 102*4dc78e53SAndroid Build Coastguard Worker 103*4dc78e53SAndroid Build Coastguard Worker[source,c] 104*4dc78e53SAndroid Build Coastguard Worker----- 105*4dc78e53SAndroid Build Coastguard Worker#include <netlink/version.h> 106*4dc78e53SAndroid Build Coastguard Worker 107*4dc78e53SAndroid Build Coastguard Worker#if LIBNL_VER_NUM >= LIBNL_VER(3,1) 108*4dc78e53SAndroid Build Coastguard Worker /* include code if compiled with libnl version >= 3.1 */ 109*4dc78e53SAndroid Build Coastguard Worker#endif 110*4dc78e53SAndroid Build Coastguard Worker----- 111*4dc78e53SAndroid Build Coastguard Worker 112*4dc78e53SAndroid Build Coastguard Worker.Linking 113*4dc78e53SAndroid Build Coastguard Worker----- 114*4dc78e53SAndroid Build Coastguard Worker$ gcc myprogram.c -o myprogram $(pkgconfig --cflags --libs libnl-3.0) 115*4dc78e53SAndroid Build Coastguard Worker----- 116*4dc78e53SAndroid Build Coastguard Worker 117*4dc78e53SAndroid Build Coastguard Worker=== Debugging 118*4dc78e53SAndroid Build Coastguard Worker 119*4dc78e53SAndroid Build Coastguard WorkerThe library has been compiled with debugging statements enabled it will 120*4dc78e53SAndroid Build Coastguard Workerprint debug information to +stderr+ if the environment variable +NLDBG+ 121*4dc78e53SAndroid Build Coastguard Workeris set to > 0. 122*4dc78e53SAndroid Build Coastguard Worker 123*4dc78e53SAndroid Build Coastguard Worker----- 124*4dc78e53SAndroid Build Coastguard Worker$ NLDBG=2 ./myprogram 125*4dc78e53SAndroid Build Coastguard Worker----- 126*4dc78e53SAndroid Build Coastguard Worker 127*4dc78e53SAndroid Build Coastguard Worker.Debugging Levels 128*4dc78e53SAndroid Build Coastguard Worker[options="header", width="80%", cols="1,5", align="center"] 129*4dc78e53SAndroid Build Coastguard Worker|=============================================================== 130*4dc78e53SAndroid Build Coastguard Worker| Level | Description 131*4dc78e53SAndroid Build Coastguard Worker| 0 | Debugging disabled (default) 132*4dc78e53SAndroid Build Coastguard Worker| 1 | Warnings, important events and notifications 133*4dc78e53SAndroid Build Coastguard Worker| 2 | More or less important debugging messages 134*4dc78e53SAndroid Build Coastguard Worker| 3 | Repetitive events causing a flood of debugging messages 135*4dc78e53SAndroid Build Coastguard Worker| 4 | Even less important messages 136*4dc78e53SAndroid Build Coastguard Worker|=============================================================== 137*4dc78e53SAndroid Build Coastguard Worker 138*4dc78e53SAndroid Build Coastguard Worker.Debugging the Netlink Protocol 139*4dc78e53SAndroid Build Coastguard Worker 140*4dc78e53SAndroid Build Coastguard WorkerIt is often useful to peek into the stream of netlink messages exchanged 141*4dc78e53SAndroid Build Coastguard Workerwith other sockets. Setting the environment variable +NLCB=debug+ will 142*4dc78e53SAndroid Build Coastguard Workercause the debugging message handlers to be used which in turn print the 143*4dc78e53SAndroid Build Coastguard Workernetlink messages exchanged in a human readable format to to +stderr+: 144*4dc78e53SAndroid Build Coastguard Worker 145*4dc78e53SAndroid Build Coastguard Worker----- 146*4dc78e53SAndroid Build Coastguard Worker$ NLCB=debug ./myprogram 147*4dc78e53SAndroid Build Coastguard Worker-- Debug: Sent Message: 148*4dc78e53SAndroid Build Coastguard Worker-------------------------- BEGIN NETLINK MESSAGE --------------------------- 149*4dc78e53SAndroid Build Coastguard Worker [HEADER] 16 octets 150*4dc78e53SAndroid Build Coastguard Worker .nlmsg_len = 20 151*4dc78e53SAndroid Build Coastguard Worker .nlmsg_type = 18 <route/link::get> 152*4dc78e53SAndroid Build Coastguard Worker .nlmsg_flags = 773 <REQUEST,ACK,ROOT,MATCH> 153*4dc78e53SAndroid Build Coastguard Worker .nlmsg_seq = 1301410712 154*4dc78e53SAndroid Build Coastguard Worker .nlmsg_pid = 20014 155*4dc78e53SAndroid Build Coastguard Worker [PAYLOAD] 16 octets 156*4dc78e53SAndroid Build Coastguard Worker 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 157*4dc78e53SAndroid Build Coastguard Worker--------------------------- END NETLINK MESSAGE --------------------------- 158*4dc78e53SAndroid Build Coastguard Worker-- Debug: Received Message: 159*4dc78e53SAndroid Build Coastguard Worker-------------------------- BEGIN NETLINK MESSAGE --------------------------- 160*4dc78e53SAndroid Build Coastguard Worker [HEADER] 16 octets 161*4dc78e53SAndroid Build Coastguard Worker .nlmsg_len = 996 162*4dc78e53SAndroid Build Coastguard Worker .nlmsg_type = 16 <route/link::new> 163*4dc78e53SAndroid Build Coastguard Worker .nlmsg_flags = 2 <MULTI> 164*4dc78e53SAndroid Build Coastguard Worker .nlmsg_seq = 1301410712 165*4dc78e53SAndroid Build Coastguard Worker .nlmsg_pid = 20014 166*4dc78e53SAndroid Build Coastguard Worker [PAYLOAD] 16 octets 167*4dc78e53SAndroid Build Coastguard Worker 00 00 04 03 01 00 00 00 49 00 01 00 00 00 00 00 ........I....... 168*4dc78e53SAndroid Build Coastguard Worker [ATTR 03] 3 octets 169*4dc78e53SAndroid Build Coastguard Worker 6c 6f 00 lo. 170*4dc78e53SAndroid Build Coastguard Worker [PADDING] 1 octets 171*4dc78e53SAndroid Build Coastguard Worker 00 . 172*4dc78e53SAndroid Build Coastguard Worker [ATTR 13] 4 octets 173*4dc78e53SAndroid Build Coastguard Worker 00 00 00 00 .... 174*4dc78e53SAndroid Build Coastguard Worker [ATTR 16] 1 octets 175*4dc78e53SAndroid Build Coastguard Worker 00 . 176*4dc78e53SAndroid Build Coastguard Worker [PADDING] 3 octets 177*4dc78e53SAndroid Build Coastguard Worker 00 00 00 ... 178*4dc78e53SAndroid Build Coastguard Worker [ATTR 17] 1 octets 179*4dc78e53SAndroid Build Coastguard Worker 00 . 180*4dc78e53SAndroid Build Coastguard Worker [...] 181*4dc78e53SAndroid Build Coastguard Worker--------------------------- END NETLINK MESSAGE --------------------------- 182*4dc78e53SAndroid Build Coastguard Worker 183*4dc78e53SAndroid Build Coastguard Worker----- 184*4dc78e53SAndroid Build Coastguard Worker 185*4dc78e53SAndroid Build Coastguard Worker[[core_netlink_fundamentals]] 186*4dc78e53SAndroid Build Coastguard Worker== Netlink Protocol Fundamentals 187*4dc78e53SAndroid Build Coastguard Worker 188*4dc78e53SAndroid Build Coastguard WorkerThe netlink protocol is a socket based IPC mechanism used for 189*4dc78e53SAndroid Build Coastguard Workercommunication between userspace processes and the kernel or between 190*4dc78e53SAndroid Build Coastguard Workeruserspace processes themselves. The netlink protocol is based on BSD 191*4dc78e53SAndroid Build Coastguard Workersockets and uses the +AF_NETLINK+ address family. Every netlink 192*4dc78e53SAndroid Build Coastguard Workerprotocol uses its own protocol number (e.g. +NETLINK_ROUTE+, 193*4dc78e53SAndroid Build Coastguard Worker+NETLINK_NETFILTER+, etc). Its addressing schema is based on a 32 bit 194*4dc78e53SAndroid Build Coastguard Workerport number, formerly referred to as PID, which uniquely identifies 195*4dc78e53SAndroid Build Coastguard Workereach peer. 196*4dc78e53SAndroid Build Coastguard Worker 197*4dc78e53SAndroid Build Coastguard Worker[[core_addressing]] 198*4dc78e53SAndroid Build Coastguard Worker=== Addressing 199*4dc78e53SAndroid Build Coastguard Worker 200*4dc78e53SAndroid Build Coastguard WorkerThe netlink address (port) consists of a 32bit integer. Port 0 (zero) 201*4dc78e53SAndroid Build Coastguard Workeris reserved for the kernel and refers to the kernel side socket of each 202*4dc78e53SAndroid Build Coastguard Workernetlink protocol family. Other port numbers usually refer to user space 203*4dc78e53SAndroid Build Coastguard Workerowned sockets, although this is not enforced. 204*4dc78e53SAndroid Build Coastguard Worker 205*4dc78e53SAndroid Build Coastguard WorkerNOTE: In the beginning, it was common practice to use the process 206*4dc78e53SAndroid Build Coastguard Worker identifier (PID) as the local port number. This became unpractical 207*4dc78e53SAndroid Build Coastguard Worker with the introduction of threaded netlink applications and 208*4dc78e53SAndroid Build Coastguard Worker applications requiring multiple sockets. Therefore libnl generates 209*4dc78e53SAndroid Build Coastguard Worker unique port numbers based on the process identifier and adds an 210*4dc78e53SAndroid Build Coastguard Worker offset to it allowing for multiple sockets to be used. The initial 211*4dc78e53SAndroid Build Coastguard Worker socket will still equal to the process identifier for backwards 212*4dc78e53SAndroid Build Coastguard Worker compatibility reasons. 213*4dc78e53SAndroid Build Coastguard Worker 214*4dc78e53SAndroid Build Coastguard Workerimage:addressing.png["Addressing Example"] 215*4dc78e53SAndroid Build Coastguard Worker 216*4dc78e53SAndroid Build Coastguard WorkerThe above figure illustrates three applications and the kernel side 217*4dc78e53SAndroid Build Coastguard Workerexposing two kernel side sockets. It shows the common netlink use 218*4dc78e53SAndroid Build Coastguard Workercases: 219*4dc78e53SAndroid Build Coastguard Worker 220*4dc78e53SAndroid Build Coastguard Worker * User space to kernel 221*4dc78e53SAndroid Build Coastguard Worker * User space to user space 222*4dc78e53SAndroid Build Coastguard Worker * Listening to kernel multicast notifications 223*4dc78e53SAndroid Build Coastguard Worker 224*4dc78e53SAndroid Build Coastguard Worker.User Space to Kernel 225*4dc78e53SAndroid Build Coastguard Worker 226*4dc78e53SAndroid Build Coastguard WorkerThe most common form of netlink usage is for a user space application 227*4dc78e53SAndroid Build Coastguard Workerto send requests to the kernel and process the reply which is either 228*4dc78e53SAndroid Build Coastguard Workeran error message or a success notification. 229*4dc78e53SAndroid Build Coastguard Worker 230*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 231*4dc78e53SAndroid Build Coastguard Worker-------- 232*4dc78e53SAndroid Build Coastguard Workermsc { 233*4dc78e53SAndroid Build Coastguard Worker App1,App2,Kernel; 234*4dc78e53SAndroid Build Coastguard Worker App1=>Kernel [label="request (src=11, dst=0)"]; 235*4dc78e53SAndroid Build Coastguard Worker App1<=Kernel [label="reply (src=0, dst=11)"]; 236*4dc78e53SAndroid Build Coastguard Worker ...; 237*4dc78e53SAndroid Build Coastguard Worker App2=>Kernel [label="request (src=21, dst=0)"]; 238*4dc78e53SAndroid Build Coastguard Worker App2<=Kernel [label="reply (src=0, dst=21)"]; 239*4dc78e53SAndroid Build Coastguard Worker} 240*4dc78e53SAndroid Build Coastguard Worker-------- 241*4dc78e53SAndroid Build Coastguard Worker 242*4dc78e53SAndroid Build Coastguard Worker.User Space to User Space 243*4dc78e53SAndroid Build Coastguard Worker 244*4dc78e53SAndroid Build Coastguard WorkerNetlink may also be used as an IPC mechanism to communicate between user 245*4dc78e53SAndroid Build Coastguard Workerspace applications directly. Communication is not limited to two peers, 246*4dc78e53SAndroid Build Coastguard Workerany number of peers may communicate with each other and multicasting 247*4dc78e53SAndroid Build Coastguard Workercapabilities allow to reach multiple peers with a single message. 248*4dc78e53SAndroid Build Coastguard Worker 249*4dc78e53SAndroid Build Coastguard WorkerIn order for the sockets to be visible to each other, both sockets must 250*4dc78e53SAndroid Build Coastguard Workerbe created for the same netlink protocol family. 251*4dc78e53SAndroid Build Coastguard Worker 252*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 253*4dc78e53SAndroid Build Coastguard Worker-------- 254*4dc78e53SAndroid Build Coastguard Workermsc { 255*4dc78e53SAndroid Build Coastguard Worker App2,App3; 256*4dc78e53SAndroid Build Coastguard Worker App2=>App3 [label="request (src=22, dst=31)"]; 257*4dc78e53SAndroid Build Coastguard Worker App2<=App3 [label="reply (src=31, dst=22)"]; 258*4dc78e53SAndroid Build Coastguard Worker ...; 259*4dc78e53SAndroid Build Coastguard Worker} 260*4dc78e53SAndroid Build Coastguard Worker-------- 261*4dc78e53SAndroid Build Coastguard Worker 262*4dc78e53SAndroid Build Coastguard Worker.User space listening to kernel notifications 263*4dc78e53SAndroid Build Coastguard Worker 264*4dc78e53SAndroid Build Coastguard WorkerThis form of netlink communication is typically found in user space 265*4dc78e53SAndroid Build Coastguard Workerdaemons that need to act on certain kernel events. Such daemons will 266*4dc78e53SAndroid Build Coastguard Workertypically maintain a netlink socket subscribed to a multicast group that 267*4dc78e53SAndroid Build Coastguard Workeris used by the kernel to notify interested user space parties about 268*4dc78e53SAndroid Build Coastguard Workerspecific events. 269*4dc78e53SAndroid Build Coastguard Worker 270*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 271*4dc78e53SAndroid Build Coastguard Worker-------- 272*4dc78e53SAndroid Build Coastguard Workermsc { 273*4dc78e53SAndroid Build Coastguard Worker Kernel,App3; 274*4dc78e53SAndroid Build Coastguard Worker Kernel=>App3 [label="notification (src=0, group=foo)"]; 275*4dc78e53SAndroid Build Coastguard Worker ...; 276*4dc78e53SAndroid Build Coastguard Worker} 277*4dc78e53SAndroid Build Coastguard Worker-------- 278*4dc78e53SAndroid Build Coastguard Worker 279*4dc78e53SAndroid Build Coastguard WorkerUse of multicasting is preferred over direct addressing due to the 280*4dc78e53SAndroid Build Coastguard Workerflexibility in exchanging the user space component at any time without 281*4dc78e53SAndroid Build Coastguard Workerthe kernel noticing. 282*4dc78e53SAndroid Build Coastguard Worker 283*4dc78e53SAndroid Build Coastguard Worker[[core_msg_format]] 284*4dc78e53SAndroid Build Coastguard Worker=== Message Format 285*4dc78e53SAndroid Build Coastguard Worker 286*4dc78e53SAndroid Build Coastguard WorkerA netlink protocol is typically based on messages and consists of the 287*4dc78e53SAndroid Build Coastguard Workernetlink message header (+struct nlmsghdr+) plus the payload attached 288*4dc78e53SAndroid Build Coastguard Workerto it. The payload can consist of arbitrary data but usually contains 289*4dc78e53SAndroid Build Coastguard Workera fixed size protocol specific header followed by a stream of 290*4dc78e53SAndroid Build Coastguard Workerattributes. 291*4dc78e53SAndroid Build Coastguard Worker 292*4dc78e53SAndroid Build Coastguard Worker.Netlink message header (struct nlmsghdr) 293*4dc78e53SAndroid Build Coastguard Worker 294*4dc78e53SAndroid Build Coastguard Workerimage:nlmsghdr.png[align="center", alt="Netlink Message Header"] 295*4dc78e53SAndroid Build Coastguard Worker 296*4dc78e53SAndroid Build Coastguard WorkerTotal Length (32bit):: 297*4dc78e53SAndroid Build Coastguard WorkerTotal length of the message in bytes including the netlink message header. 298*4dc78e53SAndroid Build Coastguard Worker 299*4dc78e53SAndroid Build Coastguard WorkerMessage Type (16bit):: 300*4dc78e53SAndroid Build Coastguard WorkerThe message type specifies the type of payload the message is carrying. 301*4dc78e53SAndroid Build Coastguard WorkerSeveral standard message types are defined by the netlink protocol. 302*4dc78e53SAndroid Build Coastguard WorkerAdditional message types may be defined by each protocol family. See 303*4dc78e53SAndroid Build Coastguard Worker<<core_msg_types>> for additional information. 304*4dc78e53SAndroid Build Coastguard Worker 305*4dc78e53SAndroid Build Coastguard WorkerMessage Flags (16bit):: 306*4dc78e53SAndroid Build Coastguard WorkerThe message flags may be used to modify the behaviour of a message type. 307*4dc78e53SAndroid Build Coastguard WorkerSee section <<core_msg_flags>> for a list of standard message flags. 308*4dc78e53SAndroid Build Coastguard Worker 309*4dc78e53SAndroid Build Coastguard WorkerSequence Number (32bit):: 310*4dc78e53SAndroid Build Coastguard WorkerThe sequence number is optional and may be used to allow referring to 311*4dc78e53SAndroid Build Coastguard Workera previous message, e.g. an error message can refer to the original 312*4dc78e53SAndroid Build Coastguard Workerrequest causing the error. 313*4dc78e53SAndroid Build Coastguard Worker 314*4dc78e53SAndroid Build Coastguard WorkerPort Number (32bit):: 315*4dc78e53SAndroid Build Coastguard WorkerThe port number specifies the peer to which the message should be delivered 316*4dc78e53SAndroid Build Coastguard Workerto. If not specified, the message will be delivered to the first matching 317*4dc78e53SAndroid Build Coastguard Workerkernel side socket of the same protocol family. 318*4dc78e53SAndroid Build Coastguard Worker 319*4dc78e53SAndroid Build Coastguard Worker[[core_msg_types]] 320*4dc78e53SAndroid Build Coastguard Worker=== Message Types 321*4dc78e53SAndroid Build Coastguard Worker 322*4dc78e53SAndroid Build Coastguard WorkerNetlink differs between requests, notifications, and replies. Requests 323*4dc78e53SAndroid Build Coastguard Workerare messages which have the +NLM_F_REQUEST+ flag set and are meant to 324*4dc78e53SAndroid Build Coastguard Workerrequest an action from the receiver. A request is typically sent from 325*4dc78e53SAndroid Build Coastguard Workera userspace process to the kernel. While not strictly enforced, requests 326*4dc78e53SAndroid Build Coastguard Workershould carry a sequence number incremented for each request sent. 327*4dc78e53SAndroid Build Coastguard Worker 328*4dc78e53SAndroid Build Coastguard WorkerDepending on the nature of the request, the receiver may reply to the 329*4dc78e53SAndroid Build Coastguard Workerrequest with another netlink message. The sequence number of a reply 330*4dc78e53SAndroid Build Coastguard Workermust match the sequence number of the request it relates to. 331*4dc78e53SAndroid Build Coastguard Worker 332*4dc78e53SAndroid Build Coastguard WorkerNotifications are of informal nature and no reply is expected, therefore 333*4dc78e53SAndroid Build Coastguard Workerthe sequence number is typically set to 0. 334*4dc78e53SAndroid Build Coastguard Worker 335*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 336*4dc78e53SAndroid Build Coastguard Worker-------- 337*4dc78e53SAndroid Build Coastguard Workermsc { 338*4dc78e53SAndroid Build Coastguard Worker A,B; 339*4dc78e53SAndroid Build Coastguard Worker A=>B [label="GET (seq=1, NLM_F_REQUEST)"]; 340*4dc78e53SAndroid Build Coastguard Worker A<=B [label="PUT (seq=1)"]; 341*4dc78e53SAndroid Build Coastguard Worker ...; 342*4dc78e53SAndroid Build Coastguard Worker A<=B [label="NOTIFY (seq=0)"]; 343*4dc78e53SAndroid Build Coastguard Worker} 344*4dc78e53SAndroid Build Coastguard Worker-------- 345*4dc78e53SAndroid Build Coastguard Worker 346*4dc78e53SAndroid Build Coastguard Worker 347*4dc78e53SAndroid Build Coastguard WorkerThe type of message is primarly identified by its 16 bit message type set 348*4dc78e53SAndroid Build Coastguard Workerin the message header. The following standard message types are defined: 349*4dc78e53SAndroid Build Coastguard Worker 350*4dc78e53SAndroid Build Coastguard Worker- +NLMSG_NOOP+ - No operation, message must be discarded 351*4dc78e53SAndroid Build Coastguard Worker- +NLMSG_ERROR+ - Error message or ACK, see <<core_errmsg>> 352*4dc78e53SAndroid Build Coastguard Worker respectively <<core_msg_ack>> 353*4dc78e53SAndroid Build Coastguard Worker- +NLMSG_DONE+ - End of multipart sequence, see <<core_multipart>> 354*4dc78e53SAndroid Build Coastguard Worker- +NLMSG_OVERRUN+ - Overrun notification (Error) 355*4dc78e53SAndroid Build Coastguard Worker 356*4dc78e53SAndroid Build Coastguard WorkerEvery netlink protocol is free to define own message types. Note that 357*4dc78e53SAndroid Build Coastguard Workermessage type values +< NLMSG_MIN_TYPE (0x10)+ are reserved and may 358*4dc78e53SAndroid Build Coastguard Workernot be used. 359*4dc78e53SAndroid Build Coastguard Worker 360*4dc78e53SAndroid Build Coastguard WorkerIt is common practice to use own message types to implement RPC schemas. 361*4dc78e53SAndroid Build Coastguard WorkerSuppose the goal of the netlink protocol you are implementing is allow 362*4dc78e53SAndroid Build Coastguard Workerconfiguration of a particular network device, therefore you want to 363*4dc78e53SAndroid Build Coastguard Workerprovide read/write access to various configuration options. The typical 364*4dc78e53SAndroid Build Coastguard Worker"netlink way" of doing this would be to define two message types 365*4dc78e53SAndroid Build Coastguard Worker+MSG_SETCFG+, +MSG_GETCFG+: 366*4dc78e53SAndroid Build Coastguard Worker 367*4dc78e53SAndroid Build Coastguard Worker[source,c] 368*4dc78e53SAndroid Build Coastguard Worker-------- 369*4dc78e53SAndroid Build Coastguard Worker#define MSG_SETCFG 0x11 370*4dc78e53SAndroid Build Coastguard Worker#define MSG_GETCFG 0x12 371*4dc78e53SAndroid Build Coastguard Worker-------- 372*4dc78e53SAndroid Build Coastguard Worker 373*4dc78e53SAndroid Build Coastguard WorkerSending a +MSG_GETCFG+ request message will typically trigger a reply 374*4dc78e53SAndroid Build Coastguard Workerwith the message type +MSG_SETCFG+ containing the current configuration. 375*4dc78e53SAndroid Build Coastguard WorkerIn object oriented terms one would describe this as "the kernel sets 376*4dc78e53SAndroid Build Coastguard Workerthe local copy of the configuration in userspace". 377*4dc78e53SAndroid Build Coastguard Worker 378*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 379*4dc78e53SAndroid Build Coastguard Worker-------- 380*4dc78e53SAndroid Build Coastguard Workermsc { 381*4dc78e53SAndroid Build Coastguard Worker A,B; 382*4dc78e53SAndroid Build Coastguard Worker A=>B [label="MSG_GETCFG (seq=1, NLM_F_REQUEST)"]; 383*4dc78e53SAndroid Build Coastguard Worker A<=B [label="MSG_SETCFG (seq=1)"]; 384*4dc78e53SAndroid Build Coastguard Worker} 385*4dc78e53SAndroid Build Coastguard Worker-------- 386*4dc78e53SAndroid Build Coastguard Worker 387*4dc78e53SAndroid Build Coastguard WorkerThe configuration may be changed by sending a +MSG_SETCFG+ which will 388*4dc78e53SAndroid Build Coastguard Workerbe responded to with either an ACK (see <<core_msg_ack>>) 389*4dc78e53SAndroid Build Coastguard Workeror an error message (see <<core_errmsg>>). 390*4dc78e53SAndroid Build Coastguard Worker 391*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 392*4dc78e53SAndroid Build Coastguard Worker-------- 393*4dc78e53SAndroid Build Coastguard Workermsc { 394*4dc78e53SAndroid Build Coastguard Worker A,B; 395*4dc78e53SAndroid Build Coastguard Worker A=>B [label="MSG_SETCFG (seq=1, NLM_F_REQUEST, NLM_F_ACK)"]; 396*4dc78e53SAndroid Build Coastguard Worker A<=B [label="ACK (seq=1)"]; 397*4dc78e53SAndroid Build Coastguard Worker} 398*4dc78e53SAndroid Build Coastguard Worker-------- 399*4dc78e53SAndroid Build Coastguard Worker 400*4dc78e53SAndroid Build Coastguard WorkerOptionally, the kernel may send out notifications for configuration 401*4dc78e53SAndroid Build Coastguard Workerchanges allowing userspace to listen for changes instead of polling 402*4dc78e53SAndroid Build Coastguard Workerfrequently. Notifications typically reuse an existing message type 403*4dc78e53SAndroid Build Coastguard Workerand rely on the application using a separate socket to differ between 404*4dc78e53SAndroid Build Coastguard Workerrequests and notifications but you may also specify a separate message 405*4dc78e53SAndroid Build Coastguard Workertype. 406*4dc78e53SAndroid Build Coastguard Worker 407*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 408*4dc78e53SAndroid Build Coastguard Worker-------- 409*4dc78e53SAndroid Build Coastguard Workermsc { 410*4dc78e53SAndroid Build Coastguard Worker A,B; 411*4dc78e53SAndroid Build Coastguard Worker A<=B [label="MSG_SETCFG (seq=0)"]; 412*4dc78e53SAndroid Build Coastguard Worker} 413*4dc78e53SAndroid Build Coastguard Worker-------- 414*4dc78e53SAndroid Build Coastguard Worker 415*4dc78e53SAndroid Build Coastguard Worker[[core_multipart]] 416*4dc78e53SAndroid Build Coastguard Worker==== Multipart Messages 417*4dc78e53SAndroid Build Coastguard Worker 418*4dc78e53SAndroid Build Coastguard WorkerAlthough in theory a netlink message can be up to 4GiB in size. The socket 419*4dc78e53SAndroid Build Coastguard Workerbuffers are very likely not large enough to hold message of such sizes. 420*4dc78e53SAndroid Build Coastguard WorkerTherefore it is common to limit messages to one page size (PAGE_SIZE) and 421*4dc78e53SAndroid Build Coastguard Workeruse the multipart mechanism to split large pieces of data into several 422*4dc78e53SAndroid Build Coastguard Workermessages. A multipart message has the flag +NLM_F_MULTI+ set and the 423*4dc78e53SAndroid Build Coastguard Workerreceiver is expected to continue receiving and parsing until the special 424*4dc78e53SAndroid Build Coastguard Workermessage type +NLMSG_DONE+ is received. 425*4dc78e53SAndroid Build Coastguard Worker 426*4dc78e53SAndroid Build Coastguard WorkerMultipart messages unlike fragmented ip packets must not be reassembled 427*4dc78e53SAndroid Build Coastguard Workereven though it is perfectly legal to do so if the protocols wishes to 428*4dc78e53SAndroid Build Coastguard Workerwork this way. Often multipart message are used to send lists or trees 429*4dc78e53SAndroid Build Coastguard Workerof objects were each multipart message simply carries multiple objects 430*4dc78e53SAndroid Build Coastguard Workerallow for each message to be parsed independently. 431*4dc78e53SAndroid Build Coastguard Worker 432*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 433*4dc78e53SAndroid Build Coastguard Worker-------- 434*4dc78e53SAndroid Build Coastguard Workermsc { 435*4dc78e53SAndroid Build Coastguard Worker A,B; 436*4dc78e53SAndroid Build Coastguard Worker A=>B [label="GET (seq=1, NLM_F_REQUEST)"]; 437*4dc78e53SAndroid Build Coastguard Worker A<=B [label="PUT (seq=1, NLM_F_MULTI)"]; 438*4dc78e53SAndroid Build Coastguard Worker ...; 439*4dc78e53SAndroid Build Coastguard Worker A<=B [label="PUT (seq=1, NLM_F_MULTI)"]; 440*4dc78e53SAndroid Build Coastguard Worker A<=B [label="NLMSG_DONE (seq=1)"]; 441*4dc78e53SAndroid Build Coastguard Worker} 442*4dc78e53SAndroid Build Coastguard Worker-------- 443*4dc78e53SAndroid Build Coastguard Worker 444*4dc78e53SAndroid Build Coastguard Worker[[core_errmsg]] 445*4dc78e53SAndroid Build Coastguard Worker==== Error Message 446*4dc78e53SAndroid Build Coastguard Worker 447*4dc78e53SAndroid Build Coastguard WorkerError messages can be sent in response to a request. Error messages must 448*4dc78e53SAndroid Build Coastguard Workeruse the standard message type +NLMSG_ERROR+. The payload consists of a 449*4dc78e53SAndroid Build Coastguard Workererror code and the original netlink mesage header of the request. 450*4dc78e53SAndroid Build Coastguard Worker 451*4dc78e53SAndroid Build Coastguard Workerimage:nlmsgerr.png["Netlink Errror Message header"] 452*4dc78e53SAndroid Build Coastguard Worker 453*4dc78e53SAndroid Build Coastguard WorkerError messages should set the sequence number to the sequence number 454*4dc78e53SAndroid Build Coastguard Workerof the request which caused the error. 455*4dc78e53SAndroid Build Coastguard Worker 456*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 457*4dc78e53SAndroid Build Coastguard Worker-------- 458*4dc78e53SAndroid Build Coastguard Workermsc { 459*4dc78e53SAndroid Build Coastguard Worker A,B; 460*4dc78e53SAndroid Build Coastguard Worker A=>B [label="GET (seq=1, NLM_F_REQUEST)"]; 461*4dc78e53SAndroid Build Coastguard Worker A<=B [label="NLMSG_ERROR code=EINVAL (seq=1)"]; 462*4dc78e53SAndroid Build Coastguard Worker} 463*4dc78e53SAndroid Build Coastguard Worker-------- 464*4dc78e53SAndroid Build Coastguard Worker 465*4dc78e53SAndroid Build Coastguard Worker[[core_msg_ack]] 466*4dc78e53SAndroid Build Coastguard Worker==== ACKs 467*4dc78e53SAndroid Build Coastguard Worker 468*4dc78e53SAndroid Build Coastguard WorkerA sender can request an ACK message to be sent back for each request 469*4dc78e53SAndroid Build Coastguard Workerprocessed by setting the +NLM_F_ACK+ flag in the request. This is typically 470*4dc78e53SAndroid Build Coastguard Workerused to allow the sender to synchronize further processing until the 471*4dc78e53SAndroid Build Coastguard Workerrequest has been processed by the receiver. 472*4dc78e53SAndroid Build Coastguard Worker 473*4dc78e53SAndroid Build Coastguard Worker["mscgen"] 474*4dc78e53SAndroid Build Coastguard Worker-------- 475*4dc78e53SAndroid Build Coastguard Workermsc { 476*4dc78e53SAndroid Build Coastguard Worker A,B; 477*4dc78e53SAndroid Build Coastguard Worker A=>B [label="GET (seq=1, NLM_F_REQUEST | NLM_F_ACK)"]; 478*4dc78e53SAndroid Build Coastguard Worker A<=B [label="ACK (seq=1)"]; 479*4dc78e53SAndroid Build Coastguard Worker} 480*4dc78e53SAndroid Build Coastguard Worker-------- 481*4dc78e53SAndroid Build Coastguard Worker 482*4dc78e53SAndroid Build Coastguard WorkerACK messages also use the message type +NLMSG_ERROR+ and payload 483*4dc78e53SAndroid Build Coastguard Workerformat but the error code is set to 0. 484*4dc78e53SAndroid Build Coastguard Worker 485*4dc78e53SAndroid Build Coastguard Worker[[core_msg_flags]] 486*4dc78e53SAndroid Build Coastguard Worker==== Message Flags 487*4dc78e53SAndroid Build Coastguard Worker 488*4dc78e53SAndroid Build Coastguard WorkerThe following standard flags are defined 489*4dc78e53SAndroid Build Coastguard Worker 490*4dc78e53SAndroid Build Coastguard Worker[source,c] 491*4dc78e53SAndroid Build Coastguard Worker-------- 492*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_REQUEST 1 493*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_MULTI 2 494*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_ACK 4 495*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_ECHO 8 496*4dc78e53SAndroid Build Coastguard Worker-------- 497*4dc78e53SAndroid Build Coastguard Worker 498*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_REQUEST` - Message is a request, see <<core_msg_types>>. 499*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_MULTI` - Multipart message, see <<core_multipart>> 500*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_ACK` - ACK message requested, see <<core_msg_ack>>. 501*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_ECHO` - Request to echo the request. 502*4dc78e53SAndroid Build Coastguard Worker 503*4dc78e53SAndroid Build Coastguard WorkerThe flag +NLM_F_ECHO+ is similar to the `NLM_F_ACK` flag. It can be 504*4dc78e53SAndroid Build Coastguard Workerused in combination with `NLM_F_REQUEST` and causes a notification 505*4dc78e53SAndroid Build Coastguard Workerwhich is sent as a result of a request to also be sent to the sender 506*4dc78e53SAndroid Build Coastguard Workerregardless of whether the sender has subscribed to the corresponding 507*4dc78e53SAndroid Build Coastguard Workermulticast group or not. See <<core_multicast>> 508*4dc78e53SAndroid Build Coastguard Worker 509*4dc78e53SAndroid Build Coastguard WorkerAdditional universal message flags are defined which only apply for 510*4dc78e53SAndroid Build Coastguard Worker+GET+ requests: 511*4dc78e53SAndroid Build Coastguard Worker 512*4dc78e53SAndroid Build Coastguard Worker[source,c] 513*4dc78e53SAndroid Build Coastguard Worker-------- 514*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_ROOT 0x100 515*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_MATCH 0x200 516*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_ATOMIC 0x400 517*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_DUMP (NLM_F_ROOT|NLM_F_MATCH) 518*4dc78e53SAndroid Build Coastguard Worker-------- 519*4dc78e53SAndroid Build Coastguard Worker 520*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_ROOT` - Return based on root of tree. 521*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_MATCH` - Return all matching entries. 522*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_ATOMIC` - Obsoleted, once used to request an atomic operation. 523*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_DUMP` - Return a list of all objects 524*4dc78e53SAndroid Build Coastguard Worker (`NLM_F_ROOT`|`NLM_F_MATCH`). 525*4dc78e53SAndroid Build Coastguard Worker 526*4dc78e53SAndroid Build Coastguard WorkerUse of these flags is completely optional and many netlink protocols only 527*4dc78e53SAndroid Build Coastguard Workermake use of the `NLM_F_DUMP` flag which typically requests the receiver 528*4dc78e53SAndroid Build Coastguard Workerto send a list of all objects in the context of the message type as a 529*4dc78e53SAndroid Build Coastguard Workersequence of multipart messages (see <<core_multipart>>). 530*4dc78e53SAndroid Build Coastguard Worker 531*4dc78e53SAndroid Build Coastguard WorkerAnother set of flags exist related to `NEW` or `SET` requests. These 532*4dc78e53SAndroid Build Coastguard Workerflags are mutually exclusive to the `GET` flags: 533*4dc78e53SAndroid Build Coastguard Worker 534*4dc78e53SAndroid Build Coastguard Worker[source,c] 535*4dc78e53SAndroid Build Coastguard Worker-------- 536*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_REPLACE 0x100 537*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_EXCL 0x200 538*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_CREATE 0x400 539*4dc78e53SAndroid Build Coastguard Worker#define NLM_F_APPEND 0x800 540*4dc78e53SAndroid Build Coastguard Worker-------- 541*4dc78e53SAndroid Build Coastguard Worker 542*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_REPLACE` - Replace an existing object if it exists. 543*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_EXCL` - Do not update object if it exists already. 544*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_CREATE` - Create object if it does not exist yet. 545*4dc78e53SAndroid Build Coastguard Worker- `NLM_F_APPEND` - Add object at end of list. 546*4dc78e53SAndroid Build Coastguard Worker 547*4dc78e53SAndroid Build Coastguard WorkerBehaviour of these flags may differ slightly between different netlink 548*4dc78e53SAndroid Build Coastguard Workerprotocols. 549*4dc78e53SAndroid Build Coastguard Worker 550*4dc78e53SAndroid Build Coastguard Worker[[core_seq_num]] 551*4dc78e53SAndroid Build Coastguard Worker=== Sequence Numbers 552*4dc78e53SAndroid Build Coastguard Worker 553*4dc78e53SAndroid Build Coastguard WorkerNetlink allows the use of sequence numbers to help relate replies to 554*4dc78e53SAndroid Build Coastguard Workerrequests. It should be noted that unlike in protocols such as TCP 555*4dc78e53SAndroid Build Coastguard Workerthere is no strict enforcement of the sequence number. The sole purpose 556*4dc78e53SAndroid Build Coastguard Workerof sequence numbers is to assist a sender in relating replies to the 557*4dc78e53SAndroid Build Coastguard Workercorresponding requests. See <<core_msg_types>> for more information. 558*4dc78e53SAndroid Build Coastguard Worker 559*4dc78e53SAndroid Build Coastguard WorkerSequence numbers are managed on a per socket basis, see 560*4dc78e53SAndroid Build Coastguard Worker<<core_sk_seq_num>> for more information on how to use sequence numbers. 561*4dc78e53SAndroid Build Coastguard Worker 562*4dc78e53SAndroid Build Coastguard Worker[[core_multicast]] 563*4dc78e53SAndroid Build Coastguard Worker=== Multicast Groups 564*4dc78e53SAndroid Build Coastguard Worker 565*4dc78e53SAndroid Build Coastguard WorkerTODO 566*4dc78e53SAndroid Build Coastguard Worker 567*4dc78e53SAndroid Build Coastguard WorkerSee <<core_sk_multicast>> 568*4dc78e53SAndroid Build Coastguard Worker 569*4dc78e53SAndroid Build Coastguard Worker[[core_sockets]] 570*4dc78e53SAndroid Build Coastguard Worker== Netlink Sockets 571*4dc78e53SAndroid Build Coastguard Worker 572*4dc78e53SAndroid Build Coastguard WorkerIn order to use the netlink protocol, a netlink socket is required. 573*4dc78e53SAndroid Build Coastguard WorkerEach socket defines an independent context for sending and receiving of 574*4dc78e53SAndroid Build Coastguard Workermessages. An application may make use multiple sockets, e.g. a socket to 575*4dc78e53SAndroid Build Coastguard Workersend requests and receive the replies and another socket subscribed to a 576*4dc78e53SAndroid Build Coastguard Workermulticast group to receive notifications. 577*4dc78e53SAndroid Build Coastguard Worker 578*4dc78e53SAndroid Build Coastguard Worker=== Socket structure (struct nl_sock) 579*4dc78e53SAndroid Build Coastguard Worker 580*4dc78e53SAndroid Build Coastguard WorkerThe netlink socket and all related attributes including the actual file 581*4dc78e53SAndroid Build Coastguard Workerdescriptor are represented by +struct nl_sock+. 582*4dc78e53SAndroid Build Coastguard Worker 583*4dc78e53SAndroid Build Coastguard Worker[source,c] 584*4dc78e53SAndroid Build Coastguard Worker-------- 585*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 586*4dc78e53SAndroid Build Coastguard Worker 587*4dc78e53SAndroid Build Coastguard Workerstruct nl_sock *nl_socket_alloc(void) 588*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_free(struct nl_sock *sk) 589*4dc78e53SAndroid Build Coastguard Worker-------- 590*4dc78e53SAndroid Build Coastguard Worker 591*4dc78e53SAndroid Build Coastguard WorkerThe application must allocate an instance of +struct nl_sock+ for each 592*4dc78e53SAndroid Build Coastguard Workernetlink socket it wishes to use. 593*4dc78e53SAndroid Build Coastguard Worker 594*4dc78e53SAndroid Build Coastguard Worker[[core_sk_seq_num]] 595*4dc78e53SAndroid Build Coastguard Worker=== Sequence Numbers 596*4dc78e53SAndroid Build Coastguard Worker 597*4dc78e53SAndroid Build Coastguard WorkerThe library will automatically take care of sequence number handling 598*4dc78e53SAndroid Build Coastguard Workerfor the application. A sequence number counter is stored in the 599*4dc78e53SAndroid Build Coastguard Workersocket structure which is used and incremented automatically when a 600*4dc78e53SAndroid Build Coastguard Workermessage needs to be sent which is expected to generate a reply such as 601*4dc78e53SAndroid Build Coastguard Workeran error or any other message type that needs to be related to the 602*4dc78e53SAndroid Build Coastguard Workeroriginal message. 603*4dc78e53SAndroid Build Coastguard Worker 604*4dc78e53SAndroid Build Coastguard WorkerAlternatively, the counter can be used directly via the function 605*4dc78e53SAndroid Build Coastguard Workernl_socket_use_seq(). It will return the current value of the counter 606*4dc78e53SAndroid Build Coastguard Workerand increment it by one afterwards. 607*4dc78e53SAndroid Build Coastguard Worker 608*4dc78e53SAndroid Build Coastguard Worker[source,c] 609*4dc78e53SAndroid Build Coastguard Worker-------- 610*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 611*4dc78e53SAndroid Build Coastguard Worker 612*4dc78e53SAndroid Build Coastguard Workerunsigned int nl_socket_use_seq(struct nl_sock *sk); 613*4dc78e53SAndroid Build Coastguard Worker-------- 614*4dc78e53SAndroid Build Coastguard Worker 615*4dc78e53SAndroid Build Coastguard WorkerMost applications will not want to deal with sequence number handling 616*4dc78e53SAndroid Build Coastguard Workerthemselves though. When using nl_send_auto() the sequence number is 617*4dc78e53SAndroid Build Coastguard Workerfilled in automatically and matched again when a reply is received. See 618*4dc78e53SAndroid Build Coastguard Workersection <<core_send_recv>> for more information. 619*4dc78e53SAndroid Build Coastguard Worker 620*4dc78e53SAndroid Build Coastguard WorkerThis behaviour can and must be disabled if the netlink protocol 621*4dc78e53SAndroid Build Coastguard Workerimplemented does not use a request/reply model, e.g. when a socket is 622*4dc78e53SAndroid Build Coastguard Workerused to receive notification messages. 623*4dc78e53SAndroid Build Coastguard Worker 624*4dc78e53SAndroid Build Coastguard Worker[source,c] 625*4dc78e53SAndroid Build Coastguard Worker-------- 626*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 627*4dc78e53SAndroid Build Coastguard Worker 628*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_disable_seq_check(struct nl_sock *sk); 629*4dc78e53SAndroid Build Coastguard Worker-------- 630*4dc78e53SAndroid Build Coastguard Worker 631*4dc78e53SAndroid Build Coastguard WorkerFor more information on the theory behind netlink sequence numbers, 632*4dc78e53SAndroid Build Coastguard Workersee section <<core_seq_num>>. 633*4dc78e53SAndroid Build Coastguard Worker 634*4dc78e53SAndroid Build Coastguard Worker[[core_sk_multicast]] 635*4dc78e53SAndroid Build Coastguard Worker=== Multicast Group Subscriptions 636*4dc78e53SAndroid Build Coastguard Worker 637*4dc78e53SAndroid Build Coastguard WorkerEach socket can subscribe to any number of multicast groups of the 638*4dc78e53SAndroid Build Coastguard Workernetlink protocol it is connected to. The socket will then receive a 639*4dc78e53SAndroid Build Coastguard Workercopy of each message sent to any of the groups. Multicast groups are 640*4dc78e53SAndroid Build Coastguard Workercommonly used to implement event notifications. 641*4dc78e53SAndroid Build Coastguard Worker 642*4dc78e53SAndroid Build Coastguard WorkerPrior to kernel 2.6.14 the group subscription was performed using a 643*4dc78e53SAndroid Build Coastguard Workerbitmask which limited the number of groups per protocol family to 32. 644*4dc78e53SAndroid Build Coastguard WorkerThis outdated interface can still be accessed via the function 645*4dc78e53SAndroid Build Coastguard Workernl_join_groups() even though it is not recommended for new code. 646*4dc78e53SAndroid Build Coastguard Worker 647*4dc78e53SAndroid Build Coastguard Worker[source,c] 648*4dc78e53SAndroid Build Coastguard Worker-------- 649*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 650*4dc78e53SAndroid Build Coastguard Worker 651*4dc78e53SAndroid Build Coastguard Workervoid nl_join_groups(struct nl_sock *sk, int bitmask); 652*4dc78e53SAndroid Build Coastguard Worker-------- 653*4dc78e53SAndroid Build Coastguard Worker 654*4dc78e53SAndroid Build Coastguard WorkerStarting with 2.6.14 a new method was introduced which supports subscribing 655*4dc78e53SAndroid Build Coastguard Workerto an almost infinite number of multicast groups. 656*4dc78e53SAndroid Build Coastguard Worker 657*4dc78e53SAndroid Build Coastguard Worker[source,c] 658*4dc78e53SAndroid Build Coastguard Worker-------- 659*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 660*4dc78e53SAndroid Build Coastguard Worker 661*4dc78e53SAndroid Build Coastguard Workerint nl_socket_add_memberships(struct nl_sock *sk, int group, ...); 662*4dc78e53SAndroid Build Coastguard Workerint nl_socket_drop_memberships(struct nl_sock *sk, int group, ...); 663*4dc78e53SAndroid Build Coastguard Worker-------- 664*4dc78e53SAndroid Build Coastguard Worker 665*4dc78e53SAndroid Build Coastguard Worker==== Multicast Example 666*4dc78e53SAndroid Build Coastguard Worker 667*4dc78e53SAndroid Build Coastguard Worker[source,c] 668*4dc78e53SAndroid Build Coastguard Worker-------- 669*4dc78e53SAndroid Build Coastguard Worker#include <netlink/netlink.h> 670*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 671*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 672*4dc78e53SAndroid Build Coastguard Worker 673*4dc78e53SAndroid Build Coastguard Worker/* 674*4dc78e53SAndroid Build Coastguard Worker * This function will be called for each valid netlink message received 675*4dc78e53SAndroid Build Coastguard Worker * in nl_recvmsgs_default() 676*4dc78e53SAndroid Build Coastguard Worker */ 677*4dc78e53SAndroid Build Coastguard Workerstatic int my_func(struct nl_msg *msg, void *arg) 678*4dc78e53SAndroid Build Coastguard Worker{ 679*4dc78e53SAndroid Build Coastguard Worker return 0; 680*4dc78e53SAndroid Build Coastguard Worker} 681*4dc78e53SAndroid Build Coastguard Worker 682*4dc78e53SAndroid Build Coastguard Workerstruct nl_sock *sk; 683*4dc78e53SAndroid Build Coastguard Worker 684*4dc78e53SAndroid Build Coastguard Worker/* Allocate a new socket */ 685*4dc78e53SAndroid Build Coastguard Workersk = nl_socket_alloc(); 686*4dc78e53SAndroid Build Coastguard Worker 687*4dc78e53SAndroid Build Coastguard Worker/* 688*4dc78e53SAndroid Build Coastguard Worker * Notifications do not use sequence numbers, disable sequence number 689*4dc78e53SAndroid Build Coastguard Worker * checking. 690*4dc78e53SAndroid Build Coastguard Worker */ 691*4dc78e53SAndroid Build Coastguard Workernl_socket_disable_seq_check(sk); 692*4dc78e53SAndroid Build Coastguard Worker 693*4dc78e53SAndroid Build Coastguard Worker/* 694*4dc78e53SAndroid Build Coastguard Worker * Define a callback function, which will be called for each notification 695*4dc78e53SAndroid Build Coastguard Worker * received 696*4dc78e53SAndroid Build Coastguard Worker */ 697*4dc78e53SAndroid Build Coastguard Workernl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, my_func, NULL); 698*4dc78e53SAndroid Build Coastguard Worker 699*4dc78e53SAndroid Build Coastguard Worker/* Connect to routing netlink protocol */ 700*4dc78e53SAndroid Build Coastguard Workernl_connect(sk, NETLINK_ROUTE); 701*4dc78e53SAndroid Build Coastguard Worker 702*4dc78e53SAndroid Build Coastguard Worker/* Subscribe to link notifications group */ 703*4dc78e53SAndroid Build Coastguard Workernl_socket_add_memberships(sk, RTNLGRP_LINK, 0); 704*4dc78e53SAndroid Build Coastguard Worker 705*4dc78e53SAndroid Build Coastguard Worker/* 706*4dc78e53SAndroid Build Coastguard Worker * Start receiving messages. The function nl_recvmsgs_default() will block 707*4dc78e53SAndroid Build Coastguard Worker * until one or more netlink messages (notification) are received which 708*4dc78e53SAndroid Build Coastguard Worker * will be passed on to my_func(). 709*4dc78e53SAndroid Build Coastguard Worker */ 710*4dc78e53SAndroid Build Coastguard Workerwhile (1) 711*4dc78e53SAndroid Build Coastguard Worker nl_recvmsgs_default(sock); 712*4dc78e53SAndroid Build Coastguard Worker-------- 713*4dc78e53SAndroid Build Coastguard Worker 714*4dc78e53SAndroid Build Coastguard Worker[[core_sk_cb]] 715*4dc78e53SAndroid Build Coastguard Worker=== Modifiying Socket Callback Configuration 716*4dc78e53SAndroid Build Coastguard Worker 717*4dc78e53SAndroid Build Coastguard WorkerSee <<core_cb>> for more information on 718*4dc78e53SAndroid Build Coastguard Workercallback hooks and overwriting capabilities. 719*4dc78e53SAndroid Build Coastguard Worker 720*4dc78e53SAndroid Build Coastguard WorkerEach socket is assigned a callback configuration which controls the 721*4dc78e53SAndroid Build Coastguard Workerbehaviour of the socket. This is f.e. required to have a separate 722*4dc78e53SAndroid Build Coastguard Workermessage receive function per socket. It is perfectly legal to share 723*4dc78e53SAndroid Build Coastguard Workercallback configurations between sockets though. 724*4dc78e53SAndroid Build Coastguard Worker 725*4dc78e53SAndroid Build Coastguard WorkerThe following functions can be used to access and set the callback 726*4dc78e53SAndroid Build Coastguard Workerconfiguration of a socket: 727*4dc78e53SAndroid Build Coastguard Worker 728*4dc78e53SAndroid Build Coastguard Worker[source,c] 729*4dc78e53SAndroid Build Coastguard Worker-------- 730*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 731*4dc78e53SAndroid Build Coastguard Worker 732*4dc78e53SAndroid Build Coastguard Workerstruct nl_cb *nl_socket_get_cb(const struct nl_sock *sk); 733*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_set_cb(struct nl_sock *sk, struct nl_cb *cb); 734*4dc78e53SAndroid Build Coastguard Worker-------- 735*4dc78e53SAndroid Build Coastguard Worker 736*4dc78e53SAndroid Build Coastguard WorkerAdditionally a shortcut exists to modify the callback configuration 737*4dc78e53SAndroid Build Coastguard Workerassigned to a socket directly: 738*4dc78e53SAndroid Build Coastguard Worker 739*4dc78e53SAndroid Build Coastguard Worker[source,c] 740*4dc78e53SAndroid Build Coastguard Worker-------- 741*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 742*4dc78e53SAndroid Build Coastguard Worker 743*4dc78e53SAndroid Build Coastguard Workerint nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type, enum nl_cb_kind kind, 744*4dc78e53SAndroid Build Coastguard Worker nl_recvmsg_msg_cb_t func, void *arg); 745*4dc78e53SAndroid Build Coastguard Worker-------- 746*4dc78e53SAndroid Build Coastguard Worker 747*4dc78e53SAndroid Build Coastguard Worker.Example: 748*4dc78e53SAndroid Build Coastguard Worker[source,c] 749*4dc78e53SAndroid Build Coastguard Worker-------- 750*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 751*4dc78e53SAndroid Build Coastguard Worker 752*4dc78e53SAndroid Build Coastguard Worker// Call my_input() for all valid messages received in socket sk 753*4dc78e53SAndroid Build Coastguard Workernl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, my_input, NULL); 754*4dc78e53SAndroid Build Coastguard Worker-------- 755*4dc78e53SAndroid Build Coastguard Worker 756*4dc78e53SAndroid Build Coastguard Worker=== Socket Attributes 757*4dc78e53SAndroid Build Coastguard Worker 758*4dc78e53SAndroid Build Coastguard Worker.Local Port 759*4dc78e53SAndroid Build Coastguard Worker 760*4dc78e53SAndroid Build Coastguard WorkerThe local port number uniquely identifies the socket and is used to 761*4dc78e53SAndroid Build Coastguard Workeraddress it. A unique local port is generated automatically when the 762*4dc78e53SAndroid Build Coastguard Workersocket is allocated. It will consist of the Process ID (22 bits) and a 763*4dc78e53SAndroid Build Coastguard Workerrandom number (10 bits) thus allowing up to 1024 sockets per process. 764*4dc78e53SAndroid Build Coastguard Worker 765*4dc78e53SAndroid Build Coastguard Worker[source,c] 766*4dc78e53SAndroid Build Coastguard Worker-------- 767*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 768*4dc78e53SAndroid Build Coastguard Worker 769*4dc78e53SAndroid Build Coastguard Workeruint32_t nl_socket_get_local_port(const struct nl_sock *sk); 770*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_set_local_port(struct nl_sock *sk, uint32_t port); 771*4dc78e53SAndroid Build Coastguard Worker-------- 772*4dc78e53SAndroid Build Coastguard Worker 773*4dc78e53SAndroid Build Coastguard WorkerSee section <<core_addressing>> for more information on port numbers. 774*4dc78e53SAndroid Build Coastguard Worker 775*4dc78e53SAndroid Build Coastguard WorkerCAUTION: Overwriting the local port is possible but you have to ensure 776*4dc78e53SAndroid Build Coastguard Workerthat the provided value is unique and no other socket in any other 777*4dc78e53SAndroid Build Coastguard Workerapplication is using the same value. 778*4dc78e53SAndroid Build Coastguard Worker 779*4dc78e53SAndroid Build Coastguard Worker.Peer Port 780*4dc78e53SAndroid Build Coastguard Worker 781*4dc78e53SAndroid Build Coastguard WorkerA peer port can be assigned to the socket which will result in all 782*4dc78e53SAndroid Build Coastguard Workerunicast messages sent over the socket to be addresses to the peer. If 783*4dc78e53SAndroid Build Coastguard Workerno peer is specified, the message is sent to the kernel which will try 784*4dc78e53SAndroid Build Coastguard Workerto automatically bind the socket to a kernel side socket of the same 785*4dc78e53SAndroid Build Coastguard Workernetlink protocol family. It is common practice not to bind the socket 786*4dc78e53SAndroid Build Coastguard Workerto a peer port as typically only one kernel side socket exists per 787*4dc78e53SAndroid Build Coastguard Workernetlink protocol family. 788*4dc78e53SAndroid Build Coastguard Worker 789*4dc78e53SAndroid Build Coastguard Worker[source,c] 790*4dc78e53SAndroid Build Coastguard Worker-------- 791*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 792*4dc78e53SAndroid Build Coastguard Worker 793*4dc78e53SAndroid Build Coastguard Workeruint32_t nl_socket_get_peer_port(const struct nl_sock *sk); 794*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_set_peer_port(struct nl_sock *sk, uint32_t port); 795*4dc78e53SAndroid Build Coastguard Worker-------- 796*4dc78e53SAndroid Build Coastguard Worker 797*4dc78e53SAndroid Build Coastguard WorkerSee section <<core_addressing>> for more information on port numbers. 798*4dc78e53SAndroid Build Coastguard Worker 799*4dc78e53SAndroid Build Coastguard Worker.File Descriptor 800*4dc78e53SAndroid Build Coastguard Worker 801*4dc78e53SAndroid Build Coastguard WorkerNetlink uses the BSD socket interface, therefore a file descriptor is 802*4dc78e53SAndroid Build Coastguard Workerbehind each socket and you may use it directly. 803*4dc78e53SAndroid Build Coastguard Worker 804*4dc78e53SAndroid Build Coastguard Worker[source,c] 805*4dc78e53SAndroid Build Coastguard Worker-------- 806*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 807*4dc78e53SAndroid Build Coastguard Worker 808*4dc78e53SAndroid Build Coastguard Workerint nl_socket_get_fd(const struct nl_sock *sk); 809*4dc78e53SAndroid Build Coastguard Worker-------- 810*4dc78e53SAndroid Build Coastguard Worker 811*4dc78e53SAndroid Build Coastguard WorkerIf a socket is used to only receive notifications it usually is best 812*4dc78e53SAndroid Build Coastguard Workerto put the socket in non-blocking mode and periodically poll for new 813*4dc78e53SAndroid Build Coastguard Workernotifications. 814*4dc78e53SAndroid Build Coastguard Worker 815*4dc78e53SAndroid Build Coastguard Worker[source,c] 816*4dc78e53SAndroid Build Coastguard Worker-------- 817*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 818*4dc78e53SAndroid Build Coastguard Worker 819*4dc78e53SAndroid Build Coastguard Workerint nl_socket_set_nonblocking(const struct nl_sock *sk); 820*4dc78e53SAndroid Build Coastguard Worker-------- 821*4dc78e53SAndroid Build Coastguard Worker 822*4dc78e53SAndroid Build Coastguard Worker.Send/Receive Buffer Size 823*4dc78e53SAndroid Build Coastguard Worker 824*4dc78e53SAndroid Build Coastguard WorkerThe socket buffer is used to queue netlink messages between sender and 825*4dc78e53SAndroid Build Coastguard Workerreceiver. The size of these buffers specifies the maximum size you 826*4dc78e53SAndroid Build Coastguard Workerwill be able to write() to a netlink socket, i.e. it will indirectly 827*4dc78e53SAndroid Build Coastguard Workerdefine the maximum message size. The default is 32KiB. 828*4dc78e53SAndroid Build Coastguard Worker 829*4dc78e53SAndroid Build Coastguard Worker[source,c] 830*4dc78e53SAndroid Build Coastguard Worker-------- 831*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 832*4dc78e53SAndroid Build Coastguard Worker 833*4dc78e53SAndroid Build Coastguard Workerint nl_socket_set_buffer_size(struct nl_sock *sk, int rx, int tx); 834*4dc78e53SAndroid Build Coastguard Worker-------- 835*4dc78e53SAndroid Build Coastguard Worker 836*4dc78e53SAndroid Build Coastguard Worker[[core_sk_cred]] 837*4dc78e53SAndroid Build Coastguard Worker.Enable/Disable Credentials 838*4dc78e53SAndroid Build Coastguard Worker 839*4dc78e53SAndroid Build Coastguard WorkerTODO 840*4dc78e53SAndroid Build Coastguard Worker 841*4dc78e53SAndroid Build Coastguard Worker[source,c] 842*4dc78e53SAndroid Build Coastguard Worker-------- 843*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 844*4dc78e53SAndroid Build Coastguard Worker 845*4dc78e53SAndroid Build Coastguard Workerint nl_socket_set_passcred(struct nl_sock *sk, int state); 846*4dc78e53SAndroid Build Coastguard Worker-------- 847*4dc78e53SAndroid Build Coastguard Worker 848*4dc78e53SAndroid Build Coastguard Worker.Enable/Disable Auto-ACK Mode 849*4dc78e53SAndroid Build Coastguard Worker 850*4dc78e53SAndroid Build Coastguard WorkerThe following functions allow to enable/disable Auto-ACK mode on a socket. 851*4dc78e53SAndroid Build Coastguard WorkerSee <<core_auto_ack>> for more information on what implications that has. 852*4dc78e53SAndroid Build Coastguard WorkerAuto-ACK mode is enabled by default. 853*4dc78e53SAndroid Build Coastguard Worker 854*4dc78e53SAndroid Build Coastguard Worker[source,c] 855*4dc78e53SAndroid Build Coastguard Worker-------- 856*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 857*4dc78e53SAndroid Build Coastguard Worker 858*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_enable_auto_ack(struct nl_sock *sk); 859*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_disable_auto_ack(struct nl_sock *sk); 860*4dc78e53SAndroid Build Coastguard Worker-------- 861*4dc78e53SAndroid Build Coastguard Worker 862*4dc78e53SAndroid Build Coastguard Worker.Enable/Disable Message Peeking 863*4dc78e53SAndroid Build Coastguard Worker 864*4dc78e53SAndroid Build Coastguard WorkerIf enabled, message peeking causes nl_recv() to try and use MSG_PEEK 865*4dc78e53SAndroid Build Coastguard Workerto retrieve the size of the next message received and allocate a 866*4dc78e53SAndroid Build Coastguard Workerbuffer of that size. Message peeking is enabled by default but can be 867*4dc78e53SAndroid Build Coastguard Workerdisabled using the following function: 868*4dc78e53SAndroid Build Coastguard Worker 869*4dc78e53SAndroid Build Coastguard Worker[source,c] 870*4dc78e53SAndroid Build Coastguard Worker-------- 871*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 872*4dc78e53SAndroid Build Coastguard Worker 873*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_enable_msg_peek(struct nl_sock *sk); 874*4dc78e53SAndroid Build Coastguard Workervoid nl_socket_disable_msg_peek(struct nl_sock *sk); 875*4dc78e53SAndroid Build Coastguard Worker-------- 876*4dc78e53SAndroid Build Coastguard Worker 877*4dc78e53SAndroid Build Coastguard Worker.Enable/Disable Receival of Packet Information 878*4dc78e53SAndroid Build Coastguard Worker 879*4dc78e53SAndroid Build Coastguard WorkerIf enabled, each received netlink message from the kernel will include 880*4dc78e53SAndroid Build Coastguard Workeran additional struct nl_pktinfo in the control message. The following 881*4dc78e53SAndroid Build Coastguard Workerfunction can be used to enable/disable receival of packet information. 882*4dc78e53SAndroid Build Coastguard Worker 883*4dc78e53SAndroid Build Coastguard Worker[source,c] 884*4dc78e53SAndroid Build Coastguard Worker-------- 885*4dc78e53SAndroid Build Coastguard Worker#include <netlink/socket.h> 886*4dc78e53SAndroid Build Coastguard Worker 887*4dc78e53SAndroid Build Coastguard Workerint nl_socket_recv_pktinfo(struct nl_sock *sk, int state); 888*4dc78e53SAndroid Build Coastguard Worker-------- 889*4dc78e53SAndroid Build Coastguard Worker 890*4dc78e53SAndroid Build Coastguard WorkerCAUTION: Processing of NETLINK_PKTINFO has not been implemented yet. 891*4dc78e53SAndroid Build Coastguard Worker 892*4dc78e53SAndroid Build Coastguard Worker[[core_send_recv]] 893*4dc78e53SAndroid Build Coastguard Worker== Sending and Receiving of Messages / Data 894*4dc78e53SAndroid Build Coastguard Worker 895*4dc78e53SAndroid Build Coastguard Worker[[core_send]] 896*4dc78e53SAndroid Build Coastguard Worker=== Sending Messages 897*4dc78e53SAndroid Build Coastguard Worker 898*4dc78e53SAndroid Build Coastguard WorkerThe standard method of sending a netlink message over a netlink socket 899*4dc78e53SAndroid Build Coastguard Workeris to use the function nl_send_auto(). It will automatically complete 900*4dc78e53SAndroid Build Coastguard Workerthe netlink message by filling the missing bits and pieces in the 901*4dc78e53SAndroid Build Coastguard Workernetlink message header and will deal with addressing based on the 902*4dc78e53SAndroid Build Coastguard Workeroptions and address set in the netlink socket. The message is then 903*4dc78e53SAndroid Build Coastguard Workerpassed on to nl_send(). 904*4dc78e53SAndroid Build Coastguard Worker 905*4dc78e53SAndroid Build Coastguard WorkerIf the default sending semantics implemented by nl_send() do not suit 906*4dc78e53SAndroid Build Coastguard Workerthe application, it may overwrite the sending function nl_send() by 907*4dc78e53SAndroid Build Coastguard Workerspecifying an own implementation using the function 908*4dc78e53SAndroid Build Coastguard Workernl_cb_overwrite_send(). 909*4dc78e53SAndroid Build Coastguard Worker 910*4dc78e53SAndroid Build Coastguard Worker[source,c] 911*4dc78e53SAndroid Build Coastguard Worker-------- 912*4dc78e53SAndroid Build Coastguard Worker nl_send_auto(sk, msg) 913*4dc78e53SAndroid Build Coastguard Worker | 914*4dc78e53SAndroid Build Coastguard Worker |-----> nl_complete_msg(sk, msg) 915*4dc78e53SAndroid Build Coastguard Worker | 916*4dc78e53SAndroid Build Coastguard Worker | 917*4dc78e53SAndroid Build Coastguard Worker | Own send function specified via nl_cb_overwrite_send() 918*4dc78e53SAndroid Build Coastguard Worker |- - - - - - - - - - - - - - - - - - - - 919*4dc78e53SAndroid Build Coastguard Worker v v 920*4dc78e53SAndroid Build Coastguard Worker nl_send(sk, msg) send_func() 921*4dc78e53SAndroid Build Coastguard Worker-------- 922*4dc78e53SAndroid Build Coastguard Worker 923*4dc78e53SAndroid Build Coastguard Worker.Using nl_send() 924*4dc78e53SAndroid Build Coastguard Worker 925*4dc78e53SAndroid Build Coastguard WorkerIf you do not require any of the automatic message completion 926*4dc78e53SAndroid Build Coastguard Workerfunctionality you may use nl_send() directly but beware that any 927*4dc78e53SAndroid Build Coastguard Workerinternal calls to nl_send_auto() by the library to send netlink 928*4dc78e53SAndroid Build Coastguard Workermessages will still use nl_send(). Therefore if you wish to use any 929*4dc78e53SAndroid Build Coastguard Workerhigher level interfaces and the behaviour of nl_send() is to your 930*4dc78e53SAndroid Build Coastguard Workerdislike then you must overwrite the nl_send() function via 931*4dc78e53SAndroid Build Coastguard Workernl_cb_overwrite_send() 932*4dc78e53SAndroid Build Coastguard Worker 933*4dc78e53SAndroid Build Coastguard WorkerThe purpose of nl_send() is to embed the netlink message into an iovec 934*4dc78e53SAndroid Build Coastguard Workerstructure and pass it on to nl_send_iovec(). 935*4dc78e53SAndroid Build Coastguard Worker 936*4dc78e53SAndroid Build Coastguard Worker[source,c] 937*4dc78e53SAndroid Build Coastguard Worker-------- 938*4dc78e53SAndroid Build Coastguard Worker nl_send(sk, msg) 939*4dc78e53SAndroid Build Coastguard Worker | 940*4dc78e53SAndroid Build Coastguard Worker v 941*4dc78e53SAndroid Build Coastguard Worker nl_send_iovec(sk, msg, iov, iovlen) 942*4dc78e53SAndroid Build Coastguard Worker-------- 943*4dc78e53SAndroid Build Coastguard Worker 944*4dc78e53SAndroid Build Coastguard Worker.Using nl_send_iovec() 945*4dc78e53SAndroid Build Coastguard Worker 946*4dc78e53SAndroid Build Coastguard Workernl_send_iovec() expects a finalized netlink message and fills out the 947*4dc78e53SAndroid Build Coastguard Workerstruct msghdr used for addressing. It will first check if the struct 948*4dc78e53SAndroid Build Coastguard Workernl_msg is addressed to a specific peer (see nlmsg_set_dst()). If not, 949*4dc78e53SAndroid Build Coastguard Workerit will try to fall back to the peer address specified in the socket 950*4dc78e53SAndroid Build Coastguard Worker(see nl_socket_set_peer_port(). Otherwise the message will be sent 951*4dc78e53SAndroid Build Coastguard Workerunaddressed and it is left to the kernel to find the correct peer. 952*4dc78e53SAndroid Build Coastguard Worker 953*4dc78e53SAndroid Build Coastguard Workernl_send_iovec() also adds credentials if present and enabled 954*4dc78e53SAndroid Build Coastguard Worker(see <<core_sk_cred>>). 955*4dc78e53SAndroid Build Coastguard Worker 956*4dc78e53SAndroid Build Coastguard WorkerThe message is then passed on to nl_sendmsg(). 957*4dc78e53SAndroid Build Coastguard Worker 958*4dc78e53SAndroid Build Coastguard Worker[source,c] 959*4dc78e53SAndroid Build Coastguard Worker-------- 960*4dc78e53SAndroid Build Coastguard Worker nl_send_iovec(sk, msg, iov, iovlen) 961*4dc78e53SAndroid Build Coastguard Worker | 962*4dc78e53SAndroid Build Coastguard Worker v 963*4dc78e53SAndroid Build Coastguard Worker nl_sendmsg(sk, msg, msghdr) 964*4dc78e53SAndroid Build Coastguard Worker-------- 965*4dc78e53SAndroid Build Coastguard Worker 966*4dc78e53SAndroid Build Coastguard Worker.Using nl_sendmsg() 967*4dc78e53SAndroid Build Coastguard Worker 968*4dc78e53SAndroid Build Coastguard Workernl_sendmsg() expects a finalized netlink message and an optional 969*4dc78e53SAndroid Build Coastguard Workerstruct msghdr containing the peer address. It will copy the local 970*4dc78e53SAndroid Build Coastguard Workeraddress as defined in the socket (see nl_socket_set_local_port()) into 971*4dc78e53SAndroid Build Coastguard Workerthe netlink message header. 972*4dc78e53SAndroid Build Coastguard Worker 973*4dc78e53SAndroid Build Coastguard WorkerAt this point, construction of the message finished and it is ready to 974*4dc78e53SAndroid Build Coastguard Workerbe sent. 975*4dc78e53SAndroid Build Coastguard Worker 976*4dc78e53SAndroid Build Coastguard Worker[source,c] 977*4dc78e53SAndroid Build Coastguard Worker-------- 978*4dc78e53SAndroid Build Coastguard Worker nl_sendmsg(sk, msg, msghdr) 979*4dc78e53SAndroid Build Coastguard Worker |- - - - - - - - - - - - - - - - - - - - v 980*4dc78e53SAndroid Build Coastguard Worker | NL_CB_MSG_OUT() 981*4dc78e53SAndroid Build Coastguard Worker |<- - - - - - - - - - - - - - - - - - - -+ 982*4dc78e53SAndroid Build Coastguard Worker v 983*4dc78e53SAndroid Build Coastguard Worker sendmsg() 984*4dc78e53SAndroid Build Coastguard Worker-------- 985*4dc78e53SAndroid Build Coastguard Worker 986*4dc78e53SAndroid Build Coastguard WorkerBefore sending the application has one last chance to modify the 987*4dc78e53SAndroid Build Coastguard Workermessage. It is passed to the NL_CB_MSG_OUT callback function which 988*4dc78e53SAndroid Build Coastguard Workermay inspect or modify the message and return an error code. If this 989*4dc78e53SAndroid Build Coastguard Workererror code is NL_OK the message is sent using sendmsg() resulting in 990*4dc78e53SAndroid Build Coastguard Workerthe number of bytes written being returned. Otherwise the message 991*4dc78e53SAndroid Build Coastguard Workersending process is aborted and the error code specified by the 992*4dc78e53SAndroid Build Coastguard Workercallback function is returned. See <<core_sk_cb>> for more information 993*4dc78e53SAndroid Build Coastguard Workeron how to set callbacks. 994*4dc78e53SAndroid Build Coastguard Worker 995*4dc78e53SAndroid Build Coastguard Worker.Sending Raw Data with nl_sendto() 996*4dc78e53SAndroid Build Coastguard Worker 997*4dc78e53SAndroid Build Coastguard WorkerIf you wish to send raw data over a netlink socket, the following 998*4dc78e53SAndroid Build Coastguard Workerfunction will pass on any buffer provided to it directly to sendto(): 999*4dc78e53SAndroid Build Coastguard Worker 1000*4dc78e53SAndroid Build Coastguard Worker[source,c] 1001*4dc78e53SAndroid Build Coastguard Worker-------- 1002*4dc78e53SAndroid Build Coastguard Worker#include <netlink/netlink.h> 1003*4dc78e53SAndroid Build Coastguard Worker 1004*4dc78e53SAndroid Build Coastguard Workerint nl_sendto(struct nl_sock *sk, void *buf, size_t size); 1005*4dc78e53SAndroid Build Coastguard Worker-------- 1006*4dc78e53SAndroid Build Coastguard Worker 1007*4dc78e53SAndroid Build Coastguard Worker.Sending of Simple Messages 1008*4dc78e53SAndroid Build Coastguard Worker 1009*4dc78e53SAndroid Build Coastguard WorkerA special interface exists for sending of trivial messages. The function 1010*4dc78e53SAndroid Build Coastguard Workerexpects the netlink message type, optional netlink message flags, and an 1011*4dc78e53SAndroid Build Coastguard Workeroptional data buffer and data length. 1012*4dc78e53SAndroid Build Coastguard Worker[source,c] 1013*4dc78e53SAndroid Build Coastguard Worker-------- 1014*4dc78e53SAndroid Build Coastguard Worker#include <netlink/netlink.h> 1015*4dc78e53SAndroid Build Coastguard Worker 1016*4dc78e53SAndroid Build Coastguard Workerint nl_send_simple(struct nl_sock *sk, int type, int flags, 1017*4dc78e53SAndroid Build Coastguard Worker void *buf, size_t size); 1018*4dc78e53SAndroid Build Coastguard Worker-------- 1019*4dc78e53SAndroid Build Coastguard Worker 1020*4dc78e53SAndroid Build Coastguard WorkerThe function will construct a netlink message header based on the message 1021*4dc78e53SAndroid Build Coastguard Workertype and flags provided and append the data buffer as message payload. The 1022*4dc78e53SAndroid Build Coastguard Workernewly constructed message is sent with nl_send_auto(). 1023*4dc78e53SAndroid Build Coastguard Worker 1024*4dc78e53SAndroid Build Coastguard WorkerThe following example will send a netlink request message causing the 1025*4dc78e53SAndroid Build Coastguard Workerkernel to dump a list of all network links to userspace: 1026*4dc78e53SAndroid Build Coastguard Worker 1027*4dc78e53SAndroid Build Coastguard Worker[source,c] 1028*4dc78e53SAndroid Build Coastguard Worker-------- 1029*4dc78e53SAndroid Build Coastguard Worker#include <netlink/netlink.h> 1030*4dc78e53SAndroid Build Coastguard Worker 1031*4dc78e53SAndroid Build Coastguard Workerstruct nl_sock *sk; 1032*4dc78e53SAndroid Build Coastguard Workerstruct rtgenmsg rt_hdr = { 1033*4dc78e53SAndroid Build Coastguard Worker .rtgen_family = AF_UNSPEC, 1034*4dc78e53SAndroid Build Coastguard Worker}; 1035*4dc78e53SAndroid Build Coastguard Worker 1036*4dc78e53SAndroid Build Coastguard Workersk = nl_socket_alloc(); 1037*4dc78e53SAndroid Build Coastguard Workernl_connect(sk, NETLINK_ROUTE); 1038*4dc78e53SAndroid Build Coastguard Worker 1039*4dc78e53SAndroid Build Coastguard Workernl_send_simple(sock, RTM_GETLINK, NLM_F_DUMP, &rt_hdr, sizeof(rt_hdr)); 1040*4dc78e53SAndroid Build Coastguard Worker-------- 1041*4dc78e53SAndroid Build Coastguard Worker 1042*4dc78e53SAndroid Build Coastguard Worker[[core_recv]] 1043*4dc78e53SAndroid Build Coastguard Worker=== Receiving Messages 1044*4dc78e53SAndroid Build Coastguard Worker 1045*4dc78e53SAndroid Build Coastguard WorkerThe easiest method to receive netlink messages is to call nl_recvmsgs_default(). 1046*4dc78e53SAndroid Build Coastguard WorkerIt will receive messages based on the semantics defined in the socket. The 1047*4dc78e53SAndroid Build Coastguard Workerapplication may customize these in detail although the default behaviour will 1048*4dc78e53SAndroid Build Coastguard Workerprobably suit most applications. 1049*4dc78e53SAndroid Build Coastguard Worker 1050*4dc78e53SAndroid Build Coastguard Workernl_recvmsgs_default() will also be called internally by the library whenever 1051*4dc78e53SAndroid Build Coastguard Workerit needs to receive and parse a netlink message. 1052*4dc78e53SAndroid Build Coastguard Worker 1053*4dc78e53SAndroid Build Coastguard WorkerThe function will fetch the callback configuration stored in the socket and 1054*4dc78e53SAndroid Build Coastguard Workercall nl_recvmsgs(): 1055*4dc78e53SAndroid Build Coastguard Worker 1056*4dc78e53SAndroid Build Coastguard Worker[source,c] 1057*4dc78e53SAndroid Build Coastguard Worker-------- 1058*4dc78e53SAndroid Build Coastguard Worker nl_recvmsgs_default(sk) 1059*4dc78e53SAndroid Build Coastguard Worker | 1060*4dc78e53SAndroid Build Coastguard Worker | cb = nl_socket_get_cb(sk) 1061*4dc78e53SAndroid Build Coastguard Worker v 1062*4dc78e53SAndroid Build Coastguard Worker nl_recvmsgs(sk, cb) 1063*4dc78e53SAndroid Build Coastguard Worker-------- 1064*4dc78e53SAndroid Build Coastguard Worker 1065*4dc78e53SAndroid Build Coastguard Worker.Using nl_recvmsgs() 1066*4dc78e53SAndroid Build Coastguard Worker 1067*4dc78e53SAndroid Build Coastguard Workernl_recvmsgs() implements the actual receiving loop, it blocks until a 1068*4dc78e53SAndroid Build Coastguard Workernetlink message has been received unless the socket has been put into 1069*4dc78e53SAndroid Build Coastguard Workernon-blocking mode. 1070*4dc78e53SAndroid Build Coastguard Worker 1071*4dc78e53SAndroid Build Coastguard WorkerFor the unlikely scenario that certain required receive characteristics 1072*4dc78e53SAndroid Build Coastguard Workercan not be achieved by fine tuning the internal recvmsgs function using 1073*4dc78e53SAndroid Build Coastguard Workerthe callback configuration (see <<core_sk_cb>>) the application may provide 1074*4dc78e53SAndroid Build Coastguard Workera complete own implementation of it and overwrite all calls to nl_recvmsgs() 1075*4dc78e53SAndroid Build Coastguard Workerwith the function nl_cb_overwrite_recvmsgs(). 1076*4dc78e53SAndroid Build Coastguard Worker 1077*4dc78e53SAndroid Build Coastguard Worker[source,c] 1078*4dc78e53SAndroid Build Coastguard Worker-------- 1079*4dc78e53SAndroid Build Coastguard Worker nl_recvmsgs(sk, cb) 1080*4dc78e53SAndroid Build Coastguard Worker | 1081*4dc78e53SAndroid Build Coastguard Worker | Own recvmsgs function specified via nl_cb_overwrite_recvmsgs() 1082*4dc78e53SAndroid Build Coastguard Worker |- - - - - - - - - - - - - - - - - - - - 1083*4dc78e53SAndroid Build Coastguard Worker v v 1084*4dc78e53SAndroid Build Coastguard Worker internal_recvmsgs() my_recvmsgs() 1085*4dc78e53SAndroid Build Coastguard Worker-------- 1086*4dc78e53SAndroid Build Coastguard Worker 1087*4dc78e53SAndroid Build Coastguard Worker[[core_recv_character]] 1088*4dc78e53SAndroid Build Coastguard Worker.Receive Characteristics 1089*4dc78e53SAndroid Build Coastguard Worker 1090*4dc78e53SAndroid Build Coastguard WorkerIf the application does not provide its own recvmsgs() implementation 1091*4dc78e53SAndroid Build Coastguard Workerwith the function nl_cb_overwrite_recvmsgs() the following characteristics 1092*4dc78e53SAndroid Build Coastguard Workerapply while receiving data from a netlink socket: 1093*4dc78e53SAndroid Build Coastguard Worker 1094*4dc78e53SAndroid Build Coastguard Worker[source,c] 1095*4dc78e53SAndroid Build Coastguard Worker-------- 1096*4dc78e53SAndroid Build Coastguard Worker internal_recvmsgs() 1097*4dc78e53SAndroid Build Coastguard Worker | 1098*4dc78e53SAndroid Build Coastguard Worker+-------------->| Own recv function specified with nl_cb_overwrite_recv() 1099*4dc78e53SAndroid Build Coastguard Worker| |- - - - - - - - - - - - - - - - 1100*4dc78e53SAndroid Build Coastguard Worker| v v 1101*4dc78e53SAndroid Build Coastguard Worker| nl_recv() my_recv() 1102*4dc78e53SAndroid Build Coastguard Worker| |<- - - - - - - - - - - - - - -+ 1103*4dc78e53SAndroid Build Coastguard Worker| |<-------------+ 1104*4dc78e53SAndroid Build Coastguard Worker| v | More data to parse? (nlmsg_next()) 1105*4dc78e53SAndroid Build Coastguard Worker| Parse Message | 1106*4dc78e53SAndroid Build Coastguard Worker| |--------------+ 1107*4dc78e53SAndroid Build Coastguard Worker| v 1108*4dc78e53SAndroid Build Coastguard Worker+------- NLM_F_MULTI set? 1109*4dc78e53SAndroid Build Coastguard Worker | 1110*4dc78e53SAndroid Build Coastguard Worker v 1111*4dc78e53SAndroid Build Coastguard Worker (SUCCESS) 1112*4dc78e53SAndroid Build Coastguard Worker-------- 1113*4dc78e53SAndroid Build Coastguard Worker 1114*4dc78e53SAndroid Build Coastguard WorkerThe function nl_recv() is invoked first to receive data from the 1115*4dc78e53SAndroid Build Coastguard Workernetlink socket. This function may be overwritten by the application 1116*4dc78e53SAndroid Build Coastguard Workerby an own implementation using the function nl_cb_overwrite_recv(). 1117*4dc78e53SAndroid Build Coastguard WorkerThis may be useful if the netlink byte stream is in fact not received 1118*4dc78e53SAndroid Build Coastguard Workerfrom a socket directly but is read from a file or another source. 1119*4dc78e53SAndroid Build Coastguard Worker 1120*4dc78e53SAndroid Build Coastguard WorkerIf data has been read, it will be attempted to parse the data. This 1121*4dc78e53SAndroid Build Coastguard Workerwill be done repeatedly until the parser returns NL_STOP, an error was 1122*4dc78e53SAndroid Build Coastguard Workerreturned or all data has been parsed. 1123*4dc78e53SAndroid Build Coastguard Worker 1124*4dc78e53SAndroid Build Coastguard WorkerIn case the last message parsed successfully was a multipart message 1125*4dc78e53SAndroid Build Coastguard Worker(see <<core_multipart>>) and the parser did not 1126*4dc78e53SAndroid Build Coastguard Workerquit due to either an error or NL_STOP nl_recv() respectively the 1127*4dc78e53SAndroid Build Coastguard Workerapplications own implementation will be called again and the parser 1128*4dc78e53SAndroid Build Coastguard Workerstarts all over. 1129*4dc78e53SAndroid Build Coastguard Worker 1130*4dc78e53SAndroid Build Coastguard WorkerSee <<core_parse_character>> for information on how to extract valid 1131*4dc78e53SAndroid Build Coastguard Workernetlink messages from the parser and on how to control the behaviour 1132*4dc78e53SAndroid Build Coastguard Workerof it. 1133*4dc78e53SAndroid Build Coastguard Worker 1134*4dc78e53SAndroid Build Coastguard Worker[[core_parse_character]] 1135*4dc78e53SAndroid Build Coastguard Worker.Parsing Characteristics 1136*4dc78e53SAndroid Build Coastguard Worker 1137*4dc78e53SAndroid Build Coastguard WorkerThe internal parser is invoked for each netlink message received from 1138*4dc78e53SAndroid Build Coastguard Workera netlink socket. It is typically fed by nl_recv() (see 1139*4dc78e53SAndroid Build Coastguard Worker<<core_recv_character>>). 1140*4dc78e53SAndroid Build Coastguard Worker 1141*4dc78e53SAndroid Build Coastguard WorkerThe parser will first ensure that the length of the data stream 1142*4dc78e53SAndroid Build Coastguard Workerprovided is sufficient to contain a netlink message header and that 1143*4dc78e53SAndroid Build Coastguard Workerthe message length as specified in the message header does not exceed 1144*4dc78e53SAndroid Build Coastguard Workerit. 1145*4dc78e53SAndroid Build Coastguard Worker 1146*4dc78e53SAndroid Build Coastguard WorkerIf this criteria is met, a new struct nl_msg is allocated and the 1147*4dc78e53SAndroid Build Coastguard Workermessage is passed on to the the callback function NL_CB_MSG_IN if one 1148*4dc78e53SAndroid Build Coastguard Workeris set. Like any other callback function, it may return NL_SKIP to 1149*4dc78e53SAndroid Build Coastguard Workerskip the current message but continue parsing the next message or 1150*4dc78e53SAndroid Build Coastguard WorkerNL_STOP to stop parsing completely. 1151*4dc78e53SAndroid Build Coastguard Worker 1152*4dc78e53SAndroid Build Coastguard WorkerThe next step is to check the sequence number of the message against 1153*4dc78e53SAndroid Build Coastguard Workerthe currently expected sequence number. The application may provide 1154*4dc78e53SAndroid Build Coastguard Workerits own sequence number checking algorithm by setting the callback 1155*4dc78e53SAndroid Build Coastguard Workerfunction NL_CB_SEQ_CHECK to its own implementation. In fact, calling 1156*4dc78e53SAndroid Build Coastguard Workernl_socket_disable_seq_check() to disable sequence number checking will 1157*4dc78e53SAndroid Build Coastguard Workerdo nothing more than set the NL_CB_SEQ_CHECK hook to a function which 1158*4dc78e53SAndroid Build Coastguard Workeralways returns NL_OK. 1159*4dc78e53SAndroid Build Coastguard Worker 1160*4dc78e53SAndroid Build Coastguard WorkerAnother callback hook NL_CB_SEND_ACK exists which is called if the 1161*4dc78e53SAndroid Build Coastguard Workermessage has the NLM_F_ACK flag set. Although I am not aware of any 1162*4dc78e53SAndroid Build Coastguard Workeruserspace netlink socket doing this, the application may want to send 1163*4dc78e53SAndroid Build Coastguard Workeran ACK message back to the sender (see <<core_msg_ack>>). 1164*4dc78e53SAndroid Build Coastguard Worker 1165*4dc78e53SAndroid Build Coastguard Worker[source,c] 1166*4dc78e53SAndroid Build Coastguard Worker-------- 1167*4dc78e53SAndroid Build Coastguard Worker parse() 1168*4dc78e53SAndroid Build Coastguard Worker | 1169*4dc78e53SAndroid Build Coastguard Worker v 1170*4dc78e53SAndroid Build Coastguard Worker nlmsg_ok() --> Ignore 1171*4dc78e53SAndroid Build Coastguard Worker | 1172*4dc78e53SAndroid Build Coastguard Worker |- - - - - - - - - - - - - - - v 1173*4dc78e53SAndroid Build Coastguard Worker | NL_CB_MSG_IN() 1174*4dc78e53SAndroid Build Coastguard Worker |<- - - - - - - - - - - - - - -+ 1175*4dc78e53SAndroid Build Coastguard Worker | 1176*4dc78e53SAndroid Build Coastguard Worker |- - - - - - - - - - - - - - - v 1177*4dc78e53SAndroid Build Coastguard Worker Sequence Check NL_CB_SEQ_CHECK() 1178*4dc78e53SAndroid Build Coastguard Worker |<- - - - - - - - - - - - - - -+ 1179*4dc78e53SAndroid Build Coastguard Worker | 1180*4dc78e53SAndroid Build Coastguard Worker | Message has NLM_F_ACK set 1181*4dc78e53SAndroid Build Coastguard Worker |- - - - - - - - - - - - - - - v 1182*4dc78e53SAndroid Build Coastguard Worker | NL_CB_SEND_ACK() 1183*4dc78e53SAndroid Build Coastguard Worker |<- - - - - - - - - - - - - - -+ 1184*4dc78e53SAndroid Build Coastguard Worker | 1185*4dc78e53SAndroid Build Coastguard Worker Handle Message Type 1186*4dc78e53SAndroid Build Coastguard Worker-------- 1187*4dc78e53SAndroid Build Coastguard Worker 1188*4dc78e53SAndroid Build Coastguard Worker[[core_auto_ack]] 1189*4dc78e53SAndroid Build Coastguard Worker=== Auto-ACK Mode 1190*4dc78e53SAndroid Build Coastguard Worker 1191*4dc78e53SAndroid Build Coastguard WorkerTODO 1192*4dc78e53SAndroid Build Coastguard Worker 1193*4dc78e53SAndroid Build Coastguard Worker== Message Parsing & Construction 1194*4dc78e53SAndroid Build Coastguard Worker 1195*4dc78e53SAndroid Build Coastguard Worker=== Message Format 1196*4dc78e53SAndroid Build Coastguard Worker 1197*4dc78e53SAndroid Build Coastguard WorkerSee <<core_netlink_fundamentals>> for an introduction to the netlink 1198*4dc78e53SAndroid Build Coastguard Workerprotocol and its message format. 1199*4dc78e53SAndroid Build Coastguard Worker 1200*4dc78e53SAndroid Build Coastguard Worker.Alignment 1201*4dc78e53SAndroid Build Coastguard Worker 1202*4dc78e53SAndroid Build Coastguard WorkerMost netlink protocols enforce a strict alignment policy for all 1203*4dc78e53SAndroid Build Coastguard Workerboundaries. The alignment value is defined by NLMSG_ALIGNTO and is 1204*4dc78e53SAndroid Build Coastguard Workerfixed to 4 bytes. Therefore all netlink message headers, begin of 1205*4dc78e53SAndroid Build Coastguard Workerpayload sections, protocol specific headers, and attribute sections 1206*4dc78e53SAndroid Build Coastguard Workermust start at an offset which is a multiple of NLMSG_ALIGNTO. 1207*4dc78e53SAndroid Build Coastguard Worker 1208*4dc78e53SAndroid Build Coastguard Worker[source,c] 1209*4dc78e53SAndroid Build Coastguard Worker-------- 1210*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1211*4dc78e53SAndroid Build Coastguard Worker 1212*4dc78e53SAndroid Build Coastguard Workerint nlmsg_size(int payloadlen); 1213*4dc78e53SAndroid Build Coastguard Workerint nlmsg_total_size(int payloadlen); 1214*4dc78e53SAndroid Build Coastguard Worker-------- 1215*4dc78e53SAndroid Build Coastguard Worker 1216*4dc78e53SAndroid Build Coastguard WorkerThe library provides a set of function to handle alignment 1217*4dc78e53SAndroid Build Coastguard Workerrequirements automatically. The function nlmsg_total_size() returns 1218*4dc78e53SAndroid Build Coastguard Workerthe total size of a netlink message including the padding to ensure 1219*4dc78e53SAndroid Build Coastguard Workerthe next message header is aligned correctly. 1220*4dc78e53SAndroid Build Coastguard Worker 1221*4dc78e53SAndroid Build Coastguard Worker[source,c] 1222*4dc78e53SAndroid Build Coastguard Worker-------- 1223*4dc78e53SAndroid Build Coastguard Worker <----------- nlmsg_total_size(len) ------------> 1224*4dc78e53SAndroid Build Coastguard Worker <----------- nlmsg_size(len) ------------> 1225*4dc78e53SAndroid Build Coastguard Worker +-------------------+- - -+- - - - - - - - +- - -+-------------------+- - - 1226*4dc78e53SAndroid Build Coastguard Worker | struct nlmsghdr | Pad | Payload | Pad | struct nlsmghdr | 1227*4dc78e53SAndroid Build Coastguard Worker +-------------------+- - -+- - - - - - - - +- - -+-------------------+- - - 1228*4dc78e53SAndroid Build Coastguard Worker <---- NLMSG_HDRLEN -----> <- NLMSG_ALIGN(len) -> <---- NLMSG_HDRLEN --- 1229*4dc78e53SAndroid Build Coastguard Worker-------- 1230*4dc78e53SAndroid Build Coastguard Worker 1231*4dc78e53SAndroid Build Coastguard WorkerIf you need to know if padding needs to be added at the end of a 1232*4dc78e53SAndroid Build Coastguard Workermessage, nlmsg_padlen() returns the number of padding bytes that need 1233*4dc78e53SAndroid Build Coastguard Workerto be added for a specific payload length. 1234*4dc78e53SAndroid Build Coastguard Worker 1235*4dc78e53SAndroid Build Coastguard Worker[source,c] 1236*4dc78e53SAndroid Build Coastguard Worker-------- 1237*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1238*4dc78e53SAndroid Build Coastguard Workerint nlmsg_padlen(int payloadlen); 1239*4dc78e53SAndroid Build Coastguard Worker-------- 1240*4dc78e53SAndroid Build Coastguard Worker 1241*4dc78e53SAndroid Build Coastguard Worker=== Parsing a Message 1242*4dc78e53SAndroid Build Coastguard Worker 1243*4dc78e53SAndroid Build Coastguard WorkerThe library offers two different methods of parsing netlink messages. 1244*4dc78e53SAndroid Build Coastguard WorkerIt offers a low level interface for applications which want to do all 1245*4dc78e53SAndroid Build Coastguard Workerthe parsing manually. This method is described below. Alternatively 1246*4dc78e53SAndroid Build Coastguard Workerthe library also offers an interface to implement a parser as part of 1247*4dc78e53SAndroid Build Coastguard Workera cache operations set which is especially useful when your protocol 1248*4dc78e53SAndroid Build Coastguard Workerdeals with objects of any sort such as network links, routes, etc. 1249*4dc78e53SAndroid Build Coastguard WorkerThis high level interface is described in <<core_cache>>. 1250*4dc78e53SAndroid Build Coastguard Worker 1251*4dc78e53SAndroid Build Coastguard Worker.Splitting a byte stream into separate messages 1252*4dc78e53SAndroid Build Coastguard Worker 1253*4dc78e53SAndroid Build Coastguard WorkerWhat you receive from a netlink socket is typically a stream of 1254*4dc78e53SAndroid Build Coastguard Workermessages. You will be given a buffer and its length, the buffer may 1255*4dc78e53SAndroid Build Coastguard Workercontain any number of netlink messages. 1256*4dc78e53SAndroid Build Coastguard Worker 1257*4dc78e53SAndroid Build Coastguard WorkerThe first message header starts at the beginning of message stream. 1258*4dc78e53SAndroid Build Coastguard WorkerAny subsequent message headers are access by calling nlmsg_next() on 1259*4dc78e53SAndroid Build Coastguard Workerthe previous header. 1260*4dc78e53SAndroid Build Coastguard Worker 1261*4dc78e53SAndroid Build Coastguard Worker[source,c] 1262*4dc78e53SAndroid Build Coastguard Worker-------- 1263*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1264*4dc78e53SAndroid Build Coastguard Worker 1265*4dc78e53SAndroid Build Coastguard Workerstruct nlmsghdr *nlmsg_next(struct nlmsghdr *hdr, int *remaining); 1266*4dc78e53SAndroid Build Coastguard Worker-------- 1267*4dc78e53SAndroid Build Coastguard Worker 1268*4dc78e53SAndroid Build Coastguard WorkerThe function nlmsg_next() will automatically substract the size of the 1269*4dc78e53SAndroid Build Coastguard Workerprevious message from the remaining number of bytes. 1270*4dc78e53SAndroid Build Coastguard Worker 1271*4dc78e53SAndroid Build Coastguard WorkerPlease note, there is no indication in the previous message whether 1272*4dc78e53SAndroid Build Coastguard Workeranother message follows or not. You must assume that more messages 1273*4dc78e53SAndroid Build Coastguard Workerfollow until all bytes of the message stream have been processed. 1274*4dc78e53SAndroid Build Coastguard Worker 1275*4dc78e53SAndroid Build Coastguard WorkerTo simplify this, the function nlmsg_ok() exists which returns true if 1276*4dc78e53SAndroid Build Coastguard Workeranother message fits into the remaining number of bytes in the message 1277*4dc78e53SAndroid Build Coastguard Workerstream. nlmsg_valid_hdr() is similar, it checks whether a specific 1278*4dc78e53SAndroid Build Coastguard Workernetlink message contains at least a minimum of payload. 1279*4dc78e53SAndroid Build Coastguard Worker 1280*4dc78e53SAndroid Build Coastguard Worker[source,c] 1281*4dc78e53SAndroid Build Coastguard Worker-------- 1282*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1283*4dc78e53SAndroid Build Coastguard Worker 1284*4dc78e53SAndroid Build Coastguard Workerint nlmsg_valid_hdr(const struct nlmsghdr *hdr, int payloadlen); 1285*4dc78e53SAndroid Build Coastguard Workerint nlmsg_ok(const struct nlmsghdr *hdr, int remaining); 1286*4dc78e53SAndroid Build Coastguard Worker-------- 1287*4dc78e53SAndroid Build Coastguard Worker 1288*4dc78e53SAndroid Build Coastguard WorkerA typical use of these functions looks like this: 1289*4dc78e53SAndroid Build Coastguard Worker 1290*4dc78e53SAndroid Build Coastguard Worker[source,c] 1291*4dc78e53SAndroid Build Coastguard Worker-------- 1292*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1293*4dc78e53SAndroid Build Coastguard Worker 1294*4dc78e53SAndroid Build Coastguard Workervoid my_parse(void *stream, int length) 1295*4dc78e53SAndroid Build Coastguard Worker{ 1296*4dc78e53SAndroid Build Coastguard Worker struct nlmsghdr *hdr = stream; 1297*4dc78e53SAndroid Build Coastguard Worker 1298*4dc78e53SAndroid Build Coastguard Worker while (nlmsg_ok(hdr, length)) { 1299*4dc78e53SAndroid Build Coastguard Worker // Parse message here 1300*4dc78e53SAndroid Build Coastguard Worker hdr = nlmsg_next(hdr, &length); 1301*4dc78e53SAndroid Build Coastguard Worker } 1302*4dc78e53SAndroid Build Coastguard Worker} 1303*4dc78e53SAndroid Build Coastguard Worker-------- 1304*4dc78e53SAndroid Build Coastguard Worker 1305*4dc78e53SAndroid Build Coastguard WorkerCAUTION: nlmsg_ok() only returns true if the *complete* message including 1306*4dc78e53SAndroid Build Coastguard Worker the message payload fits into the remaining buffer length. It will 1307*4dc78e53SAndroid Build Coastguard Worker return false if only a part of it fits. 1308*4dc78e53SAndroid Build Coastguard Worker 1309*4dc78e53SAndroid Build Coastguard WorkerThe above can also be written using the iterator nlmsg_for_each(): 1310*4dc78e53SAndroid Build Coastguard Worker 1311*4dc78e53SAndroid Build Coastguard Worker[source,c] 1312*4dc78e53SAndroid Build Coastguard Worker-------- 1313*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1314*4dc78e53SAndroid Build Coastguard Worker 1315*4dc78e53SAndroid Build Coastguard Workerstruct nlmsghdr *hdr; 1316*4dc78e53SAndroid Build Coastguard Worker 1317*4dc78e53SAndroid Build Coastguard Workernlmsg_for_each(hdr, stream, length) { 1318*4dc78e53SAndroid Build Coastguard Worker /* do something with message */ 1319*4dc78e53SAndroid Build Coastguard Worker} 1320*4dc78e53SAndroid Build Coastguard Worker-------- 1321*4dc78e53SAndroid Build Coastguard Worker 1322*4dc78e53SAndroid Build Coastguard Worker.Message Payload 1323*4dc78e53SAndroid Build Coastguard Worker 1324*4dc78e53SAndroid Build Coastguard WorkerThe message payload is appended to the message header and is guaranteed 1325*4dc78e53SAndroid Build Coastguard Workerto start at a multiple of +NLMSG_ALIGNTO+. Padding at the end of the 1326*4dc78e53SAndroid Build Coastguard Workermessage header is added if necessary to ensure this. The function 1327*4dc78e53SAndroid Build Coastguard Workernlmsg_data() will calculate the necessary offset based on the message 1328*4dc78e53SAndroid Build Coastguard Workerand returns a pointer to the start of the message payload. 1329*4dc78e53SAndroid Build Coastguard Worker 1330*4dc78e53SAndroid Build Coastguard Worker[source,c] 1331*4dc78e53SAndroid Build Coastguard Worker-------- 1332*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1333*4dc78e53SAndroid Build Coastguard Worker 1334*4dc78e53SAndroid Build Coastguard Workervoid *nlmsg_data(const struct nlmsghdr *nlh); 1335*4dc78e53SAndroid Build Coastguard Workervoid *nlmsg_tail(const struct nlmsghdr *nlh); 1336*4dc78e53SAndroid Build Coastguard Workerint nlmsg_datalen(const struct nlmsghdr *nlh); 1337*4dc78e53SAndroid Build Coastguard Worker-------- 1338*4dc78e53SAndroid Build Coastguard Worker 1339*4dc78e53SAndroid Build Coastguard WorkerThe length of the message payload is returned by nlmsg_datalen(). 1340*4dc78e53SAndroid Build Coastguard Worker 1341*4dc78e53SAndroid Build Coastguard Worker[source,c] 1342*4dc78e53SAndroid Build Coastguard Worker-------- 1343*4dc78e53SAndroid Build Coastguard Worker <--- nlmsg_datalen(nlh) ---> 1344*4dc78e53SAndroid Build Coastguard Worker +-------------------+- - -+----------------------------+- - -+ 1345*4dc78e53SAndroid Build Coastguard Worker | struct nlmsghdr | Pad | Payload | Pad | 1346*4dc78e53SAndroid Build Coastguard Worker +-------------------+- - -+----------------------------+- - -+ 1347*4dc78e53SAndroid Build Coastguard Workernlmsg_data(nlh) ---------------^ ^ 1348*4dc78e53SAndroid Build Coastguard Workernlmsg_tail(nlh) --------------------------------------------------^ 1349*4dc78e53SAndroid Build Coastguard Worker-------- 1350*4dc78e53SAndroid Build Coastguard Worker 1351*4dc78e53SAndroid Build Coastguard WorkerThe payload may consist of arbitrary data but may have strict alignment 1352*4dc78e53SAndroid Build Coastguard Workerand formatting rules depending on the actual netlink protocol. 1353*4dc78e53SAndroid Build Coastguard Worker 1354*4dc78e53SAndroid Build Coastguard Worker[[core_msg_attr]] 1355*4dc78e53SAndroid Build Coastguard Worker.Message Attributes 1356*4dc78e53SAndroid Build Coastguard Worker 1357*4dc78e53SAndroid Build Coastguard WorkerMost netlink protocols use netlink attributes. It not only makes the 1358*4dc78e53SAndroid Build Coastguard Workerprotocol self documenting but also gives flexibility in expanding the 1359*4dc78e53SAndroid Build Coastguard Workerprotocol at a later point. New attributes can be added at any time and 1360*4dc78e53SAndroid Build Coastguard Workerolder attributes can be obsoleted by newer ones without breaking 1361*4dc78e53SAndroid Build Coastguard Workerbinary compatibility of the protocol. 1362*4dc78e53SAndroid Build Coastguard Worker 1363*4dc78e53SAndroid Build Coastguard Worker[source,c] 1364*4dc78e53SAndroid Build Coastguard Worker-------- 1365*4dc78e53SAndroid Build Coastguard Worker <---------------------- payload -------------------------> 1366*4dc78e53SAndroid Build Coastguard Worker <----- hdrlen ----> <- nlmsg_attrlen(nlh, hdrlen) -> 1367*4dc78e53SAndroid Build Coastguard Worker +-------------------+- - -+----- ------------+- - -+--------------------------------+- - -+ 1368*4dc78e53SAndroid Build Coastguard Worker | struct nlmsghdr | Pad | Protocol Header | Pad | Attributes | Pad | 1369*4dc78e53SAndroid Build Coastguard Worker +-------------------+- - -+-------------------+- - -+--------------------------------+- - -+ 1370*4dc78e53SAndroid Build Coastguard Workernlmsg_attrdata(nlh, hdrlen) -----------------------------^ 1371*4dc78e53SAndroid Build Coastguard Worker-------- 1372*4dc78e53SAndroid Build Coastguard Worker 1373*4dc78e53SAndroid Build Coastguard WorkerThe function nlmsg_attrdata() returns a pointer to the begin of the 1374*4dc78e53SAndroid Build Coastguard Workerattributes section. The length of the attributes section is returned 1375*4dc78e53SAndroid Build Coastguard Workerby the function nlmsg_attrlen(). 1376*4dc78e53SAndroid Build Coastguard Worker 1377*4dc78e53SAndroid Build Coastguard Worker[source,c] 1378*4dc78e53SAndroid Build Coastguard Worker-------- 1379*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1380*4dc78e53SAndroid Build Coastguard Worker 1381*4dc78e53SAndroid Build Coastguard Workerstruct nlattr *nlmsg_attrdata(const struct nlmsghdr *hdr, int hdrlen); 1382*4dc78e53SAndroid Build Coastguard Workerint nlmsg_attrlen(const struct nlmsghdr *hdr, int hdrlen); 1383*4dc78e53SAndroid Build Coastguard Worker-------- 1384*4dc78e53SAndroid Build Coastguard Worker 1385*4dc78e53SAndroid Build Coastguard WorkerSee <<core_attr>> for more information on how to use netlink attributes. 1386*4dc78e53SAndroid Build Coastguard Worker 1387*4dc78e53SAndroid Build Coastguard Worker.Parsing a Message the Easy Way 1388*4dc78e53SAndroid Build Coastguard Worker 1389*4dc78e53SAndroid Build Coastguard WorkerThe function nlmsg_parse() validate a complete netlink message in one 1390*4dc78e53SAndroid Build Coastguard Workerstep. If +hdrlen > 0+ it will first call nlmsg_valid_hdr() to check 1391*4dc78e53SAndroid Build Coastguard Workerif the protocol header fits into the message. If there is more payload 1392*4dc78e53SAndroid Build Coastguard Workerto parse, it will assume it to be attributes and parse the payload 1393*4dc78e53SAndroid Build Coastguard Workeraccordingly. The function behaves exactly like nla_parse() when 1394*4dc78e53SAndroid Build Coastguard Workerparsing attributes, see <<core_attr_parse_easy>>. 1395*4dc78e53SAndroid Build Coastguard Worker 1396*4dc78e53SAndroid Build Coastguard Worker[source,c] 1397*4dc78e53SAndroid Build Coastguard Worker-------- 1398*4dc78e53SAndroid Build Coastguard Workerint nlmsg_parse(struct nlmsghdr *hdr, int hdrlen, struct nlattr **attrs, 1399*4dc78e53SAndroid Build Coastguard Worker int maxtype, struct nla_policy *policy); 1400*4dc78e53SAndroid Build Coastguard Worker-------- 1401*4dc78e53SAndroid Build Coastguard Worker 1402*4dc78e53SAndroid Build Coastguard WorkerThe function nlmsg_validate() is based on nla_validate() and behaves 1403*4dc78e53SAndroid Build Coastguard Workerexactly the same as nlmsg_parse() except that it only validates and 1404*4dc78e53SAndroid Build Coastguard Workerwill not fill anarray with pointers to each attribute. 1405*4dc78e53SAndroid Build Coastguard Worker 1406*4dc78e53SAndroid Build Coastguard Worker[source,c] 1407*4dc78e53SAndroid Build Coastguard Worker-------- 1408*4dc78e53SAndroid Build Coastguard Workerint nlmsg_validate(struct nlmsghdr *hdr, int hdrlen, intmaxtype, 1409*4dc78e53SAndroid Build Coastguard Worker struct nla_policy *policy); 1410*4dc78e53SAndroid Build Coastguard Worker-------- 1411*4dc78e53SAndroid Build Coastguard Worker 1412*4dc78e53SAndroid Build Coastguard WorkerSee <<core_attr_parse_easy>> for an example and more information on 1413*4dc78e53SAndroid Build Coastguard Workerattribute parsing. 1414*4dc78e53SAndroid Build Coastguard Worker 1415*4dc78e53SAndroid Build Coastguard Worker=== Construction of a Message 1416*4dc78e53SAndroid Build Coastguard Worker 1417*4dc78e53SAndroid Build Coastguard WorkerSee <<core_msg_format>> for information on the netlink message format 1418*4dc78e53SAndroid Build Coastguard Workerand alignment requirements. 1419*4dc78e53SAndroid Build Coastguard Worker 1420*4dc78e53SAndroid Build Coastguard WorkerMessage construction is based on struct nl_msg which uses an internal 1421*4dc78e53SAndroid Build Coastguard Workerbuffer to store the actual netlink message. struct nl_msg +does not+ 1422*4dc78e53SAndroid Build Coastguard Workerpoint to the netlink message header. Use nlmsg_hdr() to retrieve a 1423*4dc78e53SAndroid Build Coastguard Workerpointer to the netlink message header. 1424*4dc78e53SAndroid Build Coastguard Worker 1425*4dc78e53SAndroid Build Coastguard WorkerAt allocation time, a maximum message size is specified. It defaults 1426*4dc78e53SAndroid Build Coastguard Workerto a page (PAGE_SIZE). The application constructing the message will 1427*4dc78e53SAndroid Build Coastguard Workerreserve space out of this maximum message size repeatedly for each 1428*4dc78e53SAndroid Build Coastguard Workerheader or attribute added. This allows construction of messages across 1429*4dc78e53SAndroid Build Coastguard Workervarious layers of code where lower layers do not need to know about 1430*4dc78e53SAndroid Build Coastguard Workerthe space requirements of upper layers. 1431*4dc78e53SAndroid Build Coastguard Worker 1432*4dc78e53SAndroid Build Coastguard Worker+Why is setting the maximum message size necessary?+ This 1433*4dc78e53SAndroid Build Coastguard Workerquestion is often raised in combination with the proposed solution of 1434*4dc78e53SAndroid Build Coastguard Workerreallocating the message payload buffer on the fly using realloc(). 1435*4dc78e53SAndroid Build Coastguard WorkerWhile it is possible to reallocate the buffer during construction 1436*4dc78e53SAndroid Build Coastguard Workerusing nlmsg_expand() it will make all pointers into the message buffer 1437*4dc78e53SAndroid Build Coastguard Workerbecome stale. This breaks usage of nlmsg_hdr(), nla_nest_start(), and 1438*4dc78e53SAndroid Build Coastguard Workernla_nest_end() and is therefore not acceptable as default behaviour. 1439*4dc78e53SAndroid Build Coastguard Worker 1440*4dc78e53SAndroid Build Coastguard Worker.Allocating struct nl_msg 1441*4dc78e53SAndroid Build Coastguard Worker 1442*4dc78e53SAndroid Build Coastguard WorkerThe first step in constructing a new netlink message it to allocate a 1443*4dc78e53SAndroid Build Coastguard Worker`struct nl_msg` to hold the message header and payload. Several 1444*4dc78e53SAndroid Build Coastguard Workerfunctions exist to simplify various tasks. 1445*4dc78e53SAndroid Build Coastguard Worker 1446*4dc78e53SAndroid Build Coastguard Worker[source,c] 1447*4dc78e53SAndroid Build Coastguard Worker-------- 1448*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1449*4dc78e53SAndroid Build Coastguard Worker 1450*4dc78e53SAndroid Build Coastguard Workerstruct nl_msg *nlmsg_alloc(void); 1451*4dc78e53SAndroid Build Coastguard Workervoid nlmsg_free(struct nl_msg *msg); 1452*4dc78e53SAndroid Build Coastguard Worker-------- 1453*4dc78e53SAndroid Build Coastguard Worker 1454*4dc78e53SAndroid Build Coastguard WorkerThe function nlmsg_alloc() is the default message allocation function. 1455*4dc78e53SAndroid Build Coastguard WorkerIt allocates a new message using the default maximum message size which 1456*4dc78e53SAndroid Build Coastguard Workerequals to one page (PAGE_SIZE). The application can change the default 1457*4dc78e53SAndroid Build Coastguard Workersize for messages by calling nlmsg_set_default_size(): 1458*4dc78e53SAndroid Build Coastguard Worker 1459*4dc78e53SAndroid Build Coastguard Worker[source,c] 1460*4dc78e53SAndroid Build Coastguard Worker-------- 1461*4dc78e53SAndroid Build Coastguard Workervoid nlmsg_set_default_size(size_t); 1462*4dc78e53SAndroid Build Coastguard Worker-------- 1463*4dc78e53SAndroid Build Coastguard Worker 1464*4dc78e53SAndroid Build Coastguard WorkerCAUTION: Calling nlmsg_set_default_size() does not change the maximum 1465*4dc78e53SAndroid Build Coastguard Worker message size of already allocated messages. 1466*4dc78e53SAndroid Build Coastguard Worker 1467*4dc78e53SAndroid Build Coastguard Worker[source,c] 1468*4dc78e53SAndroid Build Coastguard Worker-------- 1469*4dc78e53SAndroid Build Coastguard Workerstruct nl_msg *nlmsg_alloc_size(size_t max); 1470*4dc78e53SAndroid Build Coastguard Worker-------- 1471*4dc78e53SAndroid Build Coastguard Worker 1472*4dc78e53SAndroid Build Coastguard WorkerInstead of changing the default message size, the function 1473*4dc78e53SAndroid Build Coastguard Workernlmsg_alloc_size() can be used to allocate a message with an individual 1474*4dc78e53SAndroid Build Coastguard Workermaximum message size. 1475*4dc78e53SAndroid Build Coastguard Worker 1476*4dc78e53SAndroid Build Coastguard Worker 1477*4dc78e53SAndroid Build Coastguard WorkerIf the netlink message header is already known at allocation time, the 1478*4dc78e53SAndroid Build Coastguard Workerapplication may sue nlmsg_inherit(). It will allocate a message using 1479*4dc78e53SAndroid Build Coastguard Workerthe default maximum message size and copy the header into the message. 1480*4dc78e53SAndroid Build Coastguard WorkerCalling nlmsg_inherit with +set+ to NULL is equivalent to calling 1481*4dc78e53SAndroid Build Coastguard Workernlmsg_alloc(). 1482*4dc78e53SAndroid Build Coastguard Worker 1483*4dc78e53SAndroid Build Coastguard Worker[source,c] 1484*4dc78e53SAndroid Build Coastguard Worker-------- 1485*4dc78e53SAndroid Build Coastguard Workerstruct nl_msg *nlmsg_inherit(struct nlmsghdr *hdr); 1486*4dc78e53SAndroid Build Coastguard Worker-------- 1487*4dc78e53SAndroid Build Coastguard Worker 1488*4dc78e53SAndroid Build Coastguard WorkerAlternatively nlmsg_alloc_simple() takes a netlink message type and 1489*4dc78e53SAndroid Build Coastguard Workernetlink message flags. It is equivalent to nlmsg_inherit() except that it 1490*4dc78e53SAndroid Build Coastguard Workertakes the two common header fields as arguments instead of a complete 1491*4dc78e53SAndroid Build Coastguard Workerheader. 1492*4dc78e53SAndroid Build Coastguard Worker 1493*4dc78e53SAndroid Build Coastguard Worker[source,c] 1494*4dc78e53SAndroid Build Coastguard Worker-------- 1495*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1496*4dc78e53SAndroid Build Coastguard Worker 1497*4dc78e53SAndroid Build Coastguard Workerstruct nl_msg *nlmsg_alloc_simple(int nlmsg_type, int flags); 1498*4dc78e53SAndroid Build Coastguard Worker-------- 1499*4dc78e53SAndroid Build Coastguard Worker 1500*4dc78e53SAndroid Build Coastguard Worker.Appending the netlink message header 1501*4dc78e53SAndroid Build Coastguard Worker 1502*4dc78e53SAndroid Build Coastguard WorkerAfter allocating struct nl_msg, the netlink message header needs to be 1503*4dc78e53SAndroid Build Coastguard Workeradded unless one of the function nlmsg_alloc_simple() or nlmsg_inherit() 1504*4dc78e53SAndroid Build Coastguard Workerhave been used for allocation in which case this step will replace the 1505*4dc78e53SAndroid Build Coastguard Workernetlink message header already in place. 1506*4dc78e53SAndroid Build Coastguard Worker 1507*4dc78e53SAndroid Build Coastguard Worker[source,c] 1508*4dc78e53SAndroid Build Coastguard Worker-------- 1509*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1510*4dc78e53SAndroid Build Coastguard Worker 1511*4dc78e53SAndroid Build Coastguard Workerstruct nlmsghdr *nlmsg_put(struct nl_msg *msg, uint32_t port, uint32_t seqnr, 1512*4dc78e53SAndroid Build Coastguard Worker int nlmsg_type, int payload, int nlmsg_flags); 1513*4dc78e53SAndroid Build Coastguard Worker-------- 1514*4dc78e53SAndroid Build Coastguard Worker 1515*4dc78e53SAndroid Build Coastguard WorkerThe function nlmsg_put() will build a netlink message header out of 1516*4dc78e53SAndroid Build Coastguard Worker+nlmsg_type+, +nlmsg_flags+, +seqnr+, and +port+ and copy it into the 1517*4dc78e53SAndroid Build Coastguard Workernetlink message. +seqnr+ can be set to +NL_AUTO_SEQ+ to indicate 1518*4dc78e53SAndroid Build Coastguard Workerthat the next possible sequence number should be used automatically. 1519*4dc78e53SAndroid Build Coastguard WorkerTo use this feature, the message must be sent using the function 1520*4dc78e53SAndroid Build Coastguard Workernl_send_auto(). Like +port+, the argument +seqnr+ can be set to 1521*4dc78e53SAndroid Build Coastguard Worker+NL_AUTO_PORT+ indicating that the local port assigned to the socket 1522*4dc78e53SAndroid Build Coastguard Workershould be used as source port. This is generally a good idea unless 1523*4dc78e53SAndroid Build Coastguard Workeryou are replying to a request. See <<core_netlink_fundamentals>> 1524*4dc78e53SAndroid Build Coastguard Workerfor more information on how to fill the header. 1525*4dc78e53SAndroid Build Coastguard Worker 1526*4dc78e53SAndroid Build Coastguard WorkerNOTE: The argument +payload+ can be used by the application to reserve 1527*4dc78e53SAndroid Build Coastguard Worker room for additional data after the header. A value of > 0 is 1528*4dc78e53SAndroid Build Coastguard Worker equivalent to calling +nlmsg_reserve(msg, payload, NLMSG_ALIGNTO)+. 1529*4dc78e53SAndroid Build Coastguard Worker See <<core_msg_reserve>> for more information on reserving room for 1530*4dc78e53SAndroid Build Coastguard Worker data. 1531*4dc78e53SAndroid Build Coastguard Worker 1532*4dc78e53SAndroid Build Coastguard Worker.Example 1533*4dc78e53SAndroid Build Coastguard Worker[source,c] 1534*4dc78e53SAndroid Build Coastguard Worker-------- 1535*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1536*4dc78e53SAndroid Build Coastguard Worker 1537*4dc78e53SAndroid Build Coastguard Workerstruct nlmsghdr *hdr; 1538*4dc78e53SAndroid Build Coastguard Workerstruct nl_msg *msg; 1539*4dc78e53SAndroid Build Coastguard Workerstruct myhdr { 1540*4dc78e53SAndroid Build Coastguard Worker uint32_t foo1, foo2; 1541*4dc78e53SAndroid Build Coastguard Worker} shdr = { 10, 20 }; 1542*4dc78e53SAndroid Build Coastguard Worker 1543*4dc78e53SAndroid Build Coastguard Worker/* Allocate a message with the default maximum message size */ 1544*4dc78e53SAndroid Build Coastguard Workermsg = nlmsg_alloc(); 1545*4dc78e53SAndroid Build Coastguard Worker 1546*4dc78e53SAndroid Build Coastguard Worker/* 1547*4dc78e53SAndroid Build Coastguard Worker * Add header with message type MY_MSGTYPE, the flag NLM_F_CREATE, 1548*4dc78e53SAndroid Build Coastguard Worker * let library fill port and sequence number, and reserve room for 1549*4dc78e53SAndroid Build Coastguard Worker * struct myhdr 1550*4dc78e53SAndroid Build Coastguard Worker */ 1551*4dc78e53SAndroid Build Coastguard Workerhdr = nlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, MY_MSGTYPE, sizeof(shdr), NLM_F_CREATE); 1552*4dc78e53SAndroid Build Coastguard Worker 1553*4dc78e53SAndroid Build Coastguard Worker/* Copy own header into newly reserved payload section */ 1554*4dc78e53SAndroid Build Coastguard Workermemcpy(nlmsg_data(hdr), &shdr, sizeof(shdr)); 1555*4dc78e53SAndroid Build Coastguard Worker 1556*4dc78e53SAndroid Build Coastguard Worker/* 1557*4dc78e53SAndroid Build Coastguard Worker * The message will now look like this: 1558*4dc78e53SAndroid Build Coastguard Worker * +-------------------+- - -+----------------+- - -+ 1559*4dc78e53SAndroid Build Coastguard Worker * | struct nlmsghdr | Pad | struct myhdr | Pad | 1560*4dc78e53SAndroid Build Coastguard Worker * +-------------------+-----+----------------+- - -+ 1561*4dc78e53SAndroid Build Coastguard Worker * nlh -^ / \ 1562*4dc78e53SAndroid Build Coastguard Worker * +--------+---------+ 1563*4dc78e53SAndroid Build Coastguard Worker * | foo1 | foo2 | 1564*4dc78e53SAndroid Build Coastguard Worker * +--------+---------+ 1565*4dc78e53SAndroid Build Coastguard Worker */ 1566*4dc78e53SAndroid Build Coastguard Worker-------- 1567*4dc78e53SAndroid Build Coastguard Worker 1568*4dc78e53SAndroid Build Coastguard Worker[[core_msg_reserve]] 1569*4dc78e53SAndroid Build Coastguard Worker.Reserving room at the end of the message 1570*4dc78e53SAndroid Build Coastguard Worker 1571*4dc78e53SAndroid Build Coastguard WorkerMost functions described later on will automatically take care of 1572*4dc78e53SAndroid Build Coastguard Workerreserving room for the data that is added to the end of the netlink 1573*4dc78e53SAndroid Build Coastguard Workermessage. In some situations it may be required for the application 1574*4dc78e53SAndroid Build Coastguard Workerto reserve room directly though. 1575*4dc78e53SAndroid Build Coastguard Worker 1576*4dc78e53SAndroid Build Coastguard Worker[source,c] 1577*4dc78e53SAndroid Build Coastguard Worker-------- 1578*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1579*4dc78e53SAndroid Build Coastguard Worker 1580*4dc78e53SAndroid Build Coastguard Workervoid *nlmsg_reserve(struct nl_msg *msg, size_t len, int pad); 1581*4dc78e53SAndroid Build Coastguard Worker-------- 1582*4dc78e53SAndroid Build Coastguard Worker 1583*4dc78e53SAndroid Build Coastguard WorkerThe function nlmsg_reserve() reserves +len+ bytes at the end of the 1584*4dc78e53SAndroid Build Coastguard Workernetlink message and returns a pointer to the start of the reserved area. 1585*4dc78e53SAndroid Build Coastguard WorkerThe +pad+ argument can be used to request +len+ to be aligned to any 1586*4dc78e53SAndroid Build Coastguard Workernumber of bytes prior to reservation. 1587*4dc78e53SAndroid Build Coastguard Worker 1588*4dc78e53SAndroid Build Coastguard WorkerThe following example requests to reserve a 17 bytes area at the end of 1589*4dc78e53SAndroid Build Coastguard Workermessage aligned to 4 bytes. Therefore a total of 20 bytes will be 1590*4dc78e53SAndroid Build Coastguard Workerreserved. 1591*4dc78e53SAndroid Build Coastguard Worker 1592*4dc78e53SAndroid Build Coastguard Worker[source,c] 1593*4dc78e53SAndroid Build Coastguard Worker-------- 1594*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1595*4dc78e53SAndroid Build Coastguard Worker 1596*4dc78e53SAndroid Build Coastguard Workervoid *buf = nlmsg_reserve(msg, 17, 4); 1597*4dc78e53SAndroid Build Coastguard Worker-------- 1598*4dc78e53SAndroid Build Coastguard Worker 1599*4dc78e53SAndroid Build Coastguard WorkerNOTE: `nlmsg_reserve()` will *not* align the start of the buffer. Any 1600*4dc78e53SAndroid Build Coastguard Worker alignment requirements must be provided by the owner of the 1601*4dc78e53SAndroid Build Coastguard Worker previous message section. 1602*4dc78e53SAndroid Build Coastguard Worker 1603*4dc78e53SAndroid Build Coastguard Worker.Appending data at the end of the message 1604*4dc78e53SAndroid Build Coastguard Worker 1605*4dc78e53SAndroid Build Coastguard WorkerThe function `nlmsg_append()` appends `len` bytes at the end of the 1606*4dc78e53SAndroid Build Coastguard Workermessage, padding it if requested and necessary. 1607*4dc78e53SAndroid Build Coastguard Worker 1608*4dc78e53SAndroid Build Coastguard Worker[source,c] 1609*4dc78e53SAndroid Build Coastguard Worker-------- 1610*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1611*4dc78e53SAndroid Build Coastguard Worker 1612*4dc78e53SAndroid Build Coastguard Workerint nlmsg_append(struct nl_msg *msg, void *data, size_t len, int pad); 1613*4dc78e53SAndroid Build Coastguard Worker-------- 1614*4dc78e53SAndroid Build Coastguard Worker 1615*4dc78e53SAndroid Build Coastguard WorkerIt is equivalent to calling `nlmsg_reserve()` and `memcpy()`ing the 1616*4dc78e53SAndroid Build Coastguard Workerdata into the freshly reserved data section. 1617*4dc78e53SAndroid Build Coastguard Worker 1618*4dc78e53SAndroid Build Coastguard WorkerNOTE: `nlmsg_append()` will *not* align the start of the data. Any 1619*4dc78e53SAndroid Build Coastguard Worker alignment requirements must be provided by the owner of the 1620*4dc78e53SAndroid Build Coastguard Worker previous message section. 1621*4dc78e53SAndroid Build Coastguard Worker 1622*4dc78e53SAndroid Build Coastguard Worker.Adding attributes to a message 1623*4dc78e53SAndroid Build Coastguard Worker 1624*4dc78e53SAndroid Build Coastguard WorkerConstruction of attributes and addition of attributes to the message is 1625*4dc78e53SAndroid Build Coastguard Workercovered in section <<core_attr>>. 1626*4dc78e53SAndroid Build Coastguard Worker 1627*4dc78e53SAndroid Build Coastguard Worker[[core_attr]] 1628*4dc78e53SAndroid Build Coastguard Worker== Attributes 1629*4dc78e53SAndroid Build Coastguard Worker 1630*4dc78e53SAndroid Build Coastguard WorkerAny form of payload should be encoded as netlink attributes whenever 1631*4dc78e53SAndroid Build Coastguard Workerpossible. Use of attributes allows to extend any netlink protocol in 1632*4dc78e53SAndroid Build Coastguard Workerthe future without breaking binary compatibility. F.e. Suppose your 1633*4dc78e53SAndroid Build Coastguard Workerdevice may currently be using 32 bit counters for statistics but years 1634*4dc78e53SAndroid Build Coastguard Workerlater the device switches to maintaining 64 bit counters to account 1635*4dc78e53SAndroid Build Coastguard Workerfor faster network hardware. If your protocol is using attributes the 1636*4dc78e53SAndroid Build Coastguard Workermove to 64 bit counters is trivial and only involves in sending an 1637*4dc78e53SAndroid Build Coastguard Workeradditional attribute containing the 64 bit variants while still 1638*4dc78e53SAndroid Build Coastguard Workerproviding the old legacy 32 bit counters. If your protocol is not using 1639*4dc78e53SAndroid Build Coastguard Workerattributes you will not be able to switch data types without breaking 1640*4dc78e53SAndroid Build Coastguard Workerall existing users of the protocol. 1641*4dc78e53SAndroid Build Coastguard Worker 1642*4dc78e53SAndroid Build Coastguard WorkerThe concept of nested attributes also allows for subsystems of your 1643*4dc78e53SAndroid Build Coastguard Workerprotocol to implement and maintain their own attribute schemas. Suppose 1644*4dc78e53SAndroid Build Coastguard Workera new generation of network device is introduced which requires a 1645*4dc78e53SAndroid Build Coastguard Workercompletely new set of configuration settings which was unthinkable when 1646*4dc78e53SAndroid Build Coastguard Workerthe netlink protocol was initially designed. Using attributes the new 1647*4dc78e53SAndroid Build Coastguard Workergeneration of devices may define a new attribute and fill it with its 1648*4dc78e53SAndroid Build Coastguard Workerown new structure of attributes which extend or even obsolete the old 1649*4dc78e53SAndroid Build Coastguard Workerattributes. 1650*4dc78e53SAndroid Build Coastguard Worker 1651*4dc78e53SAndroid Build Coastguard WorkerTherefore, _always_ use attributes even if you are almost certain that 1652*4dc78e53SAndroid Build Coastguard Workerthe message format will never ever change in the future. 1653*4dc78e53SAndroid Build Coastguard Worker 1654*4dc78e53SAndroid Build Coastguard Worker[[core_attr_format]] 1655*4dc78e53SAndroid Build Coastguard Worker=== Attribute Format 1656*4dc78e53SAndroid Build Coastguard Worker 1657*4dc78e53SAndroid Build Coastguard WorkerNetlink attributes allow for any number of data chunks of arbitrary 1658*4dc78e53SAndroid Build Coastguard Workerlength to be attached to a netlink message. See <<core_msg_attr>> 1659*4dc78e53SAndroid Build Coastguard Workerfor more information on where attributes are stored in the message. 1660*4dc78e53SAndroid Build Coastguard Worker 1661*4dc78e53SAndroid Build Coastguard WorkerThe format of the attributes data returned by nlmsg_attrdata() is as 1662*4dc78e53SAndroid Build Coastguard Workerfollows: 1663*4dc78e53SAndroid Build Coastguard Worker 1664*4dc78e53SAndroid Build Coastguard Worker[source,c] 1665*4dc78e53SAndroid Build Coastguard Worker-------- 1666*4dc78e53SAndroid Build Coastguard Worker <----------- nla_total_size(payload) -----------> 1667*4dc78e53SAndroid Build Coastguard Worker <---------- nla_size(payload) -----------> 1668*4dc78e53SAndroid Build Coastguard Worker +-----------------+- - -+- - - - - - - - - +- - -+-----------------+- - - 1669*4dc78e53SAndroid Build Coastguard Worker | struct nlattr | Pad | Payload | Pad | struct nlattr | 1670*4dc78e53SAndroid Build Coastguard Worker +-----------------+- - -+- - - - - - - - - +- - -+-----------------+- - - 1671*4dc78e53SAndroid Build Coastguard Worker <---- NLA_HDRLEN -----> <--- NLA_ALIGN(len) ---> <---- NLA_HDRLEN --- 1672*4dc78e53SAndroid Build Coastguard Worker-------- 1673*4dc78e53SAndroid Build Coastguard Worker 1674*4dc78e53SAndroid Build Coastguard WorkerEvery attribute must start at an offset which is a multiple of 1675*4dc78e53SAndroid Build Coastguard Worker+NLA_ALIGNTO+ (4 bytes). If you need to know whether an attribute needs 1676*4dc78e53SAndroid Build Coastguard Workerto be padded at the end, the function nla_padlen() returns the number 1677*4dc78e53SAndroid Build Coastguard Workerof padding bytes that will or need to be added. 1678*4dc78e53SAndroid Build Coastguard Worker 1679*4dc78e53SAndroid Build Coastguard Workerimage:attribute_hdr.png["Netlink Attribute Header"] 1680*4dc78e53SAndroid Build Coastguard Worker 1681*4dc78e53SAndroid Build Coastguard WorkerEvery attribute is encoded with a type and length field, both 16 bits, 1682*4dc78e53SAndroid Build Coastguard Workerstored in the attribute header (struct nlattr) preceding the attribute 1683*4dc78e53SAndroid Build Coastguard Workerpayload. The length of an attribute is used to calculate the offset to 1684*4dc78e53SAndroid Build Coastguard Workerthe next attribute. 1685*4dc78e53SAndroid Build Coastguard Worker 1686*4dc78e53SAndroid Build Coastguard Worker[[core_attr_parse]] 1687*4dc78e53SAndroid Build Coastguard Worker=== Parsing Attributes 1688*4dc78e53SAndroid Build Coastguard Worker 1689*4dc78e53SAndroid Build Coastguard Worker[[core_attr_parse_split]] 1690*4dc78e53SAndroid Build Coastguard Worker.Splitting an Attributes Stream into Attributes 1691*4dc78e53SAndroid Build Coastguard Worker 1692*4dc78e53SAndroid Build Coastguard WorkerAlthough most applications will use one of the functions from the 1693*4dc78e53SAndroid Build Coastguard Workernlmsg_parse() family (See <<core_attr_parse_easy>>) an interface exists 1694*4dc78e53SAndroid Build Coastguard Workerto split the attributes stream manually. 1695*4dc78e53SAndroid Build Coastguard Worker 1696*4dc78e53SAndroid Build Coastguard WorkerAs described in <<core_attr_format>> the attributes section contains a 1697*4dc78e53SAndroid Build Coastguard Workerinfinite sequence or stream of attributes. The pointer returned by 1698*4dc78e53SAndroid Build Coastguard Workernlmsg_attrdata() (See <<core_msg_attr>>) points to the first attribute 1699*4dc78e53SAndroid Build Coastguard Workerheader. Any subsequent attribute is accessed with the function nla_next() 1700*4dc78e53SAndroid Build Coastguard Workerbased on the previous header. 1701*4dc78e53SAndroid Build Coastguard Worker 1702*4dc78e53SAndroid Build Coastguard Worker[source,c] 1703*4dc78e53SAndroid Build Coastguard Worker-------- 1704*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1705*4dc78e53SAndroid Build Coastguard Worker 1706*4dc78e53SAndroid Build Coastguard Workerstruct nlattr *nla_next(const struct nlattr *attr, int *remaining); 1707*4dc78e53SAndroid Build Coastguard Worker-------- 1708*4dc78e53SAndroid Build Coastguard Worker 1709*4dc78e53SAndroid Build Coastguard WorkerThe semantics are equivalent to nlmsg_next() and thus nla_next() will also 1710*4dc78e53SAndroid Build Coastguard Workersubtract the size of the previous attribute from the remaining number of 1711*4dc78e53SAndroid Build Coastguard Workerbytes in the attributes stream. 1712*4dc78e53SAndroid Build Coastguard Worker 1713*4dc78e53SAndroid Build Coastguard WorkerLike messages, attributes do not contain an indicator whether another 1714*4dc78e53SAndroid Build Coastguard Workerattribute follows or not. The only indication is the number of bytes left 1715*4dc78e53SAndroid Build Coastguard Workerin the attribute stream. The function nla_ok() exists to determine whether 1716*4dc78e53SAndroid Build Coastguard Workeranother attribute fits into the remaining number of bytes or not. 1717*4dc78e53SAndroid Build Coastguard Worker 1718*4dc78e53SAndroid Build Coastguard Worker[source,c] 1719*4dc78e53SAndroid Build Coastguard Worker-------- 1720*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1721*4dc78e53SAndroid Build Coastguard Worker 1722*4dc78e53SAndroid Build Coastguard Workerint nla_ok(const struct nlattr *attr, int remaining); 1723*4dc78e53SAndroid Build Coastguard Worker-------- 1724*4dc78e53SAndroid Build Coastguard Worker 1725*4dc78e53SAndroid Build Coastguard WorkerA typical use of nla_ok() and nla_next() looks like this: 1726*4dc78e53SAndroid Build Coastguard Worker 1727*4dc78e53SAndroid Build Coastguard Worker.nla_ok()/nla_next() usage 1728*4dc78e53SAndroid Build Coastguard Worker[source,c] 1729*4dc78e53SAndroid Build Coastguard Worker-------- 1730*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1731*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1732*4dc78e53SAndroid Build Coastguard Worker 1733*4dc78e53SAndroid Build Coastguard Workerstruct nlattr *hdr = nlmsg_attrdata(msg, 0); 1734*4dc78e53SAndroid Build Coastguard Workerint remaining = nlmsg_attrlen(msg, 0); 1735*4dc78e53SAndroid Build Coastguard Worker 1736*4dc78e53SAndroid Build Coastguard Workerwhile (nla_ok(hdr, remaining)) { 1737*4dc78e53SAndroid Build Coastguard Worker /* parse attribute here */ 1738*4dc78e53SAndroid Build Coastguard Worker hdr = nla_next(hdr, &remaining); 1739*4dc78e53SAndroid Build Coastguard Worker}; 1740*4dc78e53SAndroid Build Coastguard Worker-------- 1741*4dc78e53SAndroid Build Coastguard Worker 1742*4dc78e53SAndroid Build Coastguard WorkerNOTE: `nla_ok()` only returns true if the *complete* attributes 1743*4dc78e53SAndroid Build Coastguard Worker including the attribute payload fits into the remaining number 1744*4dc78e53SAndroid Build Coastguard Worker of bytes. 1745*4dc78e53SAndroid Build Coastguard Worker 1746*4dc78e53SAndroid Build Coastguard Worker.Accessing Attribute Header and Payload 1747*4dc78e53SAndroid Build Coastguard Worker 1748*4dc78e53SAndroid Build Coastguard WorkerOnce the individual attributes have been sorted out by either splitting 1749*4dc78e53SAndroid Build Coastguard Workerthe attributes stream or using another interface the attribute header 1750*4dc78e53SAndroid Build Coastguard Workerand payload can be accessed. 1751*4dc78e53SAndroid Build Coastguard Worker 1752*4dc78e53SAndroid Build Coastguard Worker[source,c] 1753*4dc78e53SAndroid Build Coastguard Worker-------- 1754*4dc78e53SAndroid Build Coastguard Worker <- nla_len(hdr) -> 1755*4dc78e53SAndroid Build Coastguard Worker +-----------------+- - -+- - - - - - - - - +- - -+ 1756*4dc78e53SAndroid Build Coastguard Worker | struct nlattr | Pad | Payload | Pad | 1757*4dc78e53SAndroid Build Coastguard Worker +-----------------+- - -+- - - - - - - - - +- - -+ 1758*4dc78e53SAndroid Build Coastguard Workernla_data(hdr) ---------------^ 1759*4dc78e53SAndroid Build Coastguard Worker-------- 1760*4dc78e53SAndroid Build Coastguard Worker 1761*4dc78e53SAndroid Build Coastguard WorkerThe functions nla_len() and nla_type() can be used to access the attribute 1762*4dc78e53SAndroid Build Coastguard Workerheader. nla_len() will return the length of the payload not including 1763*4dc78e53SAndroid Build Coastguard Workereventual padding bytes. nla_type returns the attribute type. 1764*4dc78e53SAndroid Build Coastguard Worker 1765*4dc78e53SAndroid Build Coastguard Worker[source,c] 1766*4dc78e53SAndroid Build Coastguard Worker-------- 1767*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1768*4dc78e53SAndroid Build Coastguard Worker 1769*4dc78e53SAndroid Build Coastguard Workerint nla_len(const struct nlattr *hdr); 1770*4dc78e53SAndroid Build Coastguard Workerint nla_type(const struct nlattr *hdr); 1771*4dc78e53SAndroid Build Coastguard Worker-------- 1772*4dc78e53SAndroid Build Coastguard Worker 1773*4dc78e53SAndroid Build Coastguard WorkerThe function nla_data() will return a pointer to the attribute 1774*4dc78e53SAndroid Build Coastguard Workerpayload. Please note that due to +NLA_ALIGNTO+ being 4 bytes it may 1775*4dc78e53SAndroid Build Coastguard Workernot be safe to cast and dereference the pointer for any datatype 1776*4dc78e53SAndroid Build Coastguard Workerlarger than 32 bit depending on the architecture the application is 1777*4dc78e53SAndroid Build Coastguard Workerrun on. 1778*4dc78e53SAndroid Build Coastguard Worker 1779*4dc78e53SAndroid Build Coastguard Worker[source,c] 1780*4dc78e53SAndroid Build Coastguard Worker-------- 1781*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1782*4dc78e53SAndroid Build Coastguard Worker 1783*4dc78e53SAndroid Build Coastguard Workervoid *nla_data(const struct nlattr *hdr); 1784*4dc78e53SAndroid Build Coastguard Worker-------- 1785*4dc78e53SAndroid Build Coastguard Worker 1786*4dc78e53SAndroid Build Coastguard Worker[NOTE] 1787*4dc78e53SAndroid Build Coastguard WorkerNever rely on the size of a payload being what you expect it to be. 1788*4dc78e53SAndroid Build Coastguard Worker_Always_ verify the payload size and make sure that it matches your 1789*4dc78e53SAndroid Build Coastguard Workerexpectations. See <<core_attr_validation>> 1790*4dc78e53SAndroid Build Coastguard Worker 1791*4dc78e53SAndroid Build Coastguard Worker[[core_attr_validation]] 1792*4dc78e53SAndroid Build Coastguard Worker.Attribute Validation 1793*4dc78e53SAndroid Build Coastguard Worker 1794*4dc78e53SAndroid Build Coastguard WorkerWhen receiving netlink attributes, the receiver has certain expectations 1795*4dc78e53SAndroid Build Coastguard Workeron how the attributes should look like. These expectations must be 1796*4dc78e53SAndroid Build Coastguard Workerdefined to make sure the sending side meets our expectations. For this 1797*4dc78e53SAndroid Build Coastguard Workerpurpose, an attribute validation interface exists which must be used 1798*4dc78e53SAndroid Build Coastguard Workerprior to accessing any payload. 1799*4dc78e53SAndroid Build Coastguard Worker 1800*4dc78e53SAndroid Build Coastguard WorkerAll functions providing attribute validation functionality are based 1801*4dc78e53SAndroid Build Coastguard Workeron struct nla_policy: 1802*4dc78e53SAndroid Build Coastguard Worker 1803*4dc78e53SAndroid Build Coastguard Worker[source,c] 1804*4dc78e53SAndroid Build Coastguard Worker-------- 1805*4dc78e53SAndroid Build Coastguard Workerstruct nla_policy { 1806*4dc78e53SAndroid Build Coastguard Worker uint16_t type; 1807*4dc78e53SAndroid Build Coastguard Worker uint16_t minlen; 1808*4dc78e53SAndroid Build Coastguard Worker uint16_t maxlen; 1809*4dc78e53SAndroid Build Coastguard Worker}; 1810*4dc78e53SAndroid Build Coastguard Worker-------- 1811*4dc78e53SAndroid Build Coastguard Worker 1812*4dc78e53SAndroid Build Coastguard WorkerThe +type+ member specifies the datatype of the attribute, e.g. 1813*4dc78e53SAndroid Build Coastguard Worker+NLA_U32+, +NLA_STRING+, +NLA_FLAG+. The default is +NLA_UNSPEC+. The 1814*4dc78e53SAndroid Build Coastguard Worker+minlen+ member defines the minmum payload length of an attribute to 1815*4dc78e53SAndroid Build Coastguard Workerbe considered a valid attribute. The value for +minlen+ is implicit 1816*4dc78e53SAndroid Build Coastguard Workerfor most basic datatypes such as integers or flags. The +maxlen+ 1817*4dc78e53SAndroid Build Coastguard Workermember can be used to define a maximum payload length for an 1818*4dc78e53SAndroid Build Coastguard Workerattribute to still be considered valid. 1819*4dc78e53SAndroid Build Coastguard Worker 1820*4dc78e53SAndroid Build Coastguard WorkerNOTE: Specifying a maximum payload length is not recommended when 1821*4dc78e53SAndroid Build Coastguard Worker encoding structures in an attribute as it will prevent any 1822*4dc78e53SAndroid Build Coastguard Worker extension of the structure in the future. Something that is 1823*4dc78e53SAndroid Build Coastguard Worker frequently done in netlink protocols and does not break 1824*4dc78e53SAndroid Build Coastguard Worker backwards compatibility. 1825*4dc78e53SAndroid Build Coastguard Worker 1826*4dc78e53SAndroid Build Coastguard WorkerOne of the functions which use struct nla_policy is nla_validate(). 1827*4dc78e53SAndroid Build Coastguard WorkerThe function expects an array of struct nla_policy and will access the 1828*4dc78e53SAndroid Build Coastguard Workerarray using the attribute type as index. If an attribute type is out 1829*4dc78e53SAndroid Build Coastguard Workerof bounds the attribute is assumed to be valid. This is intentional 1830*4dc78e53SAndroid Build Coastguard Workerbehaviour to allow older applications not yet aware of recently 1831*4dc78e53SAndroid Build Coastguard Workerintroduced attributes to continue functioning. 1832*4dc78e53SAndroid Build Coastguard Worker 1833*4dc78e53SAndroid Build Coastguard Worker[source,c] 1834*4dc78e53SAndroid Build Coastguard Worker-------- 1835*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1836*4dc78e53SAndroid Build Coastguard Worker 1837*4dc78e53SAndroid Build Coastguard Workerint nla_validate(struct nlattr *head, int len, int maxtype, struct nla_policy *policy); 1838*4dc78e53SAndroid Build Coastguard Worker-------- 1839*4dc78e53SAndroid Build Coastguard Worker 1840*4dc78e53SAndroid Build Coastguard WorkerThe function nla_validate() returns 0 if all attributes are valid, 1841*4dc78e53SAndroid Build Coastguard Workerotherwise a validation failure specific error code is returned. 1842*4dc78e53SAndroid Build Coastguard Worker 1843*4dc78e53SAndroid Build Coastguard WorkerMost applications will rarely use nla_validate() directly but use 1844*4dc78e53SAndroid Build Coastguard Workernla_parse() instead which takes care of validation in the same way but 1845*4dc78e53SAndroid Build Coastguard Workeralso parses the the attributes in the same step. See 1846*4dc78e53SAndroid Build Coastguard Worker<<core_attr_parse_easy>> for an example and more information. 1847*4dc78e53SAndroid Build Coastguard Worker 1848*4dc78e53SAndroid Build Coastguard WorkerThe validation process in detail: 1849*4dc78e53SAndroid Build Coastguard Worker 1850*4dc78e53SAndroid Build Coastguard Worker. If attribute type is 0 or exceeds +maxtype+ attribute is 1851*4dc78e53SAndroid Build Coastguard Worker considered valid, 0 is returned. 1852*4dc78e53SAndroid Build Coastguard Worker. If payload length is < +minlen+, +-NLE_ERANGE+ is returned. 1853*4dc78e53SAndroid Build Coastguard Worker. If +maxlen+ is defined and payload exceeds it, +-NLE_ERANGE+ 1854*4dc78e53SAndroid Build Coastguard Worker is returned. 1855*4dc78e53SAndroid Build Coastguard Worker. Datatype specific requirements rules, see <<core_attr_types>> 1856*4dc78e53SAndroid Build Coastguard Worker. If all is ok, 0 is returned. 1857*4dc78e53SAndroid Build Coastguard Worker 1858*4dc78e53SAndroid Build Coastguard Worker[[core_attr_parse_easy]] 1859*4dc78e53SAndroid Build Coastguard Worker.Parsing Attributes the Easy Way 1860*4dc78e53SAndroid Build Coastguard Worker 1861*4dc78e53SAndroid Build Coastguard WorkerMost applications will not want to deal with splitting attribute 1862*4dc78e53SAndroid Build Coastguard Workerstreams themselves as described in <<core_attr_parse_split>> 1863*4dc78e53SAndroid Build Coastguard WorkerA much easier method is to use nla_parse(). 1864*4dc78e53SAndroid Build Coastguard Worker 1865*4dc78e53SAndroid Build Coastguard Worker[source,c] 1866*4dc78e53SAndroid Build Coastguard Worker-------- 1867*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1868*4dc78e53SAndroid Build Coastguard Worker 1869*4dc78e53SAndroid Build Coastguard Workerint nla_parse(struct nlattr **attrs, int maxtype, struct nlattr *head, 1870*4dc78e53SAndroid Build Coastguard Worker int len, struct nla_policy *policy); 1871*4dc78e53SAndroid Build Coastguard Worker-------- 1872*4dc78e53SAndroid Build Coastguard Worker 1873*4dc78e53SAndroid Build Coastguard WorkerThe function nla_parse() will iterate over a stream of attributes, 1874*4dc78e53SAndroid Build Coastguard Workervalidate each attribute as described in <<core_attr_validation>> 1875*4dc78e53SAndroid Build Coastguard WorkerIf the validation of all attributes succeeds, a pointer to each attribute 1876*4dc78e53SAndroid Build Coastguard Workeris stored in the +attrs+ array at `attrs[nla_type(attr)]`. 1877*4dc78e53SAndroid Build Coastguard Worker 1878*4dc78e53SAndroid Build Coastguard WorkerAs an alernative to nla_parse() the function nlmsg_parse() can be used 1879*4dc78e53SAndroid Build Coastguard Workerto parse the message and its attributes in one step. See 1880*4dc78e53SAndroid Build Coastguard Worker<<core_attr_parse_easy>> for information on how to use these functions. 1881*4dc78e53SAndroid Build Coastguard Worker 1882*4dc78e53SAndroid Build Coastguard Worker.Example: 1883*4dc78e53SAndroid Build Coastguard Worker 1884*4dc78e53SAndroid Build Coastguard WorkerThe following example demonstrates how to parse a netlink message sent 1885*4dc78e53SAndroid Build Coastguard Workerover a netlink protocol which does not use protocol headers. The example 1886*4dc78e53SAndroid Build Coastguard Workerdoes enforce an attribute policy however, the attribute MY_ATTR_FOO must 1887*4dc78e53SAndroid Build Coastguard Workerbe a 32 bit integer, and the attribute MY_ATTR_BAR must be a string with 1888*4dc78e53SAndroid Build Coastguard Workera maximum length of 16 characters. 1889*4dc78e53SAndroid Build Coastguard Worker 1890*4dc78e53SAndroid Build Coastguard Worker[source,c] 1891*4dc78e53SAndroid Build Coastguard Worker--------- 1892*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1893*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1894*4dc78e53SAndroid Build Coastguard Worker 1895*4dc78e53SAndroid Build Coastguard Workerenum { 1896*4dc78e53SAndroid Build Coastguard Worker MY_ATTR_FOO = 1, 1897*4dc78e53SAndroid Build Coastguard Worker MY_ATTR_BAR, 1898*4dc78e53SAndroid Build Coastguard Worker __MY_ATTR_MAX, 1899*4dc78e53SAndroid Build Coastguard Worker}; 1900*4dc78e53SAndroid Build Coastguard Worker 1901*4dc78e53SAndroid Build Coastguard Worker#define MY_ATTR_MAX (__MY_ATTR_MAX - 1) 1902*4dc78e53SAndroid Build Coastguard Worker 1903*4dc78e53SAndroid Build Coastguard Workerstatic struct nla_policy my_policy[MY_ATTR_MAX+1] = { 1904*4dc78e53SAndroid Build Coastguard Worker [MY_ATTR_FOO] = { .type = NLA_U32 }, 1905*4dc78e53SAndroid Build Coastguard Worker [MY_ATTR_BAR] = { .type = NLA_STRING, 1906*4dc78e53SAndroid Build Coastguard Worker .maxlen = 16 }, 1907*4dc78e53SAndroid Build Coastguard Worker}; 1908*4dc78e53SAndroid Build Coastguard Worker 1909*4dc78e53SAndroid Build Coastguard Workervoid parse_msg(struct nlmsghdr *nlh) 1910*4dc78e53SAndroid Build Coastguard Worker{ 1911*4dc78e53SAndroid Build Coastguard Worker struct nlattr *attrs[MY_ATTR_MAX+1]; 1912*4dc78e53SAndroid Build Coastguard Worker 1913*4dc78e53SAndroid Build Coastguard Worker if (nlmsg_parse(nlh, 0, attrs, MY_ATTR_MAX, my_policy) < 0) 1914*4dc78e53SAndroid Build Coastguard Worker /* error */ 1915*4dc78e53SAndroid Build Coastguard Worker 1916*4dc78e53SAndroid Build Coastguard Worker if (attrs[MY_ATTR_FOO]) { 1917*4dc78e53SAndroid Build Coastguard Worker /* MY_ATTR_FOO is present in message */ 1918*4dc78e53SAndroid Build Coastguard Worker printf("value: %u\n", nla_get_u32(attrs[MY_ATTR_FOO])); 1919*4dc78e53SAndroid Build Coastguard Worker } 1920*4dc78e53SAndroid Build Coastguard Worker} 1921*4dc78e53SAndroid Build Coastguard Worker--------- 1922*4dc78e53SAndroid Build Coastguard Worker 1923*4dc78e53SAndroid Build Coastguard Worker.Locating a Single Attribute 1924*4dc78e53SAndroid Build Coastguard Worker 1925*4dc78e53SAndroid Build Coastguard WorkerAn application only interested in a single attribute can use one of the 1926*4dc78e53SAndroid Build Coastguard Workerfunctions nla_find() or nlmsg_find_attr(). These function will iterate 1927*4dc78e53SAndroid Build Coastguard Workerover all attributes, search for a matching attribute and return a pointer 1928*4dc78e53SAndroid Build Coastguard Workerto the corresponding attribute header. 1929*4dc78e53SAndroid Build Coastguard Worker 1930*4dc78e53SAndroid Build Coastguard Worker[source,c] 1931*4dc78e53SAndroid Build Coastguard Worker-------- 1932*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1933*4dc78e53SAndroid Build Coastguard Worker 1934*4dc78e53SAndroid Build Coastguard Workerstruct nlattr *nla_find(struct nlattr *head, int len, int attrtype); 1935*4dc78e53SAndroid Build Coastguard Worker-------- 1936*4dc78e53SAndroid Build Coastguard Worker 1937*4dc78e53SAndroid Build Coastguard Worker[source,c] 1938*4dc78e53SAndroid Build Coastguard Worker-------- 1939*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 1940*4dc78e53SAndroid Build Coastguard Worker 1941*4dc78e53SAndroid Build Coastguard Workerstruct nlattr *nlmsg_find_attr(struct nlmsghdr *hdr, int hdrlen, int attrtype); 1942*4dc78e53SAndroid Build Coastguard Worker-------- 1943*4dc78e53SAndroid Build Coastguard Worker 1944*4dc78e53SAndroid Build Coastguard WorkerNOTE: `nla_find()` and `nlmsg_find_attr()` will *not* search in nested 1945*4dc78e53SAndroid Build Coastguard Worker attributes recursively, see <<core_attr_nested>>. 1946*4dc78e53SAndroid Build Coastguard Worker 1947*4dc78e53SAndroid Build Coastguard Worker==== Iterating over a Stream of Attributes 1948*4dc78e53SAndroid Build Coastguard Worker 1949*4dc78e53SAndroid Build Coastguard WorkerIn some situations it does not make sense to assign a unique attribute 1950*4dc78e53SAndroid Build Coastguard Workertype to each attribute in the attribute stream. For example a list may 1951*4dc78e53SAndroid Build Coastguard Workerbe transferred using a stream of attributes and even if the attribute type 1952*4dc78e53SAndroid Build Coastguard Workeris incremented for each attribute it may not make sense to use the 1953*4dc78e53SAndroid Build Coastguard Workernlmsg_parse() or nla_parse() function to fill an array. 1954*4dc78e53SAndroid Build Coastguard Worker 1955*4dc78e53SAndroid Build Coastguard WorkerTherefore methods exist to iterate over a stream of attributes: 1956*4dc78e53SAndroid Build Coastguard Worker 1957*4dc78e53SAndroid Build Coastguard Worker[source,c] 1958*4dc78e53SAndroid Build Coastguard Worker-------- 1959*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1960*4dc78e53SAndroid Build Coastguard Worker 1961*4dc78e53SAndroid Build Coastguard Workernla_for_each_attr(attr, head, len, remaining) 1962*4dc78e53SAndroid Build Coastguard Worker-------- 1963*4dc78e53SAndroid Build Coastguard Worker 1964*4dc78e53SAndroid Build Coastguard Workernla_for_each_attr() is a macro which can be used in front of a code 1965*4dc78e53SAndroid Build Coastguard Workerblock: 1966*4dc78e53SAndroid Build Coastguard Worker 1967*4dc78e53SAndroid Build Coastguard Worker[source,c] 1968*4dc78e53SAndroid Build Coastguard Worker-------- 1969*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 1970*4dc78e53SAndroid Build Coastguard Worker 1971*4dc78e53SAndroid Build Coastguard Workerstruct nalttr *nla; 1972*4dc78e53SAndroid Build Coastguard Workerint rem; 1973*4dc78e53SAndroid Build Coastguard Worker 1974*4dc78e53SAndroid Build Coastguard Workernla_for_each_attr(nla, attrstream, streamlen, rem) { 1975*4dc78e53SAndroid Build Coastguard Worker /* validate & parse attribute */ 1976*4dc78e53SAndroid Build Coastguard Worker} 1977*4dc78e53SAndroid Build Coastguard Worker 1978*4dc78e53SAndroid Build Coastguard Workerif (rem > 0) 1979*4dc78e53SAndroid Build Coastguard Worker /* unparsed attribute data */ 1980*4dc78e53SAndroid Build Coastguard Worker-------- 1981*4dc78e53SAndroid Build Coastguard Worker 1982*4dc78e53SAndroid Build Coastguard Worker[[core_attr_constr]] 1983*4dc78e53SAndroid Build Coastguard Worker=== Attribute Construction 1984*4dc78e53SAndroid Build Coastguard Worker 1985*4dc78e53SAndroid Build Coastguard WorkerThe interface to add attributes to a netlink message is based on the 1986*4dc78e53SAndroid Build Coastguard Workerregular message construction interface. It assumes that the message 1987*4dc78e53SAndroid Build Coastguard Workerheader and an eventual protocol header has been added to the message 1988*4dc78e53SAndroid Build Coastguard Workeralready. 1989*4dc78e53SAndroid Build Coastguard Worker 1990*4dc78e53SAndroid Build Coastguard Worker[source,c] 1991*4dc78e53SAndroid Build Coastguard Worker-------- 1992*4dc78e53SAndroid Build Coastguard Workerstruct nlattr *nla_reserve(struct nl_msg *msg, int attrtype, int len); 1993*4dc78e53SAndroid Build Coastguard Worker-------- 1994*4dc78e53SAndroid Build Coastguard Worker 1995*4dc78e53SAndroid Build Coastguard WorkerThe function nla_reserve() adds an attribute header at the end of the 1996*4dc78e53SAndroid Build Coastguard Workermessage and reserves room for +len+ bytes of payload. The function 1997*4dc78e53SAndroid Build Coastguard Workerreturns a pointer to the attribute payload section inside the message. 1998*4dc78e53SAndroid Build Coastguard WorkerPadding is added at the end of the attribute to ensure the next 1999*4dc78e53SAndroid Build Coastguard Workerattribute is properly aligned. 2000*4dc78e53SAndroid Build Coastguard Worker 2001*4dc78e53SAndroid Build Coastguard Worker[source,c] 2002*4dc78e53SAndroid Build Coastguard Worker-------- 2003*4dc78e53SAndroid Build Coastguard Workerint nla_put(struct nl_msg *msg, int attrtype, int attrlen, const void *data); 2004*4dc78e53SAndroid Build Coastguard Worker-------- 2005*4dc78e53SAndroid Build Coastguard Worker 2006*4dc78e53SAndroid Build Coastguard WorkerThe function nla_put() is based on nla_reserve() but takes an additional 2007*4dc78e53SAndroid Build Coastguard Workerpointer +data+ pointing to a buffer containing the attribute payload. 2008*4dc78e53SAndroid Build Coastguard WorkerIt will copy the buffer into the message automatically. 2009*4dc78e53SAndroid Build Coastguard Worker 2010*4dc78e53SAndroid Build Coastguard Worker.Example: 2011*4dc78e53SAndroid Build Coastguard Worker 2012*4dc78e53SAndroid Build Coastguard Worker[source,c] 2013*4dc78e53SAndroid Build Coastguard Worker-------- 2014*4dc78e53SAndroid Build Coastguard Workerstruct my_attr_struct { 2015*4dc78e53SAndroid Build Coastguard Worker uint32_t a; 2016*4dc78e53SAndroid Build Coastguard Worker uint32_t b; 2017*4dc78e53SAndroid Build Coastguard Worker}; 2018*4dc78e53SAndroid Build Coastguard Worker 2019*4dc78e53SAndroid Build Coastguard Workerint my_put(struct nl_msg *msg) 2020*4dc78e53SAndroid Build Coastguard Worker{ 2021*4dc78e53SAndroid Build Coastguard Worker struct my_attr_struct obj = { 2022*4dc78e53SAndroid Build Coastguard Worker .a = 10, 2023*4dc78e53SAndroid Build Coastguard Worker .b = 20, 2024*4dc78e53SAndroid Build Coastguard Worker }; 2025*4dc78e53SAndroid Build Coastguard Worker 2026*4dc78e53SAndroid Build Coastguard Worker return nla_put(msg, ATTR_MY_STRUCT, sizeof(obj), &obj); 2027*4dc78e53SAndroid Build Coastguard Worker} 2028*4dc78e53SAndroid Build Coastguard Worker-------- 2029*4dc78e53SAndroid Build Coastguard Worker 2030*4dc78e53SAndroid Build Coastguard WorkerSee <<core_attr_types>> for datatype specific attribute construction 2031*4dc78e53SAndroid Build Coastguard Workerfunctions. 2032*4dc78e53SAndroid Build Coastguard Worker 2033*4dc78e53SAndroid Build Coastguard Worker.Exception Based Attribute Construction 2034*4dc78e53SAndroid Build Coastguard Worker 2035*4dc78e53SAndroid Build Coastguard WorkerLike in the kernel API an exception based construction interface is 2036*4dc78e53SAndroid Build Coastguard Workerprovided. The behaviour of the macros is identical to their regular 2037*4dc78e53SAndroid Build Coastguard Workerfunction counterparts except that in case of an error, the target 2038*4dc78e53SAndroid Build Coastguard Worker`nla_put_failure` is jumped. 2039*4dc78e53SAndroid Build Coastguard Worker 2040*4dc78e53SAndroid Build Coastguard Worker.Example: 2041*4dc78e53SAndroid Build Coastguard Worker[source,c] 2042*4dc78e53SAndroid Build Coastguard Worker-------- 2043*4dc78e53SAndroid Build Coastguard Worker#include <netlink/msg.h> 2044*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 2045*4dc78e53SAndroid Build Coastguard Worker 2046*4dc78e53SAndroid Build Coastguard Workervoid construct_attrs(struct nl_msg *msg) 2047*4dc78e53SAndroid Build Coastguard Worker{ 2048*4dc78e53SAndroid Build Coastguard Worker NLA_PUT_STRING(msg, MY_ATTR_FOO1, "some text"); 2049*4dc78e53SAndroid Build Coastguard Worker NLA_PUT_U32(msg, MY_ATTR_FOO1, 0x1010); 2050*4dc78e53SAndroid Build Coastguard Worker NLA_PUT_FLAG(msg, MY_ATTR_FOO3, 1); 2051*4dc78e53SAndroid Build Coastguard Worker 2052*4dc78e53SAndroid Build Coastguard Worker return 0; 2053*4dc78e53SAndroid Build Coastguard Worker 2054*4dc78e53SAndroid Build Coastguard Workernla_put_failure: 2055*4dc78e53SAndroid Build Coastguard Worker /* NLA_PUT* macros jump here in case of an error */ 2056*4dc78e53SAndroid Build Coastguard Worker return -EMSGSIZE; 2057*4dc78e53SAndroid Build Coastguard Worker} 2058*4dc78e53SAndroid Build Coastguard Worker-------- 2059*4dc78e53SAndroid Build Coastguard Worker 2060*4dc78e53SAndroid Build Coastguard WorkerSee <<core_attr_types>> for more information on the datatype specific 2061*4dc78e53SAndroid Build Coastguard Workerexception based variants. 2062*4dc78e53SAndroid Build Coastguard Worker 2063*4dc78e53SAndroid Build Coastguard Worker[[core_attr_types]] 2064*4dc78e53SAndroid Build Coastguard Worker=== Attribute Data Types 2065*4dc78e53SAndroid Build Coastguard Worker 2066*4dc78e53SAndroid Build Coastguard WorkerA number of basic data types have been defined to simplify access and 2067*4dc78e53SAndroid Build Coastguard Workervalidation of attributes. The datatype is not encoded in the 2068*4dc78e53SAndroid Build Coastguard Workerattribute, therefore the sender and receiver are required to use the 2069*4dc78e53SAndroid Build Coastguard Workersame definition on what attribute is of what type. 2070*4dc78e53SAndroid Build Coastguard Worker 2071*4dc78e53SAndroid Build Coastguard Worker[options="header", cols="1m,5"] 2072*4dc78e53SAndroid Build Coastguard Worker|================================================ 2073*4dc78e53SAndroid Build Coastguard Worker| Type | Description 2074*4dc78e53SAndroid Build Coastguard Worker| NLA_UNSPEC | Unspecified attribute 2075*4dc78e53SAndroid Build Coastguard Worker| NLA_U{8\|16\|32} | Integers 2076*4dc78e53SAndroid Build Coastguard Worker| NLA_STRING | String 2077*4dc78e53SAndroid Build Coastguard Worker| NLA_FLAG | Flag 2078*4dc78e53SAndroid Build Coastguard Worker| NLA_NESTED | Nested attribute 2079*4dc78e53SAndroid Build Coastguard Worker|================================================ 2080*4dc78e53SAndroid Build Coastguard Worker 2081*4dc78e53SAndroid Build Coastguard WorkerBesides simplified access to the payload of such datatypes, the major 2082*4dc78e53SAndroid Build Coastguard Workeradvantage is the automatic validation of each attribute based on a 2083*4dc78e53SAndroid Build Coastguard Workerpolicy. The validation ensures safe access to the payload by checking 2084*4dc78e53SAndroid Build Coastguard Workerfor minimal payload size and can also be used to enforce maximum 2085*4dc78e53SAndroid Build Coastguard Workerpayload size for some datatypes. 2086*4dc78e53SAndroid Build Coastguard Worker 2087*4dc78e53SAndroid Build Coastguard Worker==== Integer Attributes 2088*4dc78e53SAndroid Build Coastguard Worker 2089*4dc78e53SAndroid Build Coastguard WorkerThe most frequently used datatypes are integers. Integers come in four 2090*4dc78e53SAndroid Build Coastguard Workerdifferent sizes: 2091*4dc78e53SAndroid Build Coastguard Worker[horizontal] 2092*4dc78e53SAndroid Build Coastguard WorkerNLA_U8:: 8bit integer 2093*4dc78e53SAndroid Build Coastguard WorkerNLA_U16:: 16bit integer 2094*4dc78e53SAndroid Build Coastguard WorkerNLA_U32:: 32bit integer 2095*4dc78e53SAndroid Build Coastguard WorkerNLA_U64:: 64bit integer 2096*4dc78e53SAndroid Build Coastguard Worker 2097*4dc78e53SAndroid Build Coastguard WorkerNote that due to the alignment requirements of attributes the integer 2098*4dc78e53SAndroid Build Coastguard Workerattribute +NLA_u8+ and +NLA_U16+ will not result in space savings in 2099*4dc78e53SAndroid Build Coastguard Workerthe netlink message. Their use is intended to limit the range of 2100*4dc78e53SAndroid Build Coastguard Workervalues. 2101*4dc78e53SAndroid Build Coastguard Worker 2102*4dc78e53SAndroid Build Coastguard Worker.Parsing Integer Attributes 2103*4dc78e53SAndroid Build Coastguard Worker 2104*4dc78e53SAndroid Build Coastguard Worker[source,c] 2105*4dc78e53SAndroid Build Coastguard Worker-------- 2106*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 2107*4dc78e53SAndroid Build Coastguard Worker 2108*4dc78e53SAndroid Build Coastguard Workeruint8_t nla_get_u8(struct nlattr *hdr); 2109*4dc78e53SAndroid Build Coastguard Workeruint16_t nla_get_u16(struct nlattr *hdr); 2110*4dc78e53SAndroid Build Coastguard Workeruint32_t nla_get_u32(struct nlattr *hdr); 2111*4dc78e53SAndroid Build Coastguard Workeruint64_t nla_get_u64(struct nlattr *hdr); 2112*4dc78e53SAndroid Build Coastguard Worker-------- 2113*4dc78e53SAndroid Build Coastguard Worker 2114*4dc78e53SAndroid Build Coastguard WorkerExample: 2115*4dc78e53SAndroid Build Coastguard Worker 2116*4dc78e53SAndroid Build Coastguard Worker[source,c] 2117*4dc78e53SAndroid Build Coastguard Worker-------- 2118*4dc78e53SAndroid Build Coastguard Workerif (attrs[MY_ATTR_FOO]) 2119*4dc78e53SAndroid Build Coastguard Worker uint32_t val = nla_get_u32(attrs[MY_ATTR_FOO]); 2120*4dc78e53SAndroid Build Coastguard Worker-------- 2121*4dc78e53SAndroid Build Coastguard Worker 2122*4dc78e53SAndroid Build Coastguard Worker.Constructing Integer Attributes 2123*4dc78e53SAndroid Build Coastguard Worker 2124*4dc78e53SAndroid Build Coastguard Worker[source,c] 2125*4dc78e53SAndroid Build Coastguard Worker-------- 2126*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 2127*4dc78e53SAndroid Build Coastguard Worker 2128*4dc78e53SAndroid Build Coastguard Workerint nla_put_u8(struct nl_msg *msg, int attrtype, uint8_t value); 2129*4dc78e53SAndroid Build Coastguard Workerint nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value); 2130*4dc78e53SAndroid Build Coastguard Workerint nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value); 2131*4dc78e53SAndroid Build Coastguard Workerint nla_put_u64(struct nl_msg *msg, int attrtype, uint64_t value); 2132*4dc78e53SAndroid Build Coastguard Worker-------- 2133*4dc78e53SAndroid Build Coastguard Worker 2134*4dc78e53SAndroid Build Coastguard WorkerException based: 2135*4dc78e53SAndroid Build Coastguard Worker 2136*4dc78e53SAndroid Build Coastguard Worker[source,c] 2137*4dc78e53SAndroid Build Coastguard Worker-------- 2138*4dc78e53SAndroid Build Coastguard WorkerNLA_PUT_U8(msg, attrtype, value) 2139*4dc78e53SAndroid Build Coastguard WorkerNLA_PUT_U16(msg, attrtype, value) 2140*4dc78e53SAndroid Build Coastguard WorkerNLA_PUT_U32(msg, attrtype, value) 2141*4dc78e53SAndroid Build Coastguard WorkerNLA_PUT_U64(msg, attrtype, value) 2142*4dc78e53SAndroid Build Coastguard Worker-------- 2143*4dc78e53SAndroid Build Coastguard Worker 2144*4dc78e53SAndroid Build Coastguard Worker.Validation 2145*4dc78e53SAndroid Build Coastguard Worker 2146*4dc78e53SAndroid Build Coastguard WorkerUse +NLA_U8+, +NLA_U16+, +NLA_U32+, or +NLA_U64+ to define the type of 2147*4dc78e53SAndroid Build Coastguard Workerinteger when filling out a struct nla_policy array. It will 2148*4dc78e53SAndroid Build Coastguard Workerautomatically enforce the correct minimum payload length policy. 2149*4dc78e53SAndroid Build Coastguard Worker 2150*4dc78e53SAndroid Build Coastguard WorkerValidation does not differ between signed and unsigned integers, only 2151*4dc78e53SAndroid Build Coastguard Workerthe size matters. If the application wishes to enforce particular value 2152*4dc78e53SAndroid Build Coastguard Workerranges it must do so itself. 2153*4dc78e53SAndroid Build Coastguard Worker 2154*4dc78e53SAndroid Build Coastguard Worker[source,c] 2155*4dc78e53SAndroid Build Coastguard Worker-------- 2156*4dc78e53SAndroid Build Coastguard Workerstatic struct nla_policy my_policy[ATTR_MAX+1] = { 2157*4dc78e53SAndroid Build Coastguard Worker [ATTR_FOO] = { .type = NLA_U32 }, 2158*4dc78e53SAndroid Build Coastguard Worker [ATTR_BAR] = { .type = NLA_U8 }, 2159*4dc78e53SAndroid Build Coastguard Worker}; 2160*4dc78e53SAndroid Build Coastguard Worker-------- 2161*4dc78e53SAndroid Build Coastguard Worker 2162*4dc78e53SAndroid Build Coastguard WorkerThe above is equivalent to: 2163*4dc78e53SAndroid Build Coastguard Worker[source,c] 2164*4dc78e53SAndroid Build Coastguard Worker-------- 2165*4dc78e53SAndroid Build Coastguard Workerstatic struct nla_policy my_policy[ATTR_MAX+1] = { 2166*4dc78e53SAndroid Build Coastguard Worker [ATTR_FOO] = { .minlen = sizeof(uint32_t) }, 2167*4dc78e53SAndroid Build Coastguard Worker [ATTR_BAR] = { .minlen = sizeof(uint8_t) }, 2168*4dc78e53SAndroid Build Coastguard Worker}; 2169*4dc78e53SAndroid Build Coastguard Worker-------- 2170*4dc78e53SAndroid Build Coastguard Worker 2171*4dc78e53SAndroid Build Coastguard Worker==== String Attributes 2172*4dc78e53SAndroid Build Coastguard Worker 2173*4dc78e53SAndroid Build Coastguard WorkerThe string datatype represents a NUL termianted character string of 2174*4dc78e53SAndroid Build Coastguard Workervariable length. It is not intended for binary data streams. 2175*4dc78e53SAndroid Build Coastguard Worker 2176*4dc78e53SAndroid Build Coastguard WorkerThe payload of string attributes can be accessed with the function 2177*4dc78e53SAndroid Build Coastguard Workernla_get_string(). nla_strdup() calls strdup() on the payload and returns 2178*4dc78e53SAndroid Build Coastguard Workerthe newly allocated string. 2179*4dc78e53SAndroid Build Coastguard Worker 2180*4dc78e53SAndroid Build Coastguard Worker[source,c] 2181*4dc78e53SAndroid Build Coastguard Worker-------- 2182*4dc78e53SAndroid Build Coastguard Worker#include <netlink/attr.h> 2183*4dc78e53SAndroid Build Coastguard Worker 2184*4dc78e53SAndroid Build Coastguard Workerchar *nla_get_string(struct nlattr *hdr); 2185*4dc78e53SAndroid Build Coastguard Workerchar *nla_strdup(struct nlattr *hdr); 2186*4dc78e53SAndroid Build Coastguard Worker-------- 2187*4dc78e53SAndroid Build Coastguard Worker 2188*4dc78e53SAndroid Build Coastguard WorkerString attributes are constructed with the function +nla_put_string()+ 2189*4dc78e53SAndroid Build Coastguard Workerrespectively +NLA_PUT_STRING()+. The length of the payload will be 2190*4dc78e53SAndroid Build Coastguard Workerstrlen()+1, the trailing NUL byte is included. 2191*4dc78e53SAndroid Build Coastguard Worker 2192*4dc78e53SAndroid Build Coastguard Worker[source,c] 2193*4dc78e53SAndroid Build Coastguard Worker-------- 2194*4dc78e53SAndroid Build Coastguard Workerint nla_put_string(struct nl_msg *msg, int attrtype, const char *data); 2195*4dc78e53SAndroid Build Coastguard Worker 2196*4dc78e53SAndroid Build Coastguard WorkerNLA_PUT_STRING(msg, attrtype, data) 2197*4dc78e53SAndroid Build Coastguard Worker-------- 2198*4dc78e53SAndroid Build Coastguard Worker 2199*4dc78e53SAndroid Build Coastguard WorkerFor validation purposes the type +NLA_STRING+ can be used in 2200*4dc78e53SAndroid Build Coastguard Worker+struct nla_policy+ definitions. It implies a minimum payload length 2201*4dc78e53SAndroid Build Coastguard Workerof 1 byte and checks for a trailing NUL byte. Optionally the +maxlen+ 2202*4dc78e53SAndroid Build Coastguard Workermember defines the maximum length of a character string (including the 2203*4dc78e53SAndroid Build Coastguard Workertrailing NUL byte). 2204*4dc78e53SAndroid Build Coastguard Worker 2205*4dc78e53SAndroid Build Coastguard Worker[source,c] 2206*4dc78e53SAndroid Build Coastguard Worker-------- 2207*4dc78e53SAndroid Build Coastguard Workerstatic struct nla_policy my_policy[] = { 2208*4dc78e53SAndroid Build Coastguard Worker [ATTR_FOO] = { .type = NLA_STRING, 2209*4dc78e53SAndroid Build Coastguard Worker .maxlen = IFNAMSIZ }, 2210*4dc78e53SAndroid Build Coastguard Worker}; 2211*4dc78e53SAndroid Build Coastguard Worker-------- 2212*4dc78e53SAndroid Build Coastguard Worker 2213*4dc78e53SAndroid Build Coastguard Worker==== Flag Attributes 2214*4dc78e53SAndroid Build Coastguard Worker 2215*4dc78e53SAndroid Build Coastguard WorkerThe flag attribute represents a boolean datatype. The presence of the 2216*4dc78e53SAndroid Build Coastguard Workerattribute implies a value of +true+, the absence of the attribute 2217*4dc78e53SAndroid Build Coastguard Workerimplies the value +false+. Therefore the payload length of flag 2218*4dc78e53SAndroid Build Coastguard Workerattributes is always 0. 2219*4dc78e53SAndroid Build Coastguard Worker 2220*4dc78e53SAndroid Build Coastguard Worker[source,c] 2221*4dc78e53SAndroid Build Coastguard Worker-------- 2222*4dc78e53SAndroid Build Coastguard Workerint nla_get_flag(struct nlattr *hdr); 2223*4dc78e53SAndroid Build Coastguard Workerint nla_put_flag(struct nl_msg *msg, int attrtype); 2224*4dc78e53SAndroid Build Coastguard Worker-------- 2225*4dc78e53SAndroid Build Coastguard Worker 2226*4dc78e53SAndroid Build Coastguard WorkerThe type +NLA_FLAG+ is used for validation purposes. It implies a 2227*4dc78e53SAndroid Build Coastguard Worker+maxlen+ value of 0 and thus enforces a maximum payload length of 0. 2228*4dc78e53SAndroid Build Coastguard Worker 2229*4dc78e53SAndroid Build Coastguard Worker.Example: 2230*4dc78e53SAndroid Build Coastguard Worker[source,c] 2231*4dc78e53SAndroid Build Coastguard Worker-------- 2232*4dc78e53SAndroid Build Coastguard Worker/* nla_put_flag() appends a zero sized attribute to the message. */ 2233*4dc78e53SAndroid Build Coastguard Workernla_put_flag(msg, ATTR_FLAG); 2234*4dc78e53SAndroid Build Coastguard Worker 2235*4dc78e53SAndroid Build Coastguard Worker/* There is no need for a receival function, the presence is the value. */ 2236*4dc78e53SAndroid Build Coastguard Workerif (attrs[ATTR_FLAG]) 2237*4dc78e53SAndroid Build Coastguard Worker /* flag is present */ 2238*4dc78e53SAndroid Build Coastguard Worker-------- 2239*4dc78e53SAndroid Build Coastguard Worker 2240*4dc78e53SAndroid Build Coastguard Worker[[core_attr_nested]] 2241*4dc78e53SAndroid Build Coastguard Worker==== Nested Attributes 2242*4dc78e53SAndroid Build Coastguard Worker 2243*4dc78e53SAndroid Build Coastguard WorkerAs described in <<core_attr>>, attributes can be nested allowing for 2244*4dc78e53SAndroid Build Coastguard Workercomplex tree structures of attributes. It is commonly used to delegate 2245*4dc78e53SAndroid Build Coastguard Workerthe responsibility of a subsection of the message to a subsystem. 2246*4dc78e53SAndroid Build Coastguard WorkerNested attributes are also commonly used for transmitting list of objects. 2247*4dc78e53SAndroid Build Coastguard Worker 2248*4dc78e53SAndroid Build Coastguard WorkerWhen nesting attributes, the nested attributes are included as payload 2249*4dc78e53SAndroid Build Coastguard Workerof a container attribute. 2250*4dc78e53SAndroid Build Coastguard Worker 2251*4dc78e53SAndroid Build Coastguard WorkerNOTE: When validating the attributes using nlmsg_validate(), 2252*4dc78e53SAndroid Build Coastguard Worker nlmsg_parse(), nla_validate(), or nla_parse() only the 2253*4dc78e53SAndroid Build Coastguard Worker attributes on the first level are being validated. None of these 2254*4dc78e53SAndroid Build Coastguard Worker functions will validate attributes recursively. Therefore you 2255*4dc78e53SAndroid Build Coastguard Worker must explicitly call nla_validate() or use nla_parse_nested() 2256*4dc78e53SAndroid Build Coastguard Worker for each level of nested attributes. 2257*4dc78e53SAndroid Build Coastguard Worker 2258*4dc78e53SAndroid Build Coastguard WorkerThe type +NLA_NESTED+ should be used when defining nested attributes 2259*4dc78e53SAndroid Build Coastguard Workerin a struct nla_policy definition. It will not enforce any minimum 2260*4dc78e53SAndroid Build Coastguard Workerpayload length unless +minlen+ is specified explicitly. This is 2261*4dc78e53SAndroid Build Coastguard Workerbecause some netlink protocols implicitly allow empty container 2262*4dc78e53SAndroid Build Coastguard Workerattributes. 2263*4dc78e53SAndroid Build Coastguard Worker 2264*4dc78e53SAndroid Build Coastguard Worker[source,c] 2265*4dc78e53SAndroid Build Coastguard Worker-------- 2266*4dc78e53SAndroid Build Coastguard Workerstatic struct nla_policy my_policy[] = { 2267*4dc78e53SAndroid Build Coastguard Worker [ATTR_OPTS] = { .type = NLA_NESTED }, 2268*4dc78e53SAndroid Build Coastguard Worker}; 2269*4dc78e53SAndroid Build Coastguard Worker-------- 2270*4dc78e53SAndroid Build Coastguard Worker 2271*4dc78e53SAndroid Build Coastguard Worker.Parsing of Nested Attributes 2272*4dc78e53SAndroid Build Coastguard Worker 2273*4dc78e53SAndroid Build Coastguard WorkerThe function nla_parse_nested() is used to parse nested attributes. 2274*4dc78e53SAndroid Build Coastguard WorkerIts behaviour is identical to nla_parse() except that it takes a 2275*4dc78e53SAndroid Build Coastguard Workerstruct nlattr as argument and will use the payload as stream of 2276*4dc78e53SAndroid Build Coastguard Workerattributes. 2277*4dc78e53SAndroid Build Coastguard Worker 2278*4dc78e53SAndroid Build Coastguard Worker[source,c] 2279*4dc78e53SAndroid Build Coastguard Worker-------- 2280*4dc78e53SAndroid Build Coastguard Workerif (attrs[ATTR_OPTS]) { 2281*4dc78e53SAndroid Build Coastguard Worker struct nlattr *nested[NESTED_MAX+1]; 2282*4dc78e53SAndroid Build Coastguard Worker struct nla_policy nested_policy[] = { 2283*4dc78e53SAndroid Build Coastguard Worker [NESTED_FOO] = { .type = NLA_U32 }, 2284*4dc78e53SAndroid Build Coastguard Worker }; 2285*4dc78e53SAndroid Build Coastguard Worker 2286*4dc78e53SAndroid Build Coastguard Worker if (nla_parse_nested(nested, NESTED_MAX, attrs[ATTR_OPTS], nested_policy) < 0) 2287*4dc78e53SAndroid Build Coastguard Worker /* error */ 2288*4dc78e53SAndroid Build Coastguard Worker 2289*4dc78e53SAndroid Build Coastguard Worker if (nested[NESTED_FOO]) 2290*4dc78e53SAndroid Build Coastguard Worker uint32_t val = nla_get_u32(nested[NESTED_FOO]); 2291*4dc78e53SAndroid Build Coastguard Worker} 2292*4dc78e53SAndroid Build Coastguard Worker-------- 2293*4dc78e53SAndroid Build Coastguard Worker 2294*4dc78e53SAndroid Build Coastguard Worker.Construction of Nested Attributes 2295*4dc78e53SAndroid Build Coastguard Worker 2296*4dc78e53SAndroid Build Coastguard WorkerAttributes are nested by surrounding them with calls to nla_nest_start() 2297*4dc78e53SAndroid Build Coastguard Workerand nla_nest_end(). nla_nest_start() will add an attribute header to 2298*4dc78e53SAndroid Build Coastguard Workerthe message but no actual payload. All data added to the message from 2299*4dc78e53SAndroid Build Coastguard Workerthis point on will be part of the container attribute until nla_nest_end() 2300*4dc78e53SAndroid Build Coastguard Workeris called which "closes" the attribute, correcting its payload length to 2301*4dc78e53SAndroid Build Coastguard Workerinclude all data length. 2302*4dc78e53SAndroid Build Coastguard Worker 2303*4dc78e53SAndroid Build Coastguard Worker[source,c] 2304*4dc78e53SAndroid Build Coastguard Worker-------- 2305*4dc78e53SAndroid Build Coastguard Workerint put_opts(struct nl_msg *msg) 2306*4dc78e53SAndroid Build Coastguard Worker{ 2307*4dc78e53SAndroid Build Coastguard Worker struct nlattr *opts; 2308*4dc78e53SAndroid Build Coastguard Worker 2309*4dc78e53SAndroid Build Coastguard Worker if (!(opts = nla_nest_start(msg, ATTR_OPTS))) 2310*4dc78e53SAndroid Build Coastguard Worker goto nla_put_failure; 2311*4dc78e53SAndroid Build Coastguard Worker 2312*4dc78e53SAndroid Build Coastguard Worker NLA_PUT_U32(msg, NESTED_FOO, 123); 2313*4dc78e53SAndroid Build Coastguard Worker NLA_PUT_STRING(msg, NESTED_BAR, "some text"); 2314*4dc78e53SAndroid Build Coastguard Worker 2315*4dc78e53SAndroid Build Coastguard Worker nla_nest_end(msg, opts); 2316*4dc78e53SAndroid Build Coastguard Worker return 0; 2317*4dc78e53SAndroid Build Coastguard Worker 2318*4dc78e53SAndroid Build Coastguard Workernla_put_failure: 2319*4dc78e53SAndroid Build Coastguard Worker nla_nest_cancel(msg, opts); 2320*4dc78e53SAndroid Build Coastguard Worker return -EMSGSIZE; 2321*4dc78e53SAndroid Build Coastguard Worker} 2322*4dc78e53SAndroid Build Coastguard Worker-------- 2323*4dc78e53SAndroid Build Coastguard Worker 2324*4dc78e53SAndroid Build Coastguard Worker==== Unspecified Attribute 2325*4dc78e53SAndroid Build Coastguard Worker 2326*4dc78e53SAndroid Build Coastguard WorkerThis is the default attribute type and used when none of the basic 2327*4dc78e53SAndroid Build Coastguard Workerdatatypes is suitable. It represents data of arbitrary type and length. 2328*4dc78e53SAndroid Build Coastguard Worker 2329*4dc78e53SAndroid Build Coastguard WorkerSee <<core_addr_alloc, Address Allocation>> for a more information on 2330*4dc78e53SAndroid Build Coastguard Workera special interface allowing the allocation of abstract address object 2331*4dc78e53SAndroid Build Coastguard Workerbased on netlink attributes which carry some form of network address. 2332*4dc78e53SAndroid Build Coastguard Worker 2333*4dc78e53SAndroid Build Coastguard WorkerSee <<core_data_alloc, Abstract Data Allocation>> for more information 2334*4dc78e53SAndroid Build Coastguard Workeron how to allocate abstract data objects based on netlink attributes. 2335*4dc78e53SAndroid Build Coastguard Worker 2336*4dc78e53SAndroid Build Coastguard WorkerUse the function nla_get() and nla_put() to access the payload and 2337*4dc78e53SAndroid Build Coastguard Workerconstruct attributes. See <<core_attr_constr, Attribute Construction>> 2338*4dc78e53SAndroid Build Coastguard Workerfor an example. 2339*4dc78e53SAndroid Build Coastguard Worker 2340*4dc78e53SAndroid Build Coastguard Worker=== Examples 2341*4dc78e53SAndroid Build Coastguard Worker 2342*4dc78e53SAndroid Build Coastguard Worker==== Constructing a Netlink Message with Attributes 2343*4dc78e53SAndroid Build Coastguard Worker 2344*4dc78e53SAndroid Build Coastguard Worker[source,c] 2345*4dc78e53SAndroid Build Coastguard Worker-------- 2346*4dc78e53SAndroid Build Coastguard Workerstruct nl_msg *build_msg(int ifindex, struct nl_addr *lladdr, int mtu) 2347*4dc78e53SAndroid Build Coastguard Worker{ 2348*4dc78e53SAndroid Build Coastguard Worker struct nl_msg *msg; 2349*4dc78e53SAndroid Build Coastguard Worker struct nlattr *info, *vlan; 2350*4dc78e53SAndroid Build Coastguard Worker struct ifinfomsg ifi = { 2351*4dc78e53SAndroid Build Coastguard Worker .ifi_family = AF_INET, 2352*4dc78e53SAndroid Build Coastguard Worker .ifi_index = ifindex, 2353*4dc78e53SAndroid Build Coastguard Worker }; 2354*4dc78e53SAndroid Build Coastguard Worker 2355*4dc78e53SAndroid Build Coastguard Worker /* Allocate a default sized netlink message */ 2356*4dc78e53SAndroid Build Coastguard Worker if (!(msg = nlmsg_alloc_simple(RTM_SETLINK, 0))) 2357*4dc78e53SAndroid Build Coastguard Worker return NULL; 2358*4dc78e53SAndroid Build Coastguard Worker 2359*4dc78e53SAndroid Build Coastguard Worker /* Append the protocol specific header (struct ifinfomsg)*/ 2360*4dc78e53SAndroid Build Coastguard Worker if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0) 2361*4dc78e53SAndroid Build Coastguard Worker goto nla_put_failure 2362*4dc78e53SAndroid Build Coastguard Worker 2363*4dc78e53SAndroid Build Coastguard Worker /* Append a 32 bit integer attribute to carry the MTU */ 2364*4dc78e53SAndroid Build Coastguard Worker NLA_PUT_U32(msg, IFLA_MTU, mtu); 2365*4dc78e53SAndroid Build Coastguard Worker 2366*4dc78e53SAndroid Build Coastguard Worker /* Append an unspecific attribute to carry the link layer address */ 2367*4dc78e53SAndroid Build Coastguard Worker NLA_PUT_ADDR(msg, IFLA_ADDRESS, lladdr); 2368*4dc78e53SAndroid Build Coastguard Worker 2369*4dc78e53SAndroid Build Coastguard Worker /* Append a container for nested attributes to carry link information */ 2370*4dc78e53SAndroid Build Coastguard Worker if (!(info = nla_nest_start(msg, IFLA_LINKINFO))) 2371*4dc78e53SAndroid Build Coastguard Worker goto nla_put_failure; 2372*4dc78e53SAndroid Build Coastguard Worker 2373*4dc78e53SAndroid Build Coastguard Worker /* Put a string attribute into the container */ 2374*4dc78e53SAndroid Build Coastguard Worker NLA_PUT_STRING(msg, IFLA_INFO_KIND, "vlan"); 2375*4dc78e53SAndroid Build Coastguard Worker 2376*4dc78e53SAndroid Build Coastguard Worker /* 2377*4dc78e53SAndroid Build Coastguard Worker * Append another container inside the open container to carry 2378*4dc78e53SAndroid Build Coastguard Worker * vlan specific attributes 2379*4dc78e53SAndroid Build Coastguard Worker */ 2380*4dc78e53SAndroid Build Coastguard Worker if (!(vlan = nla_nest_start(msg, IFLA_INFO_DATA))) 2381*4dc78e53SAndroid Build Coastguard Worker goto nla_put_failure; 2382*4dc78e53SAndroid Build Coastguard Worker 2383*4dc78e53SAndroid Build Coastguard Worker /* add vlan specific info attributes here... */ 2384*4dc78e53SAndroid Build Coastguard Worker 2385*4dc78e53SAndroid Build Coastguard Worker /* Finish nesting the vlan attributes and close the second container. */ 2386*4dc78e53SAndroid Build Coastguard Worker nla_nest_end(msg, vlan); 2387*4dc78e53SAndroid Build Coastguard Worker 2388*4dc78e53SAndroid Build Coastguard Worker /* Finish nesting the link info attribute and close the first container. */ 2389*4dc78e53SAndroid Build Coastguard Worker nla_nest_end(msg, info); 2390*4dc78e53SAndroid Build Coastguard Worker 2391*4dc78e53SAndroid Build Coastguard Worker return msg; 2392*4dc78e53SAndroid Build Coastguard Worker 2393*4dc78e53SAndroid Build Coastguard Workernla_put_failure: 2394*4dc78e53SAndroid Build Coastguard Worker nlmsg_free(msg); 2395*4dc78e53SAndroid Build Coastguard Worker return NULL; 2396*4dc78e53SAndroid Build Coastguard Worker} 2397*4dc78e53SAndroid Build Coastguard Worker------ 2398*4dc78e53SAndroid Build Coastguard Worker 2399*4dc78e53SAndroid Build Coastguard Worker==== Parsing a Netlink Message with Attributes 2400*4dc78e53SAndroid Build Coastguard Worker 2401*4dc78e53SAndroid Build Coastguard Worker[source,c] 2402*4dc78e53SAndroid Build Coastguard Worker-------- 2403*4dc78e53SAndroid Build Coastguard Workerint parse_message(struct nlmsghdr *hdr) 2404*4dc78e53SAndroid Build Coastguard Worker{ 2405*4dc78e53SAndroid Build Coastguard Worker /* 2406*4dc78e53SAndroid Build Coastguard Worker * The policy defines two attributes: a 32 bit integer and a container 2407*4dc78e53SAndroid Build Coastguard Worker * for nested attributes. 2408*4dc78e53SAndroid Build Coastguard Worker */ 2409*4dc78e53SAndroid Build Coastguard Worker struct nla_policy attr_policy[] = { 2410*4dc78e53SAndroid Build Coastguard Worker [ATTR_FOO] = { .type = NLA_U32 }, 2411*4dc78e53SAndroid Build Coastguard Worker [ATTR_BAR] = { .type = NLA_NESTED }, 2412*4dc78e53SAndroid Build Coastguard Worker }; 2413*4dc78e53SAndroid Build Coastguard Worker struct nlattr *attrs[ATTR_MAX+1]; 2414*4dc78e53SAndroid Build Coastguard Worker int err; 2415*4dc78e53SAndroid Build Coastguard Worker 2416*4dc78e53SAndroid Build Coastguard Worker /* 2417*4dc78e53SAndroid Build Coastguard Worker * The nlmsg_parse() function will make sure that the message contains 2418*4dc78e53SAndroid Build Coastguard Worker * enough payload to hold the header (struct my_hdr), validates any 2419*4dc78e53SAndroid Build Coastguard Worker * attributes attached to the messages and stores a pointer to each 2420*4dc78e53SAndroid Build Coastguard Worker * attribute in the attrs[] array accessible by attribute type. 2421*4dc78e53SAndroid Build Coastguard Worker */ 2422*4dc78e53SAndroid Build Coastguard Worker if ((err = nlmsg_parse(hdr, sizeof(struct my_hdr), attrs, ATTR_MAX, 2423*4dc78e53SAndroid Build Coastguard Worker attr_policy)) < 0) 2424*4dc78e53SAndroid Build Coastguard Worker goto errout; 2425*4dc78e53SAndroid Build Coastguard Worker 2426*4dc78e53SAndroid Build Coastguard Worker if (attrs[ATTR_FOO]) { 2427*4dc78e53SAndroid Build Coastguard Worker /* 2428*4dc78e53SAndroid Build Coastguard Worker * It is safe to directly access the attribute payload without 2429*4dc78e53SAndroid Build Coastguard Worker * any further checks since nlmsg_parse() enforced the policy. 2430*4dc78e53SAndroid Build Coastguard Worker */ 2431*4dc78e53SAndroid Build Coastguard Worker uint32_t foo = nla_get_u32(attrs[ATTR_FOO]); 2432*4dc78e53SAndroid Build Coastguard Worker } 2433*4dc78e53SAndroid Build Coastguard Worker 2434*4dc78e53SAndroid Build Coastguard Worker if (attrs[ATTR_BAR]) { 2435*4dc78e53SAndroid Build Coastguard Worker struct *nested[NESTED_MAX+1]; 2436*4dc78e53SAndroid Build Coastguard Worker 2437*4dc78e53SAndroid Build Coastguard Worker /* 2438*4dc78e53SAndroid Build Coastguard Worker * Attributes nested in a container can be parsed the same way 2439*4dc78e53SAndroid Build Coastguard Worker * as top level attributes. 2440*4dc78e53SAndroid Build Coastguard Worker */ 2441*4dc78e53SAndroid Build Coastguard Worker err = nla_parse_nested(nested, NESTED_MAX, attrs[ATTR_BAR], 2442*4dc78e53SAndroid Build Coastguard Worker nested_policy); 2443*4dc78e53SAndroid Build Coastguard Worker if (err < 0) 2444*4dc78e53SAndroid Build Coastguard Worker goto errout; 2445*4dc78e53SAndroid Build Coastguard Worker 2446*4dc78e53SAndroid Build Coastguard Worker // Process nested attributes here. 2447*4dc78e53SAndroid Build Coastguard Worker } 2448*4dc78e53SAndroid Build Coastguard Worker 2449*4dc78e53SAndroid Build Coastguard Worker err = 0; 2450*4dc78e53SAndroid Build Coastguard Workererrout: 2451*4dc78e53SAndroid Build Coastguard Worker return err; 2452*4dc78e53SAndroid Build Coastguard Worker} 2453*4dc78e53SAndroid Build Coastguard Worker-------- 2454*4dc78e53SAndroid Build Coastguard Worker 2455*4dc78e53SAndroid Build Coastguard Worker[[core_cb]] 2456*4dc78e53SAndroid Build Coastguard Worker== Callback Configurations 2457*4dc78e53SAndroid Build Coastguard Worker 2458*4dc78e53SAndroid Build Coastguard WorkerCallback hooks and overwriting capabilities are provided in various places 2459*4dc78e53SAndroid Build Coastguard Workerinside the library to control the behaviour of several functions. All the 2460*4dc78e53SAndroid Build Coastguard Workercallback and overwrite functions are packed together in struct nl_cb which 2461*4dc78e53SAndroid Build Coastguard Workeris attached to a netlink socket or passed on to functions directly. 2462*4dc78e53SAndroid Build Coastguard Worker 2463*4dc78e53SAndroid Build Coastguard Worker=== Callback Hooks 2464*4dc78e53SAndroid Build Coastguard Worker 2465*4dc78e53SAndroid Build Coastguard WorkerCallback hooks are spread across the library to provide entry points for 2466*4dc78e53SAndroid Build Coastguard Workermessage processing and to take action upon certain events. 2467*4dc78e53SAndroid Build Coastguard Worker 2468*4dc78e53SAndroid Build Coastguard WorkerCallback functions may return the following return codes: 2469*4dc78e53SAndroid Build Coastguard Worker[options="header", cols="1m,4"] 2470*4dc78e53SAndroid Build Coastguard Worker|======================================================================== 2471*4dc78e53SAndroid Build Coastguard Worker| Return Code | Description 2472*4dc78e53SAndroid Build Coastguard Worker| NL_OK | Proceed. 2473*4dc78e53SAndroid Build Coastguard Worker| NL_SKIP | Skip message currently being processed and continue 2474*4dc78e53SAndroid Build Coastguard Worker parsing the receive buffer. 2475*4dc78e53SAndroid Build Coastguard Worker| NL_STOP | Stop parsing and discard all remaining data in the 2476*4dc78e53SAndroid Build Coastguard Worker receive buffer. 2477*4dc78e53SAndroid Build Coastguard Worker|======================================================================== 2478*4dc78e53SAndroid Build Coastguard Worker 2479*4dc78e53SAndroid Build Coastguard Worker.Default Callback Implementations 2480*4dc78e53SAndroid Build Coastguard Worker 2481*4dc78e53SAndroid Build Coastguard WorkerThe library provides three sets of default callback implementations: 2482*4dc78e53SAndroid Build Coastguard Worker* +NL_CB_DEFAULT+ This is the default set. It implements the default behaviour. 2483*4dc78e53SAndroid Build Coastguard Worker See the table below for more information on the return codes of each 2484*4dc78e53SAndroid Build Coastguard Worker function. 2485*4dc78e53SAndroid Build Coastguard Worker* +NL_CB_VERBOSE+ This set is based on the default set but will cause an 2486*4dc78e53SAndroid Build Coastguard Worker error message to be printed to stderr for error messages, invalid 2487*4dc78e53SAndroid Build Coastguard Worker messages, message overruns and unhandled valid messages. The 2488*4dc78e53SAndroid Build Coastguard Worker +arg+ pointer in nl_cb_set() and nl_cb_err() can be used to 2489*4dc78e53SAndroid Build Coastguard Worker provide a FILE * which overwrites stderr. 2490*4dc78e53SAndroid Build Coastguard Worker* +NL_CB_DEBUG+ This set is intended for debugging purposes. It is 2491*4dc78e53SAndroid Build Coastguard Worker based on the verbose set but will decode and dump each message sent 2492*4dc78e53SAndroid Build Coastguard Worker or received to the console. 2493*4dc78e53SAndroid Build Coastguard Worker 2494*4dc78e53SAndroid Build Coastguard Worker.Message Processing Callbacks 2495*4dc78e53SAndroid Build Coastguard Worker 2496*4dc78e53SAndroid Build Coastguard Worker.nl_sendmsg() callback hooks: 2497*4dc78e53SAndroid Build Coastguard Worker[cols="2m,4e,1m", options="header"] 2498*4dc78e53SAndroid Build Coastguard Worker|============================================================================ 2499*4dc78e53SAndroid Build Coastguard Worker| Callback ID | Description | Default Return Value 2500*4dc78e53SAndroid Build Coastguard Worker| NL_CB_MSG_OUT | Each message sent | NL_OK 2501*4dc78e53SAndroid Build Coastguard Worker|============================================================================ 2502*4dc78e53SAndroid Build Coastguard Worker 2503*4dc78e53SAndroid Build Coastguard WorkerAny function called by NL_CB_MSG_OUT may return a negative error code to 2504*4dc78e53SAndroid Build Coastguard Workerprevent the message from being sent and the error code being returned. 2505*4dc78e53SAndroid Build Coastguard Worker 2506*4dc78e53SAndroid Build Coastguard Workernl_recvmsgs() callback hooks (ordered by priority): 2507*4dc78e53SAndroid Build Coastguard Worker[cols="2m,4e,1m", options="header"] 2508*4dc78e53SAndroid Build Coastguard Worker|============================================================================ 2509*4dc78e53SAndroid Build Coastguard Worker| Callback ID | Description | Default Return Value 2510*4dc78e53SAndroid Build Coastguard Worker| NL_CB_MSG_IN | Each message received | NL_OK 2511*4dc78e53SAndroid Build Coastguard Worker| NL_CB_SEQ_CHECK | May overwrite sequence check algo | NL_OK 2512*4dc78e53SAndroid Build Coastguard Worker| NL_CB_INVALID | Invalid messages | NL_STOP 2513*4dc78e53SAndroid Build Coastguard Worker| NL_CB_SEND_ACK | Messages with NLM_F_ACK flag set | NL_OK 2514*4dc78e53SAndroid Build Coastguard Worker| NL_CB_FINISH | Messages of type NLMSG_DONE | NL_STOP 2515*4dc78e53SAndroid Build Coastguard Worker| NL_CB_SKIPPED | Messages of type NLMSG_NOOP | NL_SKIP 2516*4dc78e53SAndroid Build Coastguard Worker| NL_CB_OVERRUN | Messages of type NLMSG_OVERRUN | NL_STOP 2517*4dc78e53SAndroid Build Coastguard Worker| NL_CB_ACK | ACK Messages | NL_STOP 2518*4dc78e53SAndroid Build Coastguard Worker| NL_CB_VALID | Each valid message | NL_OK 2519*4dc78e53SAndroid Build Coastguard Worker|============================================================================ 2520*4dc78e53SAndroid Build Coastguard Worker 2521*4dc78e53SAndroid Build Coastguard WorkerAny of these functions may return NL_OK, NL_SKIP, or NL_STOP. 2522*4dc78e53SAndroid Build Coastguard Worker 2523*4dc78e53SAndroid Build Coastguard WorkerMessage processing callback functions are set with nl_cb_set(): 2524*4dc78e53SAndroid Build Coastguard Worker[source,c] 2525*4dc78e53SAndroid Build Coastguard Worker-------- 2526*4dc78e53SAndroid Build Coastguard Worker#include <netlink/handlers.h> 2527*4dc78e53SAndroid Build Coastguard Worker 2528*4dc78e53SAndroid Build Coastguard Workerint nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, 2529*4dc78e53SAndroid Build Coastguard Worker nl_recvmsg_msg_cb_t func, void *arg); 2530*4dc78e53SAndroid Build Coastguard Worker 2531*4dc78e53SAndroid Build Coastguard Workertypedef int (*nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg); 2532*4dc78e53SAndroid Build Coastguard Worker-------- 2533*4dc78e53SAndroid Build Coastguard Worker 2534*4dc78e53SAndroid Build Coastguard Worker.Callback for Error Messages 2535*4dc78e53SAndroid Build Coastguard Worker 2536*4dc78e53SAndroid Build Coastguard WorkerA special function prototype is used for the error message callback hook: 2537*4dc78e53SAndroid Build Coastguard Worker 2538*4dc78e53SAndroid Build Coastguard Worker[source,c] 2539*4dc78e53SAndroid Build Coastguard Worker-------- 2540*4dc78e53SAndroid Build Coastguard Worker#include <netlink/handlers.h> 2541*4dc78e53SAndroid Build Coastguard Worker 2542*4dc78e53SAndroid Build Coastguard Workerint nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg); 2543*4dc78e53SAndroid Build Coastguard Worker 2544*4dc78e53SAndroid Build Coastguard Workertypedef int(* nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg); 2545*4dc78e53SAndroid Build Coastguard Worker-------- 2546*4dc78e53SAndroid Build Coastguard Worker 2547*4dc78e53SAndroid Build Coastguard Worker.Example: Setting up a callback set 2548*4dc78e53SAndroid Build Coastguard Worker[source,c] 2549*4dc78e53SAndroid Build Coastguard Worker-------- 2550*4dc78e53SAndroid Build Coastguard Worker#include <netlink/handlers.h> 2551*4dc78e53SAndroid Build Coastguard Worker 2552*4dc78e53SAndroid Build Coastguard Worker/* Allocate a callback set and initialize it to the verbose default set */ 2553*4dc78e53SAndroid Build Coastguard Workerstruct nl_cb *cb = nl_cb_alloc(NL_CB_VERBOSE); 2554*4dc78e53SAndroid Build Coastguard Worker 2555*4dc78e53SAndroid Build Coastguard Worker/* Modify the set to call my_func() for all valid messages */ 2556*4dc78e53SAndroid Build Coastguard Workernl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, my_func, NULL); 2557*4dc78e53SAndroid Build Coastguard Worker 2558*4dc78e53SAndroid Build Coastguard Worker/* 2559*4dc78e53SAndroid Build Coastguard Worker * Set the error message handler to the verbose default implementation 2560*4dc78e53SAndroid Build Coastguard Worker * and direct it to print all errors to the given file descriptor. 2561*4dc78e53SAndroid Build Coastguard Worker */ 2562*4dc78e53SAndroid Build Coastguard WorkerFILE *file = fopen(...); 2563*4dc78e53SAndroid Build Coastguard Workernl_cb_err(cb, NL_CB_VERBOSE, NULL, file); 2564*4dc78e53SAndroid Build Coastguard Worker-------- 2565*4dc78e53SAndroid Build Coastguard Worker 2566*4dc78e53SAndroid Build Coastguard Worker=== Overwriting of Internal Functions 2567*4dc78e53SAndroid Build Coastguard Worker 2568*4dc78e53SAndroid Build Coastguard WorkerWhen the library needs to send or receive netlink messages in high level 2569*4dc78e53SAndroid Build Coastguard Workerinterfaces it does so by calling its own low level API. In the case the 2570*4dc78e53SAndroid Build Coastguard Workerdefault characteristics are not sufficient for the application, it may 2571*4dc78e53SAndroid Build Coastguard Workeroverwrite several internal function calls with own implementations. 2572*4dc78e53SAndroid Build Coastguard Worker 2573*4dc78e53SAndroid Build Coastguard Worker.Overwriting recvmsgs() 2574*4dc78e53SAndroid Build Coastguard Worker 2575*4dc78e53SAndroid Build Coastguard WorkerSee <<core_recv, Receiving Netlink Messages>> for more information on 2576*4dc78e53SAndroid Build Coastguard Workerhow and when recvmsgs() is called internally. 2577*4dc78e53SAndroid Build Coastguard Worker 2578*4dc78e53SAndroid Build Coastguard Worker[source,c] 2579*4dc78e53SAndroid Build Coastguard Worker-------- 2580*4dc78e53SAndroid Build Coastguard Worker#include <netlink/handlers.h> 2581*4dc78e53SAndroid Build Coastguard Worker 2582*4dc78e53SAndroid Build Coastguard Workervoid nl_cb_overwrite_recvmsgs(struct nl_cb *cb, 2583*4dc78e53SAndroid Build Coastguard Worker int (*func)(struct nl_sock *sk, struct nl_cb *cb)); 2584*4dc78e53SAndroid Build Coastguard Worker-------- 2585*4dc78e53SAndroid Build Coastguard Worker 2586*4dc78e53SAndroid Build Coastguard WorkerThe following criterias must be met if a recvmsgs() implementation is 2587*4dc78e53SAndroid Build Coastguard Workersupposed to work with high level interfaces: 2588*4dc78e53SAndroid Build Coastguard Worker 2589*4dc78e53SAndroid Build Coastguard Worker- MUST respect the callback configuration +cb+, therefore: 2590*4dc78e53SAndroid Build Coastguard Worker - MUST call +NL_CB_VALID+ for all valid messages, passing on 2591*4dc78e53SAndroid Build Coastguard Worker - MUST call +NL_CB_ACK+ for all ACK messages 2592*4dc78e53SAndroid Build Coastguard Worker - MUST correctly handle multipart messages, calling NL_CB_VALID for 2593*4dc78e53SAndroid Build Coastguard Worker each message until a NLMSG_DONE message is received. 2594*4dc78e53SAndroid Build Coastguard Worker- MUST report error code if a NLMSG_ERROR or NLMSG_OVERRUN mesasge is 2595*4dc78e53SAndroid Build Coastguard Worker received. 2596*4dc78e53SAndroid Build Coastguard Worker 2597*4dc78e53SAndroid Build Coastguard Worker.Overwriting nl_recv() 2598*4dc78e53SAndroid Build Coastguard Worker 2599*4dc78e53SAndroid Build Coastguard WorkerOften it is sufficient to overwrite `nl_recv()` which is responsible 2600*4dc78e53SAndroid Build Coastguard Workerfrom receiving the actual data from the socket instead of replacing 2601*4dc78e53SAndroid Build Coastguard Workerthe complete `recvmsgs()` logic. 2602*4dc78e53SAndroid Build Coastguard Worker 2603*4dc78e53SAndroid Build Coastguard WorkerSee <<core_recv_character, Receive Characteristics>> for more 2604*4dc78e53SAndroid Build Coastguard Workerinformation on how and when `nl_recv()` is called internally. 2605*4dc78e53SAndroid Build Coastguard Worker 2606*4dc78e53SAndroid Build Coastguard Worker[source,c] 2607*4dc78e53SAndroid Build Coastguard Worker-------- 2608*4dc78e53SAndroid Build Coastguard Worker#include <netlink/handlers.h> 2609*4dc78e53SAndroid Build Coastguard Worker 2610*4dc78e53SAndroid Build Coastguard Workervoid nl_cb_overwrite_recv(struct nl_cb *cb, 2611*4dc78e53SAndroid Build Coastguard Worker int (*func)(struct nl_sock * sk, 2612*4dc78e53SAndroid Build Coastguard Worker struct sockaddr_nl *addr, 2613*4dc78e53SAndroid Build Coastguard Worker unsigned char **buf, 2614*4dc78e53SAndroid Build Coastguard Worker struct ucred **cred)); 2615*4dc78e53SAndroid Build Coastguard Worker-------- 2616*4dc78e53SAndroid Build Coastguard Worker 2617*4dc78e53SAndroid Build Coastguard WorkerThe following criteras must be met for an own `nl_recv()` 2618*4dc78e53SAndroid Build Coastguard Workerimplementation: 2619*4dc78e53SAndroid Build Coastguard Worker 2620*4dc78e53SAndroid Build Coastguard Worker - *MUST* return the number of bytes read or a negative error code if 2621*4dc78e53SAndroid Build Coastguard Worker an error occurred. The function may also return 0 to indicate that 2622*4dc78e53SAndroid Build Coastguard Worker no data has been read. 2623*4dc78e53SAndroid Build Coastguard Worker - *MUST* set `*buf` to a buffer containing the data read. It must be 2624*4dc78e53SAndroid Build Coastguard Worker safe for the caller to access the number of bytes read returned as 2625*4dc78e53SAndroid Build Coastguard Worker return code. 2626*4dc78e53SAndroid Build Coastguard Worker - *MAY* fill out `*addr` with the netlink address of the peer the 2627*4dc78e53SAndroid Build Coastguard Worker data has been received from. 2628*4dc78e53SAndroid Build Coastguard Worker - *MAY* set `*cred` to a newly allocated struct ucred containing 2629*4dc78e53SAndroid Build Coastguard Worker credentials. 2630*4dc78e53SAndroid Build Coastguard Worker 2631*4dc78e53SAndroid Build Coastguard Worker.Overwriting nl_send() 2632*4dc78e53SAndroid Build Coastguard Worker 2633*4dc78e53SAndroid Build Coastguard WorkerSee <<core_send, Sending Netlink Messages>> for more information on 2634*4dc78e53SAndroid Build Coastguard Workerhow and when nl_send() is called internally. 2635*4dc78e53SAndroid Build Coastguard Worker 2636*4dc78e53SAndroid Build Coastguard Worker[source,c] 2637*4dc78e53SAndroid Build Coastguard Worker-------- 2638*4dc78e53SAndroid Build Coastguard Worker#include <netlink/handlers.h> 2639*4dc78e53SAndroid Build Coastguard Worker 2640*4dc78e53SAndroid Build Coastguard Workervoid nl_cb_overwrite_send(struct nl_cb *cb, int (*func)(struct nl_sock *sk, 2641*4dc78e53SAndroid Build Coastguard Worker struct nl_msg *msg)); 2642*4dc78e53SAndroid Build Coastguard Worker-------- 2643*4dc78e53SAndroid Build Coastguard Worker 2644*4dc78e53SAndroid Build Coastguard WorkerOwn implementations must send the netlink message and return 0 on success 2645*4dc78e53SAndroid Build Coastguard Workeror a negative error code. 2646*4dc78e53SAndroid Build Coastguard Worker 2647*4dc78e53SAndroid Build Coastguard Worker[[core_cache]] 2648*4dc78e53SAndroid Build Coastguard Worker== Cache System 2649*4dc78e53SAndroid Build Coastguard Worker 2650*4dc78e53SAndroid Build Coastguard Worker=== Allocation of Caches 2651*4dc78e53SAndroid Build Coastguard Worker 2652*4dc78e53SAndroid Build Coastguard WorkerAlmost all subsystem provide a function to allocate a new cache 2653*4dc78e53SAndroid Build Coastguard Workerof some form. The function usually looks like this: 2654*4dc78e53SAndroid Build Coastguard Worker[source,c] 2655*4dc78e53SAndroid Build Coastguard Worker-------- 2656*4dc78e53SAndroid Build Coastguard Workerstruct nl_cache *<object name>_alloc_cache(struct nl_sock *sk); 2657*4dc78e53SAndroid Build Coastguard Worker-------- 2658*4dc78e53SAndroid Build Coastguard Worker 2659*4dc78e53SAndroid Build Coastguard WorkerThese functions allocate a new cache for the own object type, 2660*4dc78e53SAndroid Build Coastguard Workerinitializes it properly and updates it to represent the current 2661*4dc78e53SAndroid Build Coastguard Workerstate of their master, e.g. a link cache would include all 2662*4dc78e53SAndroid Build Coastguard Workerlinks currently configured in the kernel. 2663*4dc78e53SAndroid Build Coastguard Worker 2664*4dc78e53SAndroid Build Coastguard WorkerSome of the allocation functions may take additional arguments 2665*4dc78e53SAndroid Build Coastguard Workerto further specify what will be part of the cache. 2666*4dc78e53SAndroid Build Coastguard Worker 2667*4dc78e53SAndroid Build Coastguard WorkerAll such functions return a newly allocated cache or NULL 2668*4dc78e53SAndroid Build Coastguard Workerin case of an error. 2669*4dc78e53SAndroid Build Coastguard Worker 2670*4dc78e53SAndroid Build Coastguard Worker=== Cache Manager 2671*4dc78e53SAndroid Build Coastguard Worker 2672*4dc78e53SAndroid Build Coastguard WorkerThe purpose of a cache manager is to keep track of caches and 2673*4dc78e53SAndroid Build Coastguard Workerautomatically receive event notifications to keep the caches 2674*4dc78e53SAndroid Build Coastguard Workerup to date with the kernel state. Each manager has exactly one 2675*4dc78e53SAndroid Build Coastguard Workernetlink socket assigned which limits the scope of each manager 2676*4dc78e53SAndroid Build Coastguard Workerto exactly one netlink family. Therefore all caches committed 2677*4dc78e53SAndroid Build Coastguard Workerto a manager must be part of the same netlink family. Due to the 2678*4dc78e53SAndroid Build Coastguard Workernature of a manager, it is not possible to have a cache maintain 2679*4dc78e53SAndroid Build Coastguard Workertwo instances of the same cache type. The socket is subscribed 2680*4dc78e53SAndroid Build Coastguard Workerto the event notification group of each cache and also put into 2681*4dc78e53SAndroid Build Coastguard Workernon-blocking mode. Functions exist to poll() on the socket to 2682*4dc78e53SAndroid Build Coastguard Workerwait for new events to be received. 2683*4dc78e53SAndroid Build Coastguard Worker 2684*4dc78e53SAndroid Build Coastguard Worker 2685*4dc78e53SAndroid Build Coastguard Worker---- 2686*4dc78e53SAndroid Build Coastguard Worker App libnl Kernel 2687*4dc78e53SAndroid Build Coastguard Worker | | 2688*4dc78e53SAndroid Build Coastguard Worker +-----------------+ [ notification, link change ] 2689*4dc78e53SAndroid Build Coastguard Worker | | Cache Manager | | [ (IFF_UP | IFF_RUNNING) ] 2690*4dc78e53SAndroid Build Coastguard Worker | | | 2691*4dc78e53SAndroid Build Coastguard Worker | | +------------+| | | [ notification, new addr ] 2692*4dc78e53SAndroid Build Coastguard Worker <-------|---| route/link |<-------(async)--+ [ 10.0.1.1/32 dev eth1 ] 2693*4dc78e53SAndroid Build Coastguard Worker | | +------------+| | | 2694*4dc78e53SAndroid Build Coastguard Worker | +------------+| | 2695*4dc78e53SAndroid Build Coastguard Worker <---|---|---| route/addr |<------|-(async)--------------+ 2696*4dc78e53SAndroid Build Coastguard Worker | +------------+| 2697*4dc78e53SAndroid Build Coastguard Worker | | +------------+| | 2698*4dc78e53SAndroid Build Coastguard Worker <-------|---| ... || 2699*4dc78e53SAndroid Build Coastguard Worker | | +------------+| | 2700*4dc78e53SAndroid Build Coastguard Worker +-----------------+ 2701*4dc78e53SAndroid Build Coastguard Worker | | 2702*4dc78e53SAndroid Build Coastguard Worker---- 2703*4dc78e53SAndroid Build Coastguard Worker 2704*4dc78e53SAndroid Build Coastguard Worker.Creating a new cache manager 2705*4dc78e53SAndroid Build Coastguard Worker 2706*4dc78e53SAndroid Build Coastguard Worker[source,c] 2707*4dc78e53SAndroid Build Coastguard Worker---- 2708*4dc78e53SAndroid Build Coastguard Workerstruct nl_cache_mngr *mngr; 2709*4dc78e53SAndroid Build Coastguard Worker 2710*4dc78e53SAndroid Build Coastguard Worker// Allocate a new cache manager for RTNETLINK and automatically 2711*4dc78e53SAndroid Build Coastguard Worker// provide the caches added to the manager. 2712*4dc78e53SAndroid Build Coastguard Workererr = nl_cache_mngr_alloc(NULL, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr); 2713*4dc78e53SAndroid Build Coastguard Worker---- 2714*4dc78e53SAndroid Build Coastguard Worker 2715*4dc78e53SAndroid Build Coastguard Worker.Keep track of a cache 2716*4dc78e53SAndroid Build Coastguard Worker 2717*4dc78e53SAndroid Build Coastguard Worker[source,c] 2718*4dc78e53SAndroid Build Coastguard Worker---- 2719*4dc78e53SAndroid Build Coastguard Workerstruct nl_cache *cache; 2720*4dc78e53SAndroid Build Coastguard Worker 2721*4dc78e53SAndroid Build Coastguard Worker// Create a new cache for links/interfaces and ask the manager to 2722*4dc78e53SAndroid Build Coastguard Worker// keep it up to date for us. This will trigger a full dump request 2723*4dc78e53SAndroid Build Coastguard Worker// to initially fill the cache. 2724*4dc78e53SAndroid Build Coastguard Workercache = nl_cache_mngr_add(mngr, "route/link"); 2725*4dc78e53SAndroid Build Coastguard Worker----- 2726*4dc78e53SAndroid Build Coastguard Worker 2727*4dc78e53SAndroid Build Coastguard Worker.Make the manager receive updates 2728*4dc78e53SAndroid Build Coastguard Worker 2729*4dc78e53SAndroid Build Coastguard Worker[source,c] 2730*4dc78e53SAndroid Build Coastguard Worker---- 2731*4dc78e53SAndroid Build Coastguard Worker// Give the manager the ability to receive updates, will call poll() 2732*4dc78e53SAndroid Build Coastguard Worker// with a timeout of 5 seconds. 2733*4dc78e53SAndroid Build Coastguard Workerif (nl_cache_mngr_poll(mngr, 5000) > 0) { 2734*4dc78e53SAndroid Build Coastguard Worker // Manager received at least one update, dump cache? 2735*4dc78e53SAndroid Build Coastguard Worker nl_cache_dump(cache, ...); 2736*4dc78e53SAndroid Build Coastguard Worker} 2737*4dc78e53SAndroid Build Coastguard Worker---- 2738*4dc78e53SAndroid Build Coastguard Worker 2739*4dc78e53SAndroid Build Coastguard Worker.Release cache manager 2740*4dc78e53SAndroid Build Coastguard Worker 2741*4dc78e53SAndroid Build Coastguard Worker[source,c] 2742*4dc78e53SAndroid Build Coastguard Worker---- 2743*4dc78e53SAndroid Build Coastguard Workernl_cache_mngr_free(mngr); 2744*4dc78e53SAndroid Build Coastguard Worker---- 2745*4dc78e53SAndroid Build Coastguard Worker 2746*4dc78e53SAndroid Build Coastguard Worker== Abstract Data Types 2747*4dc78e53SAndroid Build Coastguard Worker 2748*4dc78e53SAndroid Build Coastguard WorkerA few high level abstract data types which are used by a majority netlink 2749*4dc78e53SAndroid Build Coastguard Workerprotocols are implemented in the core library. More may be added in the 2750*4dc78e53SAndroid Build Coastguard Workerfuture if the need arises. 2751*4dc78e53SAndroid Build Coastguard Worker 2752*4dc78e53SAndroid Build Coastguard Worker=== Abstract Address 2753*4dc78e53SAndroid Build Coastguard Worker 2754*4dc78e53SAndroid Build Coastguard WorkerMost netlink protocols deal with networking related topics and thus 2755*4dc78e53SAndroid Build Coastguard Workerdealing with network addresses is a common task. 2756*4dc78e53SAndroid Build Coastguard Worker 2757*4dc78e53SAndroid Build Coastguard WorkerCurrently the following address families are supported: 2758*4dc78e53SAndroid Build Coastguard Worker 2759*4dc78e53SAndroid Build Coastguard Worker[options="compact"] 2760*4dc78e53SAndroid Build Coastguard Worker * `AF_INET` 2761*4dc78e53SAndroid Build Coastguard Worker * `AF_INET6` 2762*4dc78e53SAndroid Build Coastguard Worker * `AF_LLC` 2763*4dc78e53SAndroid Build Coastguard Worker * `AF_DECnet` 2764*4dc78e53SAndroid Build Coastguard Worker * `AF_UNSPEC` 2765*4dc78e53SAndroid Build Coastguard Worker 2766*4dc78e53SAndroid Build Coastguard Worker[[core_addr_alloc]] 2767*4dc78e53SAndroid Build Coastguard Worker.Address Allocation 2768*4dc78e53SAndroid Build Coastguard Worker 2769*4dc78e53SAndroid Build Coastguard WorkerThe function nl_addr_alloc() allocates a new empty address. The 2770*4dc78e53SAndroid Build Coastguard Worker+maxsize+ argument defines the maximum length of an address in bytes. 2771*4dc78e53SAndroid Build Coastguard WorkerThe size of an address is address family specific. If the address 2772*4dc78e53SAndroid Build Coastguard Workerfamily and address data are known at allocation time the function 2773*4dc78e53SAndroid Build Coastguard Workernl_addr_build() can be used alternatively. You may also clone 2774*4dc78e53SAndroid Build Coastguard Workeran address by calling nl_addr_clone() 2775*4dc78e53SAndroid Build Coastguard Worker 2776*4dc78e53SAndroid Build Coastguard Worker[source,c] 2777*4dc78e53SAndroid Build Coastguard Worker-------- 2778*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2779*4dc78e53SAndroid Build Coastguard Worker 2780*4dc78e53SAndroid Build Coastguard Workerstruct nl_addr *nl_addr_alloc(size_t maxsize); 2781*4dc78e53SAndroid Build Coastguard Workerstruct nl_addr *nl_addr_clone(struct nl_addr *addr); 2782*4dc78e53SAndroid Build Coastguard Workerstruct nl_addr *nl_addr_build(int family, void *addr, size_t size); 2783*4dc78e53SAndroid Build Coastguard Worker-------- 2784*4dc78e53SAndroid Build Coastguard Worker 2785*4dc78e53SAndroid Build Coastguard WorkerIf the address is transported in a netlink attribute, the function 2786*4dc78e53SAndroid Build Coastguard Workernl_addr_alloc_attr() allocates a new address based on the payload 2787*4dc78e53SAndroid Build Coastguard Workerof the attribute provided. The +family+ argument is used to specify 2788*4dc78e53SAndroid Build Coastguard Workerthe address family of the address, set to +AF_UNSPEC+ if unknown. 2789*4dc78e53SAndroid Build Coastguard Worker 2790*4dc78e53SAndroid Build Coastguard Worker[source,c] 2791*4dc78e53SAndroid Build Coastguard Worker-------- 2792*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2793*4dc78e53SAndroid Build Coastguard Worker 2794*4dc78e53SAndroid Build Coastguard Workerstruct nl_addr *nl_addr_alloc_attr(struct nlattr *attr, int family); 2795*4dc78e53SAndroid Build Coastguard Worker-------- 2796*4dc78e53SAndroid Build Coastguard Worker 2797*4dc78e53SAndroid Build Coastguard WorkerIf the address is provided by a user, it is usually stored in a human 2798*4dc78e53SAndroid Build Coastguard Workerreadable format. The function nl_addr_parse() parses a character 2799*4dc78e53SAndroid Build Coastguard Workerstring representing an address and allocates a new address based on 2800*4dc78e53SAndroid Build Coastguard Workerit. 2801*4dc78e53SAndroid Build Coastguard Worker 2802*4dc78e53SAndroid Build Coastguard Worker[source,c] 2803*4dc78e53SAndroid Build Coastguard Worker-------- 2804*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2805*4dc78e53SAndroid Build Coastguard Worker 2806*4dc78e53SAndroid Build Coastguard Workerint nl_addr_parse(const char *addr, int hint, struct nl_addr **result); 2807*4dc78e53SAndroid Build Coastguard Worker-------- 2808*4dc78e53SAndroid Build Coastguard Worker 2809*4dc78e53SAndroid Build Coastguard WorkerIf parsing succeeds the function returns 0 and the allocated address 2810*4dc78e53SAndroid Build Coastguard Workeris stored in +*result+. 2811*4dc78e53SAndroid Build Coastguard Worker 2812*4dc78e53SAndroid Build Coastguard WorkerNOTE: Make sure to return the reference to an address using 2813*4dc78e53SAndroid Build Coastguard Worker `nl_addr_put()` after usage to allow memory being freed. 2814*4dc78e53SAndroid Build Coastguard Worker 2815*4dc78e53SAndroid Build Coastguard Worker.Example: Transform character string to abstract address 2816*4dc78e53SAndroid Build Coastguard Worker[source,c] 2817*4dc78e53SAndroid Build Coastguard Worker----- 2818*4dc78e53SAndroid Build Coastguard Workerstruct nl_addr *a = nl_addr_parse("::1", AF_UNSPEC); 2819*4dc78e53SAndroid Build Coastguard Workerprintf("Address family: %s\n", nl_af2str(nl_addr_get_family(a))); 2820*4dc78e53SAndroid Build Coastguard Workernl_addr_put(a); 2821*4dc78e53SAndroid Build Coastguard Workera = nl_addr_parse("11:22:33:44:55:66", AF_UNSPEC); 2822*4dc78e53SAndroid Build Coastguard Workerprintf("Address family: %s\n", nl_af2str(nl_addr_get_family(a))); 2823*4dc78e53SAndroid Build Coastguard Workernl_addr_put(a); 2824*4dc78e53SAndroid Build Coastguard Worker----- 2825*4dc78e53SAndroid Build Coastguard Worker 2826*4dc78e53SAndroid Build Coastguard Worker.Address References 2827*4dc78e53SAndroid Build Coastguard Worker 2828*4dc78e53SAndroid Build Coastguard WorkerAbstract addresses use reference counting to account for all users of 2829*4dc78e53SAndroid Build Coastguard Workera particular address. After the last user has returned the reference 2830*4dc78e53SAndroid Build Coastguard Workerthe address is freed. 2831*4dc78e53SAndroid Build Coastguard Worker 2832*4dc78e53SAndroid Build Coastguard WorkerIf you pass on an address object to another function and you are not 2833*4dc78e53SAndroid Build Coastguard Workersure how long it will be used, make sure to call nl_addr_get() to 2834*4dc78e53SAndroid Build Coastguard Workeracquire an additional reference and have that function or code path 2835*4dc78e53SAndroid Build Coastguard Workercall nl_addr_put() as soon as it has finished using the address. 2836*4dc78e53SAndroid Build Coastguard Worker 2837*4dc78e53SAndroid Build Coastguard Worker[source,c] 2838*4dc78e53SAndroid Build Coastguard Worker-------- 2839*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2840*4dc78e53SAndroid Build Coastguard Worker 2841*4dc78e53SAndroid Build Coastguard Workerstruct nl_addr *nl_addr_get(struct nl_addr *addr); 2842*4dc78e53SAndroid Build Coastguard Workervoid nl_addr_put(struct nl_addr *addr); 2843*4dc78e53SAndroid Build Coastguard Workerint nl_addr_shared(struct nl_addr *addr); 2844*4dc78e53SAndroid Build Coastguard Worker-------- 2845*4dc78e53SAndroid Build Coastguard Worker 2846*4dc78e53SAndroid Build Coastguard WorkerYou may call nl_addr_shared() at any time to check if you are the only 2847*4dc78e53SAndroid Build Coastguard Workeruser of an address. 2848*4dc78e53SAndroid Build Coastguard Worker 2849*4dc78e53SAndroid Build Coastguard Worker.Address Attributes 2850*4dc78e53SAndroid Build Coastguard Worker 2851*4dc78e53SAndroid Build Coastguard WorkerThe address is usually set at allocation time. If it was unknown at that 2852*4dc78e53SAndroid Build Coastguard Workertime it can be specified later by calling nl_addr_set_family() and is 2853*4dc78e53SAndroid Build Coastguard Workeraccessed with the function nl_addr_get_family(). 2854*4dc78e53SAndroid Build Coastguard Worker 2855*4dc78e53SAndroid Build Coastguard Worker[source,c] 2856*4dc78e53SAndroid Build Coastguard Worker-------- 2857*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2858*4dc78e53SAndroid Build Coastguard Worker 2859*4dc78e53SAndroid Build Coastguard Workervoid nl_addr_set_family(struct nl_addr *addr, int family); 2860*4dc78e53SAndroid Build Coastguard Workerint nl_addr_get_family(struct nl_addr *addr); 2861*4dc78e53SAndroid Build Coastguard Worker-------- 2862*4dc78e53SAndroid Build Coastguard Worker 2863*4dc78e53SAndroid Build Coastguard WorkerThe same is true for the actual address data. It is typically present 2864*4dc78e53SAndroid Build Coastguard Workerat allocation time. For exceptions it can be specified later or 2865*4dc78e53SAndroid Build Coastguard Workeroverwritten with the function `nl_addr_set_binary_addr()`. Beware that 2866*4dc78e53SAndroid Build Coastguard Workerthe length of the address may not exceed `maxlen` specified at 2867*4dc78e53SAndroid Build Coastguard Workerallocation time. The address data is returned by the function 2868*4dc78e53SAndroid Build Coastguard Worker`nl_addr_get_binary_addr()` and its length by the function 2869*4dc78e53SAndroid Build Coastguard Worker`nl_addr_get_len()`. 2870*4dc78e53SAndroid Build Coastguard Worker 2871*4dc78e53SAndroid Build Coastguard Worker[source,c] 2872*4dc78e53SAndroid Build Coastguard Worker-------- 2873*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2874*4dc78e53SAndroid Build Coastguard Worker 2875*4dc78e53SAndroid Build Coastguard Workerint nl_addr_set_binary_addr(struct nl_addr *addr, void *data, size_t size); 2876*4dc78e53SAndroid Build Coastguard Workervoid *nl_addr_get_binary_addr(struct nl_addr *addr); 2877*4dc78e53SAndroid Build Coastguard Workerunsigned int nl_addr_get_len(struct nl_addr *addr); 2878*4dc78e53SAndroid Build Coastguard Worker-------- 2879*4dc78e53SAndroid Build Coastguard Worker 2880*4dc78e53SAndroid Build Coastguard WorkerIf you only want to check if the address data consists of all zeros 2881*4dc78e53SAndroid Build Coastguard Workerthe function `nl_addr_iszero()` is a shortcut to that. 2882*4dc78e53SAndroid Build Coastguard Worker 2883*4dc78e53SAndroid Build Coastguard Worker[source,c] 2884*4dc78e53SAndroid Build Coastguard Worker-------- 2885*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2886*4dc78e53SAndroid Build Coastguard Worker 2887*4dc78e53SAndroid Build Coastguard Workerint nl_addr_iszero(struct nl_addr *addr); 2888*4dc78e53SAndroid Build Coastguard Worker-------- 2889*4dc78e53SAndroid Build Coastguard Worker 2890*4dc78e53SAndroid Build Coastguard Worker==== Address Prefix Length 2891*4dc78e53SAndroid Build Coastguard Worker 2892*4dc78e53SAndroid Build Coastguard WorkerAlthough this functionality is somewhat specific to routing it has 2893*4dc78e53SAndroid Build Coastguard Workerbeen implemented here. Addresses can have a prefix length assigned 2894*4dc78e53SAndroid Build Coastguard Workerwhich implies that only the first n bits are of importance. This is 2895*4dc78e53SAndroid Build Coastguard Workerf.e. used to implement subnets. 2896*4dc78e53SAndroid Build Coastguard Worker 2897*4dc78e53SAndroid Build Coastguard WorkerUse set functions `nl_addr_set_prefixlen()` and 2898*4dc78e53SAndroid Build Coastguard Worker`nl_addr_get_prefixlen()` to work with prefix lengths. 2899*4dc78e53SAndroid Build Coastguard Worker 2900*4dc78e53SAndroid Build Coastguard Worker[source,c] 2901*4dc78e53SAndroid Build Coastguard Worker-------- 2902*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2903*4dc78e53SAndroid Build Coastguard Worker 2904*4dc78e53SAndroid Build Coastguard Workervoid nl_addr_set_prefixlen(struct nl_addr *addr, int n); 2905*4dc78e53SAndroid Build Coastguard Workerunsigned int nl_addr_get_prefixlen(struct nl_addr *addr); 2906*4dc78e53SAndroid Build Coastguard Worker-------- 2907*4dc78e53SAndroid Build Coastguard Worker 2908*4dc78e53SAndroid Build Coastguard WorkerNOTE: The default prefix length is set to (address length * 8) 2909*4dc78e53SAndroid Build Coastguard Worker 2910*4dc78e53SAndroid Build Coastguard Worker.Address Helpers 2911*4dc78e53SAndroid Build Coastguard Worker 2912*4dc78e53SAndroid Build Coastguard WorkerSeveral functions exist to help when dealing with addresses. The 2913*4dc78e53SAndroid Build Coastguard Workerfunction `nl_addr_cmp()` compares two addresses and returns an integer 2914*4dc78e53SAndroid Build Coastguard Workerless than, equal to or greater than zero without considering the 2915*4dc78e53SAndroid Build Coastguard Workerprefix length at all. If you want to consider the prefix length, use 2916*4dc78e53SAndroid Build Coastguard Workerthe function `nl_addr_cmp_prefix()`. 2917*4dc78e53SAndroid Build Coastguard Worker 2918*4dc78e53SAndroid Build Coastguard Worker[source,c] 2919*4dc78e53SAndroid Build Coastguard Worker-------- 2920*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2921*4dc78e53SAndroid Build Coastguard Worker 2922*4dc78e53SAndroid Build Coastguard Workerint nl_addr_cmp(struct nl_addr *addr, struct nl_addr *addr); 2923*4dc78e53SAndroid Build Coastguard Workerint nl_addr_cmp_prefix(struct nl_addr *addr, struct nl_addr *addr); 2924*4dc78e53SAndroid Build Coastguard Worker-------- 2925*4dc78e53SAndroid Build Coastguard Worker 2926*4dc78e53SAndroid Build Coastguard WorkerIf an abstract address needs to presented to the user it should be 2927*4dc78e53SAndroid Build Coastguard Workerdone in a human readable format which differs depending on the address 2928*4dc78e53SAndroid Build Coastguard Workerfamily. The function `nl_addr2str()` takes care of this by calling the 2929*4dc78e53SAndroid Build Coastguard Workerappropriate conversion functions internally. It expects a `buf` of 2930*4dc78e53SAndroid Build Coastguard Workerlength `size` to write the character string into and returns a pointer 2931*4dc78e53SAndroid Build Coastguard Workerto `buf` for easy `printf()` usage. 2932*4dc78e53SAndroid Build Coastguard Worker 2933*4dc78e53SAndroid Build Coastguard Worker[source,c] 2934*4dc78e53SAndroid Build Coastguard Worker-------- 2935*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2936*4dc78e53SAndroid Build Coastguard Worker 2937*4dc78e53SAndroid Build Coastguard Workerchar *nl_addr2str(struct nl_addr *addr, char *buf, size_t size); 2938*4dc78e53SAndroid Build Coastguard Worker-------- 2939*4dc78e53SAndroid Build Coastguard Worker 2940*4dc78e53SAndroid Build Coastguard WorkerIf the address family is unknown, the address data will be printed in 2941*4dc78e53SAndroid Build Coastguard Workerhexadecimal format `AA:BB:CC:DD:...` 2942*4dc78e53SAndroid Build Coastguard Worker 2943*4dc78e53SAndroid Build Coastguard WorkerOften the only way to figure out the address family is by looking at 2944*4dc78e53SAndroid Build Coastguard Workerthe length of the address. The function `nl_addr_guess_family()` does 2945*4dc78e53SAndroid Build Coastguard Workerjust this and returns the address family guessed based on the address 2946*4dc78e53SAndroid Build Coastguard Workersize. 2947*4dc78e53SAndroid Build Coastguard Worker 2948*4dc78e53SAndroid Build Coastguard Worker[source,c] 2949*4dc78e53SAndroid Build Coastguard Worker-------- 2950*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2951*4dc78e53SAndroid Build Coastguard Worker 2952*4dc78e53SAndroid Build Coastguard Workerint nl_addr_guess_family(struct nl_addr *addr); 2953*4dc78e53SAndroid Build Coastguard Worker-------- 2954*4dc78e53SAndroid Build Coastguard Worker 2955*4dc78e53SAndroid Build Coastguard WorkerBefore allocating an address you may want to check if the character 2956*4dc78e53SAndroid Build Coastguard Workerstring actually represents a valid address of the address family you 2957*4dc78e53SAndroid Build Coastguard Workerare expecting. The function `nl_addr_valid()` can be used for that, it 2958*4dc78e53SAndroid Build Coastguard Workerreturns 1 if the supplied `addr` is a valid address in the context of 2959*4dc78e53SAndroid Build Coastguard Worker`family`. See `inet_pton(3)`, `dnet_pton(3)` for more information on 2960*4dc78e53SAndroid Build Coastguard Workervalid address formats. 2961*4dc78e53SAndroid Build Coastguard Worker 2962*4dc78e53SAndroid Build Coastguard Worker[source,c] 2963*4dc78e53SAndroid Build Coastguard Worker-------- 2964*4dc78e53SAndroid Build Coastguard Worker#include <netlink/addr.h> 2965*4dc78e53SAndroid Build Coastguard Worker 2966*4dc78e53SAndroid Build Coastguard Workerint nl_addr_valid(char *addr, int family); 2967*4dc78e53SAndroid Build Coastguard Worker-------- 2968*4dc78e53SAndroid Build Coastguard Worker 2969*4dc78e53SAndroid Build Coastguard Worker=== Abstract Data 2970*4dc78e53SAndroid Build Coastguard Worker 2971*4dc78e53SAndroid Build Coastguard WorkerThe abstract data type is a trivial datatype with the primary purpose 2972*4dc78e53SAndroid Build Coastguard Workerto simplify usage of netlink attributes of arbitrary length. 2973*4dc78e53SAndroid Build Coastguard Worker 2974*4dc78e53SAndroid Build Coastguard Worker[[core_data_alloc]] 2975*4dc78e53SAndroid Build Coastguard Worker.Allocation of a Data Object 2976*4dc78e53SAndroid Build Coastguard WorkerThe function `nl_data_alloc()` alloctes a new abstract data object and 2977*4dc78e53SAndroid Build Coastguard Workerfills it with the provided data. `nl_data_alloc_attr()` does the same 2978*4dc78e53SAndroid Build Coastguard Workerbut bases the data on the payload of a netlink attribute. New data 2979*4dc78e53SAndroid Build Coastguard Workerobjects can also be allocated by cloning existing ones by using 2980*4dc78e53SAndroid Build Coastguard Worker`nl_data_clone()`. 2981*4dc78e53SAndroid Build Coastguard Worker 2982*4dc78e53SAndroid Build Coastguard Worker[source,c] 2983*4dc78e53SAndroid Build Coastguard Worker-------- 2984*4dc78e53SAndroid Build Coastguard Workerstruct nl_data *nl_data_alloc(void *buf, size_t size); 2985*4dc78e53SAndroid Build Coastguard Workerstruct nl_data *nl_data_alloc_attr(struct nlattr *attr); 2986*4dc78e53SAndroid Build Coastguard Workerstruct nl_data *nl_data_clone(struct nl_data *data); 2987*4dc78e53SAndroid Build Coastguard Workervoid nl_data_free(struct nl_data *data); 2988*4dc78e53SAndroid Build Coastguard Worker-------- 2989*4dc78e53SAndroid Build Coastguard Worker 2990*4dc78e53SAndroid Build Coastguard Worker.Access to Data 2991*4dc78e53SAndroid Build Coastguard Worker 2992*4dc78e53SAndroid Build Coastguard WorkerThe function `nl_data_get()` returns a pointer to the data, the size 2993*4dc78e53SAndroid Build Coastguard Workerof data is returned by `nl_data_get_size()`. 2994*4dc78e53SAndroid Build Coastguard Worker 2995*4dc78e53SAndroid Build Coastguard Worker[source,c] 2996*4dc78e53SAndroid Build Coastguard Worker-------- 2997*4dc78e53SAndroid Build Coastguard Workervoid *nl_data_get(struct nl_data *data); 2998*4dc78e53SAndroid Build Coastguard Workersize_t nl_data_get_size(struct nl_data *data); 2999*4dc78e53SAndroid Build Coastguard Worker-------- 3000*4dc78e53SAndroid Build Coastguard Worker 3001*4dc78e53SAndroid Build Coastguard Worker.Data Helpers 3002*4dc78e53SAndroid Build Coastguard Worker 3003*4dc78e53SAndroid Build Coastguard WorkerThe function nl_data_append() reallocates the internal data buffers 3004*4dc78e53SAndroid Build Coastguard Workerand appends the specified `buf` to the existing data. 3005*4dc78e53SAndroid Build Coastguard Worker 3006*4dc78e53SAndroid Build Coastguard Worker[source,c] 3007*4dc78e53SAndroid Build Coastguard Worker-------- 3008*4dc78e53SAndroid Build Coastguard Workerint nl_data_append(struct nl_data *data, void *buf, size_t size); 3009*4dc78e53SAndroid Build Coastguard Worker-------- 3010*4dc78e53SAndroid Build Coastguard Worker 3011*4dc78e53SAndroid Build Coastguard WorkerCAUTION: Any call to `nl_data_append()` invalidates all pointers 3012*4dc78e53SAndroid Build Coastguard Worker returned by `nl_data_get()` of the same data object. 3013*4dc78e53SAndroid Build Coastguard Worker 3014*4dc78e53SAndroid Build Coastguard Worker[source,c] 3015*4dc78e53SAndroid Build Coastguard Worker-------- 3016*4dc78e53SAndroid Build Coastguard Workerint nl_data_cmp(struct nl_data *data, struct nl_data *data); 3017*4dc78e53SAndroid Build Coastguard Worker-------- 3018