rxrpc.rst 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ======================
  3. RxRPC Network Protocol
  4. ======================
  5. The RxRPC protocol driver provides a reliable two-phase transport on top of UDP
  6. that can be used to perform RxRPC remote operations. This is done over sockets
  7. of AF_RXRPC family, using sendmsg() and recvmsg() with control data to send and
  8. receive data, aborts and errors.
  9. Contents of this document:
  10. (#) Overview.
  11. (#) RxRPC protocol summary.
  12. (#) AF_RXRPC driver model.
  13. (#) Control messages.
  14. (#) Socket options.
  15. (#) Security.
  16. (#) Example client usage.
  17. (#) Example server usage.
  18. (#) AF_RXRPC kernel interface.
  19. (#) Configurable parameters.
  20. Overview
  21. ========
  22. RxRPC is a two-layer protocol. There is a session layer which provides
  23. reliable virtual connections using UDP over IPv4 (or IPv6) as the transport
  24. layer, but implements a real network protocol; and there's the presentation
  25. layer which renders structured data to binary blobs and back again using XDR
  26. (as does SunRPC)::
  27. +-------------+
  28. | Application |
  29. +-------------+
  30. | XDR | Presentation
  31. +-------------+
  32. | RxRPC | Session
  33. +-------------+
  34. | UDP | Transport
  35. +-------------+
  36. AF_RXRPC provides:
  37. (1) Part of an RxRPC facility for both kernel and userspace applications by
  38. making the session part of it a Linux network protocol (AF_RXRPC).
  39. (2) A two-phase protocol. The client transmits a blob (the request) and then
  40. receives a blob (the reply), and the server receives the request and then
  41. transmits the reply.
  42. (3) Retention of the reusable bits of the transport system set up for one call
  43. to speed up subsequent calls.
  44. (4) A secure protocol, using the Linux kernel's key retention facility to
  45. manage security on the client end. The server end must of necessity be
  46. more active in security negotiations.
  47. AF_RXRPC does not provide XDR marshalling/presentation facilities. That is
  48. left to the application. AF_RXRPC only deals in blobs. Even the operation ID
  49. is just the first four bytes of the request blob, and as such is beyond the
  50. kernel's interest.
  51. Sockets of AF_RXRPC family are:
  52. (1) created as type SOCK_DGRAM;
  53. (2) provided with a protocol of the type of underlying transport they're going
  54. to use - currently only PF_INET is supported.
  55. The Andrew File System (AFS) is an example of an application that uses this and
  56. that has both kernel (filesystem) and userspace (utility) components.
  57. RxRPC Protocol Summary
  58. ======================
  59. An overview of the RxRPC protocol:
  60. (#) RxRPC sits on top of another networking protocol (UDP is the only option
  61. currently), and uses this to provide network transport. UDP ports, for
  62. example, provide transport endpoints.
  63. (#) RxRPC supports multiple virtual "connections" from any given transport
  64. endpoint, thus allowing the endpoints to be shared, even to the same
  65. remote endpoint.
  66. (#) Each connection goes to a particular "service". A connection may not go
  67. to multiple services. A service may be considered the RxRPC equivalent of
  68. a port number. AF_RXRPC permits multiple services to share an endpoint.
  69. (#) Client-originating packets are marked, thus a transport endpoint can be
  70. shared between client and server connections (connections have a
  71. direction).
  72. (#) Up to a billion connections may be supported concurrently between one
  73. local transport endpoint and one service on one remote endpoint. An RxRPC
  74. connection is described by seven numbers::
  75. Local address }
  76. Local port } Transport (UDP) address
  77. Remote address }
  78. Remote port }
  79. Direction
  80. Connection ID
  81. Service ID
  82. (#) Each RxRPC operation is a "call". A connection may make up to four
  83. billion calls, but only up to four calls may be in progress on a
  84. connection at any one time.
  85. (#) Calls are two-phase and asymmetric: the client sends its request data,
  86. which the service receives; then the service transmits the reply data
  87. which the client receives.
  88. (#) The data blobs are of indefinite size, the end of a phase is marked with a
  89. flag in the packet. The number of packets of data making up one blob may
  90. not exceed 4 billion, however, as this would cause the sequence number to
  91. wrap.
  92. (#) The first four bytes of the request data are the service operation ID.
  93. (#) Security is negotiated on a per-connection basis. The connection is
  94. initiated by the first data packet on it arriving. If security is
  95. requested, the server then issues a "challenge" and then the client
  96. replies with a "response". If the response is successful, the security is
  97. set for the lifetime of that connection, and all subsequent calls made
  98. upon it use that same security. In the event that the server lets a
  99. connection lapse before the client, the security will be renegotiated if
  100. the client uses the connection again.
  101. (#) Calls use ACK packets to handle reliability. Data packets are also
  102. explicitly sequenced per call.
  103. (#) There are two types of positive acknowledgment: hard-ACKs and soft-ACKs.
  104. A hard-ACK indicates to the far side that all the data received to a point
  105. has been received and processed; a soft-ACK indicates that the data has
  106. been received but may yet be discarded and re-requested. The sender may
  107. not discard any transmittable packets until they've been hard-ACK'd.
  108. (#) Reception of a reply data packet implicitly hard-ACK's all the data
  109. packets that make up the request.
  110. (#) An call is complete when the request has been sent, the reply has been
  111. received and the final hard-ACK on the last packet of the reply has
  112. reached the server.
  113. (#) An call may be aborted by either end at any time up to its completion.
  114. AF_RXRPC Driver Model
  115. =====================
  116. About the AF_RXRPC driver:
  117. (#) The AF_RXRPC protocol transparently uses internal sockets of the transport
  118. protocol to represent transport endpoints.
  119. (#) AF_RXRPC sockets map onto RxRPC connection bundles. Actual RxRPC
  120. connections are handled transparently. One client socket may be used to
  121. make multiple simultaneous calls to the same service. One server socket
  122. may handle calls from many clients.
  123. (#) Additional parallel client connections will be initiated to support extra
  124. concurrent calls, up to a tunable limit.
  125. (#) Each connection is retained for a certain amount of time [tunable] after
  126. the last call currently using it has completed in case a new call is made
  127. that could reuse it.
  128. (#) Each internal UDP socket is retained [tunable] for a certain amount of
  129. time [tunable] after the last connection using it discarded, in case a new
  130. connection is made that could use it.
  131. (#) A client-side connection is only shared between calls if they have
  132. the same key struct describing their security (and assuming the calls
  133. would otherwise share the connection). Non-secured calls would also be
  134. able to share connections with each other.
  135. (#) A server-side connection is shared if the client says it is.
  136. (#) ACK'ing is handled by the protocol driver automatically, including ping
  137. replying.
  138. (#) SO_KEEPALIVE automatically pings the other side to keep the connection
  139. alive [TODO].
  140. (#) If an ICMP error is received, all calls affected by that error will be
  141. aborted with an appropriate network error passed through recvmsg().
  142. Interaction with the user of the RxRPC socket:
  143. (#) A socket is made into a server socket by binding an address with a
  144. non-zero service ID.
  145. (#) In the client, sending a request is achieved with one or more sendmsgs,
  146. followed by the reply being received with one or more recvmsgs.
  147. (#) The first sendmsg for a request to be sent from a client contains a tag to
  148. be used in all other sendmsgs or recvmsgs associated with that call. The
  149. tag is carried in the control data.
  150. (#) connect() is used to supply a default destination address for a client
  151. socket. This may be overridden by supplying an alternate address to the
  152. first sendmsg() of a call (struct msghdr::msg_name).
  153. (#) If connect() is called on an unbound client, a random local port will
  154. bound before the operation takes place.
  155. (#) A server socket may also be used to make client calls. To do this, the
  156. first sendmsg() of the call must specify the target address. The server's
  157. transport endpoint is used to send the packets.
  158. (#) Once the application has received the last message associated with a call,
  159. the tag is guaranteed not to be seen again, and so it can be used to pin
  160. client resources. A new call can then be initiated with the same tag
  161. without fear of interference.
  162. (#) In the server, a request is received with one or more recvmsgs, then the
  163. the reply is transmitted with one or more sendmsgs, and then the final ACK
  164. is received with a last recvmsg.
  165. (#) When sending data for a call, sendmsg is given MSG_MORE if there's more
  166. data to come on that call.
  167. (#) When receiving data for a call, recvmsg flags MSG_MORE if there's more
  168. data to come for that call.
  169. (#) When receiving data or messages for a call, MSG_EOR is flagged by recvmsg
  170. to indicate the terminal message for that call.
  171. (#) A call may be aborted by adding an abort control message to the control
  172. data. Issuing an abort terminates the kernel's use of that call's tag.
  173. Any messages waiting in the receive queue for that call will be discarded.
  174. (#) Aborts, busy notifications and challenge packets are delivered by recvmsg,
  175. and control data messages will be set to indicate the context. Receiving
  176. an abort or a busy message terminates the kernel's use of that call's tag.
  177. (#) The control data part of the msghdr struct is used for a number of things:
  178. (#) The tag of the intended or affected call.
  179. (#) Sending or receiving errors, aborts and busy notifications.
  180. (#) Notifications of incoming calls.
  181. (#) Sending debug requests and receiving debug replies [TODO].
  182. (#) When the kernel has received and set up an incoming call, it sends a
  183. message to server application to let it know there's a new call awaiting
  184. its acceptance [recvmsg reports a special control message]. The server
  185. application then uses sendmsg to assign a tag to the new call. Once that
  186. is done, the first part of the request data will be delivered by recvmsg.
  187. (#) The server application has to provide the server socket with a keyring of
  188. secret keys corresponding to the security types it permits. When a secure
  189. connection is being set up, the kernel looks up the appropriate secret key
  190. in the keyring and then sends a challenge packet to the client and
  191. receives a response packet. The kernel then checks the authorisation of
  192. the packet and either aborts the connection or sets up the security.
  193. (#) The name of the key a client will use to secure its communications is
  194. nominated by a socket option.
  195. Notes on sendmsg:
  196. (#) MSG_WAITALL can be set to tell sendmsg to ignore signals if the peer is
  197. making progress at accepting packets within a reasonable time such that we
  198. manage to queue up all the data for transmission. This requires the
  199. client to accept at least one packet per 2*RTT time period.
  200. If this isn't set, sendmsg() will return immediately, either returning
  201. EINTR/ERESTARTSYS if nothing was consumed or returning the amount of data
  202. consumed.
  203. Notes on recvmsg:
  204. (#) If there's a sequence of data messages belonging to a particular call on
  205. the receive queue, then recvmsg will keep working through them until:
  206. (a) it meets the end of that call's received data,
  207. (b) it meets a non-data message,
  208. (c) it meets a message belonging to a different call, or
  209. (d) it fills the user buffer.
  210. If recvmsg is called in blocking mode, it will keep sleeping, awaiting the
  211. reception of further data, until one of the above four conditions is met.
  212. (2) MSG_PEEK operates similarly, but will return immediately if it has put any
  213. data in the buffer rather than sleeping until it can fill the buffer.
  214. (3) If a data message is only partially consumed in filling a user buffer,
  215. then the remainder of that message will be left on the front of the queue
  216. for the next taker. MSG_TRUNC will never be flagged.
  217. (4) If there is more data to be had on a call (it hasn't copied the last byte
  218. of the last data message in that phase yet), then MSG_MORE will be
  219. flagged.
  220. Control Messages
  221. ================
  222. AF_RXRPC makes use of control messages in sendmsg() and recvmsg() to multiplex
  223. calls, to invoke certain actions and to report certain conditions. These are:
  224. ======================= === =========== ===============================
  225. MESSAGE ID SRT DATA MEANING
  226. ======================= === =========== ===============================
  227. RXRPC_USER_CALL_ID sr- User ID App's call specifier
  228. RXRPC_ABORT srt Abort code Abort code to issue/received
  229. RXRPC_ACK -rt n/a Final ACK received
  230. RXRPC_NET_ERROR -rt error num Network error on call
  231. RXRPC_BUSY -rt n/a Call rejected (server busy)
  232. RXRPC_LOCAL_ERROR -rt error num Local error encountered
  233. RXRPC_NEW_CALL -r- n/a New call received
  234. RXRPC_ACCEPT s-- n/a Accept new call
  235. RXRPC_EXCLUSIVE_CALL s-- n/a Make an exclusive client call
  236. RXRPC_UPGRADE_SERVICE s-- n/a Client call can be upgraded
  237. RXRPC_TX_LENGTH s-- data len Total length of Tx data
  238. ======================= === =========== ===============================
  239. (SRT = usable in Sendmsg / delivered by Recvmsg / Terminal message)
  240. (#) RXRPC_USER_CALL_ID
  241. This is used to indicate the application's call ID. It's an unsigned long
  242. that the app specifies in the client by attaching it to the first data
  243. message or in the server by passing it in association with an RXRPC_ACCEPT
  244. message. recvmsg() passes it in conjunction with all messages except
  245. those of the RXRPC_NEW_CALL message.
  246. (#) RXRPC_ABORT
  247. This is can be used by an application to abort a call by passing it to
  248. sendmsg, or it can be delivered by recvmsg to indicate a remote abort was
  249. received. Either way, it must be associated with an RXRPC_USER_CALL_ID to
  250. specify the call affected. If an abort is being sent, then error EBADSLT
  251. will be returned if there is no call with that user ID.
  252. (#) RXRPC_ACK
  253. This is delivered to a server application to indicate that the final ACK
  254. of a call was received from the client. It will be associated with an
  255. RXRPC_USER_CALL_ID to indicate the call that's now complete.
  256. (#) RXRPC_NET_ERROR
  257. This is delivered to an application to indicate that an ICMP error message
  258. was encountered in the process of trying to talk to the peer. An
  259. errno-class integer value will be included in the control message data
  260. indicating the problem, and an RXRPC_USER_CALL_ID will indicate the call
  261. affected.
  262. (#) RXRPC_BUSY
  263. This is delivered to a client application to indicate that a call was
  264. rejected by the server due to the server being busy. It will be
  265. associated with an RXRPC_USER_CALL_ID to indicate the rejected call.
  266. (#) RXRPC_LOCAL_ERROR
  267. This is delivered to an application to indicate that a local error was
  268. encountered and that a call has been aborted because of it. An
  269. errno-class integer value will be included in the control message data
  270. indicating the problem, and an RXRPC_USER_CALL_ID will indicate the call
  271. affected.
  272. (#) RXRPC_NEW_CALL
  273. This is delivered to indicate to a server application that a new call has
  274. arrived and is awaiting acceptance. No user ID is associated with this,
  275. as a user ID must subsequently be assigned by doing an RXRPC_ACCEPT.
  276. (#) RXRPC_ACCEPT
  277. This is used by a server application to attempt to accept a call and
  278. assign it a user ID. It should be associated with an RXRPC_USER_CALL_ID
  279. to indicate the user ID to be assigned. If there is no call to be
  280. accepted (it may have timed out, been aborted, etc.), then sendmsg will
  281. return error ENODATA. If the user ID is already in use by another call,
  282. then error EBADSLT will be returned.
  283. (#) RXRPC_EXCLUSIVE_CALL
  284. This is used to indicate that a client call should be made on a one-off
  285. connection. The connection is discarded once the call has terminated.
  286. (#) RXRPC_UPGRADE_SERVICE
  287. This is used to make a client call to probe if the specified service ID
  288. may be upgraded by the server. The caller must check msg_name returned to
  289. recvmsg() for the service ID actually in use. The operation probed must
  290. be one that takes the same arguments in both services.
  291. Once this has been used to establish the upgrade capability (or lack
  292. thereof) of the server, the service ID returned should be used for all
  293. future communication to that server and RXRPC_UPGRADE_SERVICE should no
  294. longer be set.
  295. (#) RXRPC_TX_LENGTH
  296. This is used to inform the kernel of the total amount of data that is
  297. going to be transmitted by a call (whether in a client request or a
  298. service response). If given, it allows the kernel to encrypt from the
  299. userspace buffer directly to the packet buffers, rather than copying into
  300. the buffer and then encrypting in place. This may only be given with the
  301. first sendmsg() providing data for a call. EMSGSIZE will be generated if
  302. the amount of data actually given is different.
  303. This takes a parameter of __s64 type that indicates how much will be
  304. transmitted. This may not be less than zero.
  305. The symbol RXRPC__SUPPORTED is defined as one more than the highest control
  306. message type supported. At run time this can be queried by means of the
  307. RXRPC_SUPPORTED_CMSG socket option (see below).
  308. ==============
  309. SOCKET OPTIONS
  310. ==============
  311. AF_RXRPC sockets support a few socket options at the SOL_RXRPC level:
  312. (#) RXRPC_SECURITY_KEY
  313. This is used to specify the description of the key to be used. The key is
  314. extracted from the calling process's keyrings with request_key() and
  315. should be of "rxrpc" type.
  316. The optval pointer points to the description string, and optlen indicates
  317. how long the string is, without the NUL terminator.
  318. (#) RXRPC_SECURITY_KEYRING
  319. Similar to above but specifies a keyring of server secret keys to use (key
  320. type "keyring"). See the "Security" section.
  321. (#) RXRPC_EXCLUSIVE_CONNECTION
  322. This is used to request that new connections should be used for each call
  323. made subsequently on this socket. optval should be NULL and optlen 0.
  324. (#) RXRPC_MIN_SECURITY_LEVEL
  325. This is used to specify the minimum security level required for calls on
  326. this socket. optval must point to an int containing one of the following
  327. values:
  328. (a) RXRPC_SECURITY_PLAIN
  329. Encrypted checksum only.
  330. (b) RXRPC_SECURITY_AUTH
  331. Encrypted checksum plus packet padded and first eight bytes of packet
  332. encrypted - which includes the actual packet length.
  333. (c) RXRPC_SECURITY_ENCRYPT
  334. Encrypted checksum plus entire packet padded and encrypted, including
  335. actual packet length.
  336. (#) RXRPC_UPGRADEABLE_SERVICE
  337. This is used to indicate that a service socket with two bindings may
  338. upgrade one bound service to the other if requested by the client. optval
  339. must point to an array of two unsigned short ints. The first is the
  340. service ID to upgrade from and the second the service ID to upgrade to.
  341. (#) RXRPC_SUPPORTED_CMSG
  342. This is a read-only option that writes an int into the buffer indicating
  343. the highest control message type supported.
  344. ========
  345. SECURITY
  346. ========
  347. Currently, only the kerberos 4 equivalent protocol has been implemented
  348. (security index 2 - rxkad). This requires the rxkad module to be loaded and,
  349. on the client, tickets of the appropriate type to be obtained from the AFS
  350. kaserver or the kerberos server and installed as "rxrpc" type keys. This is
  351. normally done using the klog program. An example simple klog program can be
  352. found at:
  353. http://people.redhat.com/~dhowells/rxrpc/klog.c
  354. The payload provided to add_key() on the client should be of the following
  355. form::
  356. struct rxrpc_key_sec2_v1 {
  357. uint16_t security_index; /* 2 */
  358. uint16_t ticket_length; /* length of ticket[] */
  359. uint32_t expiry; /* time at which expires */
  360. uint8_t kvno; /* key version number */
  361. uint8_t __pad[3];
  362. uint8_t session_key[8]; /* DES session key */
  363. uint8_t ticket[0]; /* the encrypted ticket */
  364. };
  365. Where the ticket blob is just appended to the above structure.
  366. For the server, keys of type "rxrpc_s" must be made available to the server.
  367. They have a description of "<serviceID>:<securityIndex>" (eg: "52:2" for an
  368. rxkad key for the AFS VL service). When such a key is created, it should be
  369. given the server's secret key as the instantiation data (see the example
  370. below).
  371. add_key("rxrpc_s", "52:2", secret_key, 8, keyring);
  372. A keyring is passed to the server socket by naming it in a sockopt. The server
  373. socket then looks the server secret keys up in this keyring when secure
  374. incoming connections are made. This can be seen in an example program that can
  375. be found at:
  376. http://people.redhat.com/~dhowells/rxrpc/listen.c
  377. ====================
  378. EXAMPLE CLIENT USAGE
  379. ====================
  380. A client would issue an operation by:
  381. (1) An RxRPC socket is set up by::
  382. client = socket(AF_RXRPC, SOCK_DGRAM, PF_INET);
  383. Where the third parameter indicates the protocol family of the transport
  384. socket used - usually IPv4 but it can also be IPv6 [TODO].
  385. (2) A local address can optionally be bound::
  386. struct sockaddr_rxrpc srx = {
  387. .srx_family = AF_RXRPC,
  388. .srx_service = 0, /* we're a client */
  389. .transport_type = SOCK_DGRAM, /* type of transport socket */
  390. .transport.sin_family = AF_INET,
  391. .transport.sin_port = htons(7000), /* AFS callback */
  392. .transport.sin_address = 0, /* all local interfaces */
  393. };
  394. bind(client, &srx, sizeof(srx));
  395. This specifies the local UDP port to be used. If not given, a random
  396. non-privileged port will be used. A UDP port may be shared between
  397. several unrelated RxRPC sockets. Security is handled on a basis of
  398. per-RxRPC virtual connection.
  399. (3) The security is set::
  400. const char *key = "AFS:cambridge.redhat.com";
  401. setsockopt(client, SOL_RXRPC, RXRPC_SECURITY_KEY, key, strlen(key));
  402. This issues a request_key() to get the key representing the security
  403. context. The minimum security level can be set::
  404. unsigned int sec = RXRPC_SECURITY_ENCRYPT;
  405. setsockopt(client, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
  406. &sec, sizeof(sec));
  407. (4) The server to be contacted can then be specified (alternatively this can
  408. be done through sendmsg)::
  409. struct sockaddr_rxrpc srx = {
  410. .srx_family = AF_RXRPC,
  411. .srx_service = VL_SERVICE_ID,
  412. .transport_type = SOCK_DGRAM, /* type of transport socket */
  413. .transport.sin_family = AF_INET,
  414. .transport.sin_port = htons(7005), /* AFS volume manager */
  415. .transport.sin_address = ...,
  416. };
  417. connect(client, &srx, sizeof(srx));
  418. (5) The request data should then be posted to the server socket using a series
  419. of sendmsg() calls, each with the following control message attached:
  420. ================== ===================================
  421. RXRPC_USER_CALL_ID specifies the user ID for this call
  422. ================== ===================================
  423. MSG_MORE should be set in msghdr::msg_flags on all but the last part of
  424. the request. Multiple requests may be made simultaneously.
  425. An RXRPC_TX_LENGTH control message can also be specified on the first
  426. sendmsg() call.
  427. If a call is intended to go to a destination other than the default
  428. specified through connect(), then msghdr::msg_name should be set on the
  429. first request message of that call.
  430. (6) The reply data will then be posted to the server socket for recvmsg() to
  431. pick up. MSG_MORE will be flagged by recvmsg() if there's more reply data
  432. for a particular call to be read. MSG_EOR will be set on the terminal
  433. read for a call.
  434. All data will be delivered with the following control message attached:
  435. RXRPC_USER_CALL_ID - specifies the user ID for this call
  436. If an abort or error occurred, this will be returned in the control data
  437. buffer instead, and MSG_EOR will be flagged to indicate the end of that
  438. call.
  439. A client may ask for a service ID it knows and ask that this be upgraded to a
  440. better service if one is available by supplying RXRPC_UPGRADE_SERVICE on the
  441. first sendmsg() of a call. The client should then check srx_service in the
  442. msg_name filled in by recvmsg() when collecting the result. srx_service will
  443. hold the same value as given to sendmsg() if the upgrade request was ignored by
  444. the service - otherwise it will be altered to indicate the service ID the
  445. server upgraded to. Note that the upgraded service ID is chosen by the server.
  446. The caller has to wait until it sees the service ID in the reply before sending
  447. any more calls (further calls to the same destination will be blocked until the
  448. probe is concluded).
  449. Example Server Usage
  450. ====================
  451. A server would be set up to accept operations in the following manner:
  452. (1) An RxRPC socket is created by::
  453. server = socket(AF_RXRPC, SOCK_DGRAM, PF_INET);
  454. Where the third parameter indicates the address type of the transport
  455. socket used - usually IPv4.
  456. (2) Security is set up if desired by giving the socket a keyring with server
  457. secret keys in it::
  458. keyring = add_key("keyring", "AFSkeys", NULL, 0,
  459. KEY_SPEC_PROCESS_KEYRING);
  460. const char secret_key[8] = {
  461. 0xa7, 0x83, 0x8a, 0xcb, 0xc7, 0x83, 0xec, 0x94 };
  462. add_key("rxrpc_s", "52:2", secret_key, 8, keyring);
  463. setsockopt(server, SOL_RXRPC, RXRPC_SECURITY_KEYRING, "AFSkeys", 7);
  464. The keyring can be manipulated after it has been given to the socket. This
  465. permits the server to add more keys, replace keys, etc. while it is live.
  466. (3) A local address must then be bound::
  467. struct sockaddr_rxrpc srx = {
  468. .srx_family = AF_RXRPC,
  469. .srx_service = VL_SERVICE_ID, /* RxRPC service ID */
  470. .transport_type = SOCK_DGRAM, /* type of transport socket */
  471. .transport.sin_family = AF_INET,
  472. .transport.sin_port = htons(7000), /* AFS callback */
  473. .transport.sin_address = 0, /* all local interfaces */
  474. };
  475. bind(server, &srx, sizeof(srx));
  476. More than one service ID may be bound to a socket, provided the transport
  477. parameters are the same. The limit is currently two. To do this, bind()
  478. should be called twice.
  479. (4) If service upgrading is required, first two service IDs must have been
  480. bound and then the following option must be set::
  481. unsigned short service_ids[2] = { from_ID, to_ID };
  482. setsockopt(server, SOL_RXRPC, RXRPC_UPGRADEABLE_SERVICE,
  483. service_ids, sizeof(service_ids));
  484. This will automatically upgrade connections on service from_ID to service
  485. to_ID if they request it. This will be reflected in msg_name obtained
  486. through recvmsg() when the request data is delivered to userspace.
  487. (5) The server is then set to listen out for incoming calls::
  488. listen(server, 100);
  489. (6) The kernel notifies the server of pending incoming connections by sending
  490. it a message for each. This is received with recvmsg() on the server
  491. socket. It has no data, and has a single dataless control message
  492. attached::
  493. RXRPC_NEW_CALL
  494. The address that can be passed back by recvmsg() at this point should be
  495. ignored since the call for which the message was posted may have gone by
  496. the time it is accepted - in which case the first call still on the queue
  497. will be accepted.
  498. (7) The server then accepts the new call by issuing a sendmsg() with two
  499. pieces of control data and no actual data:
  500. ================== ==============================
  501. RXRPC_ACCEPT indicate connection acceptance
  502. RXRPC_USER_CALL_ID specify user ID for this call
  503. ================== ==============================
  504. (8) The first request data packet will then be posted to the server socket for
  505. recvmsg() to pick up. At that point, the RxRPC address for the call can
  506. be read from the address fields in the msghdr struct.
  507. Subsequent request data will be posted to the server socket for recvmsg()
  508. to collect as it arrives. All but the last piece of the request data will
  509. be delivered with MSG_MORE flagged.
  510. All data will be delivered with the following control message attached:
  511. ================== ===================================
  512. RXRPC_USER_CALL_ID specifies the user ID for this call
  513. ================== ===================================
  514. (9) The reply data should then be posted to the server socket using a series
  515. of sendmsg() calls, each with the following control messages attached:
  516. ================== ===================================
  517. RXRPC_USER_CALL_ID specifies the user ID for this call
  518. ================== ===================================
  519. MSG_MORE should be set in msghdr::msg_flags on all but the last message
  520. for a particular call.
  521. (10) The final ACK from the client will be posted for retrieval by recvmsg()
  522. when it is received. It will take the form of a dataless message with two
  523. control messages attached:
  524. ================== ===================================
  525. RXRPC_USER_CALL_ID specifies the user ID for this call
  526. RXRPC_ACK indicates final ACK (no data)
  527. ================== ===================================
  528. MSG_EOR will be flagged to indicate that this is the final message for
  529. this call.
  530. (11) Up to the point the final packet of reply data is sent, the call can be
  531. aborted by calling sendmsg() with a dataless message with the following
  532. control messages attached:
  533. ================== ===================================
  534. RXRPC_USER_CALL_ID specifies the user ID for this call
  535. RXRPC_ABORT indicates abort code (4 byte data)
  536. ================== ===================================
  537. Any packets waiting in the socket's receive queue will be discarded if
  538. this is issued.
  539. Note that all the communications for a particular service take place through
  540. the one server socket, using control messages on sendmsg() and recvmsg() to
  541. determine the call affected.
  542. AF_RXRPC Kernel Interface
  543. =========================
  544. The AF_RXRPC module also provides an interface for use by in-kernel utilities
  545. such as the AFS filesystem. This permits such a utility to:
  546. (1) Use different keys directly on individual client calls on one socket
  547. rather than having to open a whole slew of sockets, one for each key it
  548. might want to use.
  549. (2) Avoid having RxRPC call request_key() at the point of issue of a call or
  550. opening of a socket. Instead the utility is responsible for requesting a
  551. key at the appropriate point. AFS, for instance, would do this during VFS
  552. operations such as open() or unlink(). The key is then handed through
  553. when the call is initiated.
  554. (3) Request the use of something other than GFP_KERNEL to allocate memory.
  555. (4) Avoid the overhead of using the recvmsg() call. RxRPC messages can be
  556. intercepted before they get put into the socket Rx queue and the socket
  557. buffers manipulated directly.
  558. To use the RxRPC facility, a kernel utility must still open an AF_RXRPC socket,
  559. bind an address as appropriate and listen if it's to be a server socket, but
  560. then it passes this to the kernel interface functions.
  561. The kernel interface functions are as follows:
  562. (#) Begin a new client call::
  563. struct rxrpc_call *
  564. rxrpc_kernel_begin_call(struct socket *sock,
  565. struct sockaddr_rxrpc *srx,
  566. struct key *key,
  567. unsigned long user_call_ID,
  568. s64 tx_total_len,
  569. gfp_t gfp,
  570. rxrpc_notify_rx_t notify_rx,
  571. bool upgrade,
  572. bool intr,
  573. unsigned int debug_id);
  574. This allocates the infrastructure to make a new RxRPC call and assigns
  575. call and connection numbers. The call will be made on the UDP port that
  576. the socket is bound to. The call will go to the destination address of a
  577. connected client socket unless an alternative is supplied (srx is
  578. non-NULL).
  579. If a key is supplied then this will be used to secure the call instead of
  580. the key bound to the socket with the RXRPC_SECURITY_KEY sockopt. Calls
  581. secured in this way will still share connections if at all possible.
  582. The user_call_ID is equivalent to that supplied to sendmsg() in the
  583. control data buffer. It is entirely feasible to use this to point to a
  584. kernel data structure.
  585. tx_total_len is the amount of data the caller is intending to transmit
  586. with this call (or -1 if unknown at this point). Setting the data size
  587. allows the kernel to encrypt directly to the packet buffers, thereby
  588. saving a copy. The value may not be less than -1.
  589. notify_rx is a pointer to a function to be called when events such as
  590. incoming data packets or remote aborts happen.
  591. upgrade should be set to true if a client operation should request that
  592. the server upgrade the service to a better one. The resultant service ID
  593. is returned by rxrpc_kernel_recv_data().
  594. intr should be set to true if the call should be interruptible. If this
  595. is not set, this function may not return until a channel has been
  596. allocated; if it is set, the function may return -ERESTARTSYS.
  597. debug_id is the call debugging ID to be used for tracing. This can be
  598. obtained by atomically incrementing rxrpc_debug_id.
  599. If this function is successful, an opaque reference to the RxRPC call is
  600. returned. The caller now holds a reference on this and it must be
  601. properly ended.
  602. (#) End a client call::
  603. void rxrpc_kernel_end_call(struct socket *sock,
  604. struct rxrpc_call *call);
  605. This is used to end a previously begun call. The user_call_ID is expunged
  606. from AF_RXRPC's knowledge and will not be seen again in association with
  607. the specified call.
  608. (#) Send data through a call::
  609. typedef void (*rxrpc_notify_end_tx_t)(struct sock *sk,
  610. unsigned long user_call_ID,
  611. struct sk_buff *skb);
  612. int rxrpc_kernel_send_data(struct socket *sock,
  613. struct rxrpc_call *call,
  614. struct msghdr *msg,
  615. size_t len,
  616. rxrpc_notify_end_tx_t notify_end_rx);
  617. This is used to supply either the request part of a client call or the
  618. reply part of a server call. msg.msg_iovlen and msg.msg_iov specify the
  619. data buffers to be used. msg_iov may not be NULL and must point
  620. exclusively to in-kernel virtual addresses. msg.msg_flags may be given
  621. MSG_MORE if there will be subsequent data sends for this call.
  622. The msg must not specify a destination address, control data or any flags
  623. other than MSG_MORE. len is the total amount of data to transmit.
  624. notify_end_rx can be NULL or it can be used to specify a function to be
  625. called when the call changes state to end the Tx phase. This function is
  626. called with the call-state spinlock held to prevent any reply or final ACK
  627. from being delivered first.
  628. (#) Receive data from a call::
  629. int rxrpc_kernel_recv_data(struct socket *sock,
  630. struct rxrpc_call *call,
  631. void *buf,
  632. size_t size,
  633. size_t *_offset,
  634. bool want_more,
  635. u32 *_abort,
  636. u16 *_service)
  637. This is used to receive data from either the reply part of a client call
  638. or the request part of a service call. buf and size specify how much
  639. data is desired and where to store it. *_offset is added on to buf and
  640. subtracted from size internally; the amount copied into the buffer is
  641. added to *_offset before returning.
  642. want_more should be true if further data will be required after this is
  643. satisfied and false if this is the last item of the receive phase.
  644. There are three normal returns: 0 if the buffer was filled and want_more
  645. was true; 1 if the buffer was filled, the last DATA packet has been
  646. emptied and want_more was false; and -EAGAIN if the function needs to be
  647. called again.
  648. If the last DATA packet is processed but the buffer contains less than
  649. the amount requested, EBADMSG is returned. If want_more wasn't set, but
  650. more data was available, EMSGSIZE is returned.
  651. If a remote ABORT is detected, the abort code received will be stored in
  652. ``*_abort`` and ECONNABORTED will be returned.
  653. The service ID that the call ended up with is returned into *_service.
  654. This can be used to see if a call got a service upgrade.
  655. (#) Abort a call??
  656. ::
  657. void rxrpc_kernel_abort_call(struct socket *sock,
  658. struct rxrpc_call *call,
  659. u32 abort_code);
  660. This is used to abort a call if it's still in an abortable state. The
  661. abort code specified will be placed in the ABORT message sent.
  662. (#) Intercept received RxRPC messages::
  663. typedef void (*rxrpc_interceptor_t)(struct sock *sk,
  664. unsigned long user_call_ID,
  665. struct sk_buff *skb);
  666. void
  667. rxrpc_kernel_intercept_rx_messages(struct socket *sock,
  668. rxrpc_interceptor_t interceptor);
  669. This installs an interceptor function on the specified AF_RXRPC socket.
  670. All messages that would otherwise wind up in the socket's Rx queue are
  671. then diverted to this function. Note that care must be taken to process
  672. the messages in the right order to maintain DATA message sequentiality.
  673. The interceptor function itself is provided with the address of the socket
  674. and handling the incoming message, the ID assigned by the kernel utility
  675. to the call and the socket buffer containing the message.
  676. The skb->mark field indicates the type of message:
  677. =============================== =======================================
  678. Mark Meaning
  679. =============================== =======================================
  680. RXRPC_SKB_MARK_DATA Data message
  681. RXRPC_SKB_MARK_FINAL_ACK Final ACK received for an incoming call
  682. RXRPC_SKB_MARK_BUSY Client call rejected as server busy
  683. RXRPC_SKB_MARK_REMOTE_ABORT Call aborted by peer
  684. RXRPC_SKB_MARK_NET_ERROR Network error detected
  685. RXRPC_SKB_MARK_LOCAL_ERROR Local error encountered
  686. RXRPC_SKB_MARK_NEW_CALL New incoming call awaiting acceptance
  687. =============================== =======================================
  688. The remote abort message can be probed with rxrpc_kernel_get_abort_code().
  689. The two error messages can be probed with rxrpc_kernel_get_error_number().
  690. A new call can be accepted with rxrpc_kernel_accept_call().
  691. Data messages can have their contents extracted with the usual bunch of
  692. socket buffer manipulation functions. A data message can be determined to
  693. be the last one in a sequence with rxrpc_kernel_is_data_last(). When a
  694. data message has been used up, rxrpc_kernel_data_consumed() should be
  695. called on it.
  696. Messages should be handled to rxrpc_kernel_free_skb() to dispose of. It
  697. is possible to get extra refs on all types of message for later freeing,
  698. but this may pin the state of a call until the message is finally freed.
  699. (#) Accept an incoming call::
  700. struct rxrpc_call *
  701. rxrpc_kernel_accept_call(struct socket *sock,
  702. unsigned long user_call_ID);
  703. This is used to accept an incoming call and to assign it a call ID. This
  704. function is similar to rxrpc_kernel_begin_call() and calls accepted must
  705. be ended in the same way.
  706. If this function is successful, an opaque reference to the RxRPC call is
  707. returned. The caller now holds a reference on this and it must be
  708. properly ended.
  709. (#) Reject an incoming call::
  710. int rxrpc_kernel_reject_call(struct socket *sock);
  711. This is used to reject the first incoming call on the socket's queue with
  712. a BUSY message. -ENODATA is returned if there were no incoming calls.
  713. Other errors may be returned if the call had been aborted (-ECONNABORTED)
  714. or had timed out (-ETIME).
  715. (#) Allocate a null key for doing anonymous security::
  716. struct key *rxrpc_get_null_key(const char *keyname);
  717. This is used to allocate a null RxRPC key that can be used to indicate
  718. anonymous security for a particular domain.
  719. (#) Get the peer address of a call::
  720. void rxrpc_kernel_get_peer(struct socket *sock, struct rxrpc_call *call,
  721. struct sockaddr_rxrpc *_srx);
  722. This is used to find the remote peer address of a call.
  723. (#) Set the total transmit data size on a call::
  724. void rxrpc_kernel_set_tx_length(struct socket *sock,
  725. struct rxrpc_call *call,
  726. s64 tx_total_len);
  727. This sets the amount of data that the caller is intending to transmit on a
  728. call. It's intended to be used for setting the reply size as the request
  729. size should be set when the call is begun. tx_total_len may not be less
  730. than zero.
  731. (#) Get call RTT::
  732. u64 rxrpc_kernel_get_rtt(struct socket *sock, struct rxrpc_call *call);
  733. Get the RTT time to the peer in use by a call. The value returned is in
  734. nanoseconds.
  735. (#) Check call still alive::
  736. bool rxrpc_kernel_check_life(struct socket *sock,
  737. struct rxrpc_call *call,
  738. u32 *_life);
  739. void rxrpc_kernel_probe_life(struct socket *sock,
  740. struct rxrpc_call *call);
  741. The first function passes back in ``*_life`` a number that is updated when
  742. ACKs are received from the peer (notably including PING RESPONSE ACKs
  743. which we can elicit by sending PING ACKs to see if the call still exists
  744. on the server). The caller should compare the numbers of two calls to see
  745. if the call is still alive after waiting for a suitable interval. It also
  746. returns true as long as the call hasn't yet reached the completed state.
  747. This allows the caller to work out if the server is still contactable and
  748. if the call is still alive on the server while waiting for the server to
  749. process a client operation.
  750. The second function causes a ping ACK to be transmitted to try to provoke
  751. the peer into responding, which would then cause the value returned by the
  752. first function to change. Note that this must be called in TASK_RUNNING
  753. state.
  754. (#) Get reply timestamp::
  755. bool rxrpc_kernel_get_reply_time(struct socket *sock,
  756. struct rxrpc_call *call,
  757. ktime_t *_ts)
  758. This allows the timestamp on the first DATA packet of the reply of a
  759. client call to be queried, provided that it is still in the Rx ring. If
  760. successful, the timestamp will be stored into ``*_ts`` and true will be
  761. returned; false will be returned otherwise.
  762. (#) Get remote client epoch::
  763. u32 rxrpc_kernel_get_epoch(struct socket *sock,
  764. struct rxrpc_call *call)
  765. This allows the epoch that's contained in packets of an incoming client
  766. call to be queried. This value is returned. The function always
  767. successful if the call is still in progress. It shouldn't be called once
  768. the call has expired. Note that calling this on a local client call only
  769. returns the local epoch.
  770. This value can be used to determine if the remote client has been
  771. restarted as it shouldn't change otherwise.
  772. (#) Set the maxmimum lifespan on a call::
  773. void rxrpc_kernel_set_max_life(struct socket *sock,
  774. struct rxrpc_call *call,
  775. unsigned long hard_timeout)
  776. This sets the maximum lifespan on a call to hard_timeout (which is in
  777. jiffies). In the event of the timeout occurring, the call will be
  778. aborted and -ETIME or -ETIMEDOUT will be returned.
  779. (#) Apply the RXRPC_MIN_SECURITY_LEVEL sockopt to a socket from within in the
  780. kernel::
  781. int rxrpc_sock_set_min_security_level(struct sock *sk,
  782. unsigned int val);
  783. This specifies the minimum security level required for calls on this
  784. socket.
  785. Configurable Parameters
  786. =======================
  787. The RxRPC protocol driver has a number of configurable parameters that can be
  788. adjusted through sysctls in /proc/net/rxrpc/:
  789. (#) req_ack_delay
  790. The amount of time in milliseconds after receiving a packet with the
  791. request-ack flag set before we honour the flag and actually send the
  792. requested ack.
  793. Usually the other side won't stop sending packets until the advertised
  794. reception window is full (to a maximum of 255 packets), so delaying the
  795. ACK permits several packets to be ACK'd in one go.
  796. (#) soft_ack_delay
  797. The amount of time in milliseconds after receiving a new packet before we
  798. generate a soft-ACK to tell the sender that it doesn't need to resend.
  799. (#) idle_ack_delay
  800. The amount of time in milliseconds after all the packets currently in the
  801. received queue have been consumed before we generate a hard-ACK to tell
  802. the sender it can free its buffers, assuming no other reason occurs that
  803. we would send an ACK.
  804. (#) resend_timeout
  805. The amount of time in milliseconds after transmitting a packet before we
  806. transmit it again, assuming no ACK is received from the receiver telling
  807. us they got it.
  808. (#) max_call_lifetime
  809. The maximum amount of time in seconds that a call may be in progress
  810. before we preemptively kill it.
  811. (#) dead_call_expiry
  812. The amount of time in seconds before we remove a dead call from the call
  813. list. Dead calls are kept around for a little while for the purpose of
  814. repeating ACK and ABORT packets.
  815. (#) connection_expiry
  816. The amount of time in seconds after a connection was last used before we
  817. remove it from the connection list. While a connection is in existence,
  818. it serves as a placeholder for negotiated security; when it is deleted,
  819. the security must be renegotiated.
  820. (#) transport_expiry
  821. The amount of time in seconds after a transport was last used before we
  822. remove it from the transport list. While a transport is in existence, it
  823. serves to anchor the peer data and keeps the connection ID counter.
  824. (#) rxrpc_rx_window_size
  825. The size of the receive window in packets. This is the maximum number of
  826. unconsumed received packets we're willing to hold in memory for any
  827. particular call.
  828. (#) rxrpc_rx_mtu
  829. The maximum packet MTU size that we're willing to receive in bytes. This
  830. indicates to the peer whether we're willing to accept jumbo packets.
  831. (#) rxrpc_rx_jumbo_max
  832. The maximum number of packets that we're willing to accept in a jumbo
  833. packet. Non-terminal packets in a jumbo packet must contain a four byte
  834. header plus exactly 1412 bytes of data. The terminal packet must contain
  835. a four byte header plus any amount of data. In any event, a jumbo packet
  836. may not exceed rxrpc_rx_mtu in size.