socket.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "port.h"
  38. #include <linux/export.h>
  39. #define SS_LISTENING -1 /* socket is listening */
  40. #define SS_READY -2 /* socket is connectionless */
  41. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  42. static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
  43. static void tipc_data_ready(struct sock *sk);
  44. static void tipc_write_space(struct sock *sk);
  45. static int tipc_release(struct socket *sock);
  46. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
  47. static const struct proto_ops packet_ops;
  48. static const struct proto_ops stream_ops;
  49. static const struct proto_ops msg_ops;
  50. static struct proto tipc_proto;
  51. static struct proto tipc_proto_kern;
  52. /*
  53. * Revised TIPC socket locking policy:
  54. *
  55. * Most socket operations take the standard socket lock when they start
  56. * and hold it until they finish (or until they need to sleep). Acquiring
  57. * this lock grants the owner exclusive access to the fields of the socket
  58. * data structures, with the exception of the backlog queue. A few socket
  59. * operations can be done without taking the socket lock because they only
  60. * read socket information that never changes during the life of the socket.
  61. *
  62. * Socket operations may acquire the lock for the associated TIPC port if they
  63. * need to perform an operation on the port. If any routine needs to acquire
  64. * both the socket lock and the port lock it must take the socket lock first
  65. * to avoid the risk of deadlock.
  66. *
  67. * The dispatcher handling incoming messages cannot grab the socket lock in
  68. * the standard fashion, since invoked it runs at the BH level and cannot block.
  69. * Instead, it checks to see if the socket lock is currently owned by someone,
  70. * and either handles the message itself or adds it to the socket's backlog
  71. * queue; in the latter case the queued message is processed once the process
  72. * owning the socket lock releases it.
  73. *
  74. * NOTE: Releasing the socket lock while an operation is sleeping overcomes
  75. * the problem of a blocked socket operation preventing any other operations
  76. * from occurring. However, applications must be careful if they have
  77. * multiple threads trying to send (or receive) on the same socket, as these
  78. * operations might interfere with each other. For example, doing a connect
  79. * and a receive at the same time might allow the receive to consume the
  80. * ACK message meant for the connect. While additional work could be done
  81. * to try and overcome this, it doesn't seem to be worthwhile at the present.
  82. *
  83. * NOTE: Releasing the socket lock while an operation is sleeping also ensures
  84. * that another operation that must be performed in a non-blocking manner is
  85. * not delayed for very long because the lock has already been taken.
  86. *
  87. * NOTE: This code assumes that certain fields of a port/socket pair are
  88. * constant over its lifetime; such fields can be examined without taking
  89. * the socket lock and/or port lock, and do not need to be re-read even
  90. * after resuming processing after waiting. These fields include:
  91. * - socket type
  92. * - pointer to socket sk structure (aka tipc_sock structure)
  93. * - pointer to port structure
  94. * - port reference
  95. */
  96. #include "socket.h"
  97. /**
  98. * advance_rx_queue - discard first buffer in socket receive queue
  99. *
  100. * Caller must hold socket lock
  101. */
  102. static void advance_rx_queue(struct sock *sk)
  103. {
  104. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  105. }
  106. /**
  107. * reject_rx_queue - reject all buffers in socket receive queue
  108. *
  109. * Caller must hold socket lock
  110. */
  111. static void reject_rx_queue(struct sock *sk)
  112. {
  113. struct sk_buff *buf;
  114. while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
  115. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  116. }
  117. /**
  118. * tipc_sk_create - create a TIPC socket
  119. * @net: network namespace (must be default network)
  120. * @sock: pre-allocated socket structure
  121. * @protocol: protocol indicator (must be 0)
  122. * @kern: caused by kernel or by userspace?
  123. *
  124. * This routine creates additional data structures used by the TIPC socket,
  125. * initializes them, and links them together.
  126. *
  127. * Returns 0 on success, errno otherwise
  128. */
  129. static int tipc_sk_create(struct net *net, struct socket *sock,
  130. int protocol, int kern)
  131. {
  132. const struct proto_ops *ops;
  133. socket_state state;
  134. struct sock *sk;
  135. struct tipc_sock *tsk;
  136. struct tipc_port *port;
  137. u32 ref;
  138. /* Validate arguments */
  139. if (unlikely(protocol != 0))
  140. return -EPROTONOSUPPORT;
  141. switch (sock->type) {
  142. case SOCK_STREAM:
  143. ops = &stream_ops;
  144. state = SS_UNCONNECTED;
  145. break;
  146. case SOCK_SEQPACKET:
  147. ops = &packet_ops;
  148. state = SS_UNCONNECTED;
  149. break;
  150. case SOCK_DGRAM:
  151. case SOCK_RDM:
  152. ops = &msg_ops;
  153. state = SS_READY;
  154. break;
  155. default:
  156. return -EPROTOTYPE;
  157. }
  158. /* Allocate socket's protocol area */
  159. if (!kern)
  160. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  161. else
  162. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
  163. if (sk == NULL)
  164. return -ENOMEM;
  165. tsk = tipc_sk(sk);
  166. port = &tsk->port;
  167. ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
  168. if (!ref) {
  169. pr_warn("Socket registration failed, ref. table exhausted\n");
  170. sk_free(sk);
  171. return -ENOMEM;
  172. }
  173. /* Finish initializing socket data structures */
  174. sock->ops = ops;
  175. sock->state = state;
  176. sock_init_data(sock, sk);
  177. sk->sk_backlog_rcv = backlog_rcv;
  178. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  179. sk->sk_data_ready = tipc_data_ready;
  180. sk->sk_write_space = tipc_write_space;
  181. tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
  182. tipc_port_unlock(port);
  183. if (sock->state == SS_READY) {
  184. tipc_port_set_unreturnable(port, true);
  185. if (sock->type == SOCK_DGRAM)
  186. tipc_port_set_unreliable(port, true);
  187. }
  188. return 0;
  189. }
  190. /**
  191. * tipc_sock_create_local - create TIPC socket from inside TIPC module
  192. * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
  193. *
  194. * We cannot use sock_creat_kern here because it bumps module user count.
  195. * Since socket owner and creator is the same module we must make sure
  196. * that module count remains zero for module local sockets, otherwise
  197. * we cannot do rmmod.
  198. *
  199. * Returns 0 on success, errno otherwise
  200. */
  201. int tipc_sock_create_local(int type, struct socket **res)
  202. {
  203. int rc;
  204. rc = sock_create_lite(AF_TIPC, type, 0, res);
  205. if (rc < 0) {
  206. pr_err("Failed to create kernel socket\n");
  207. return rc;
  208. }
  209. tipc_sk_create(&init_net, *res, 0, 1);
  210. return 0;
  211. }
  212. /**
  213. * tipc_sock_release_local - release socket created by tipc_sock_create_local
  214. * @sock: the socket to be released.
  215. *
  216. * Module reference count is not incremented when such sockets are created,
  217. * so we must keep it from being decremented when they are released.
  218. */
  219. void tipc_sock_release_local(struct socket *sock)
  220. {
  221. tipc_release(sock);
  222. sock->ops = NULL;
  223. sock_release(sock);
  224. }
  225. /**
  226. * tipc_sock_accept_local - accept a connection on a socket created
  227. * with tipc_sock_create_local. Use this function to avoid that
  228. * module reference count is inadvertently incremented.
  229. *
  230. * @sock: the accepting socket
  231. * @newsock: reference to the new socket to be created
  232. * @flags: socket flags
  233. */
  234. int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
  235. int flags)
  236. {
  237. struct sock *sk = sock->sk;
  238. int ret;
  239. ret = sock_create_lite(sk->sk_family, sk->sk_type,
  240. sk->sk_protocol, newsock);
  241. if (ret < 0)
  242. return ret;
  243. ret = tipc_accept(sock, *newsock, flags);
  244. if (ret < 0) {
  245. sock_release(*newsock);
  246. return ret;
  247. }
  248. (*newsock)->ops = sock->ops;
  249. return ret;
  250. }
  251. /**
  252. * tipc_release - destroy a TIPC socket
  253. * @sock: socket to destroy
  254. *
  255. * This routine cleans up any messages that are still queued on the socket.
  256. * For DGRAM and RDM socket types, all queued messages are rejected.
  257. * For SEQPACKET and STREAM socket types, the first message is rejected
  258. * and any others are discarded. (If the first message on a STREAM socket
  259. * is partially-read, it is discarded and the next one is rejected instead.)
  260. *
  261. * NOTE: Rejected messages are not necessarily returned to the sender! They
  262. * are returned or discarded according to the "destination droppable" setting
  263. * specified for the message by the sender.
  264. *
  265. * Returns 0 on success, errno otherwise
  266. */
  267. static int tipc_release(struct socket *sock)
  268. {
  269. struct sock *sk = sock->sk;
  270. struct tipc_sock *tsk;
  271. struct tipc_port *port;
  272. struct sk_buff *buf;
  273. /*
  274. * Exit if socket isn't fully initialized (occurs when a failed accept()
  275. * releases a pre-allocated child socket that was never used)
  276. */
  277. if (sk == NULL)
  278. return 0;
  279. tsk = tipc_sk(sk);
  280. port = &tsk->port;
  281. lock_sock(sk);
  282. /*
  283. * Reject all unreceived messages, except on an active connection
  284. * (which disconnects locally & sends a 'FIN+' to peer)
  285. */
  286. while (sock->state != SS_DISCONNECTING) {
  287. buf = __skb_dequeue(&sk->sk_receive_queue);
  288. if (buf == NULL)
  289. break;
  290. if (TIPC_SKB_CB(buf)->handle != NULL)
  291. kfree_skb(buf);
  292. else {
  293. if ((sock->state == SS_CONNECTING) ||
  294. (sock->state == SS_CONNECTED)) {
  295. sock->state = SS_DISCONNECTING;
  296. tipc_port_disconnect(port->ref);
  297. }
  298. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  299. }
  300. }
  301. /* Destroy TIPC port; also disconnects an active connection and
  302. * sends a 'FIN-' to peer.
  303. */
  304. tipc_port_destroy(port);
  305. /* Discard any remaining (connection-based) messages in receive queue */
  306. __skb_queue_purge(&sk->sk_receive_queue);
  307. /* Reject any messages that accumulated in backlog queue */
  308. sock->state = SS_DISCONNECTING;
  309. release_sock(sk);
  310. sock_put(sk);
  311. sock->sk = NULL;
  312. return 0;
  313. }
  314. /**
  315. * tipc_bind - associate or disassocate TIPC name(s) with a socket
  316. * @sock: socket structure
  317. * @uaddr: socket address describing name(s) and desired operation
  318. * @uaddr_len: size of socket address data structure
  319. *
  320. * Name and name sequence binding is indicated using a positive scope value;
  321. * a negative scope value unbinds the specified name. Specifying no name
  322. * (i.e. a socket address length of 0) unbinds all names from the socket.
  323. *
  324. * Returns 0 on success, errno otherwise
  325. *
  326. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  327. * access any non-constant socket information.
  328. */
  329. static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
  330. int uaddr_len)
  331. {
  332. struct sock *sk = sock->sk;
  333. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  334. struct tipc_sock *tsk = tipc_sk(sk);
  335. int res = -EINVAL;
  336. lock_sock(sk);
  337. if (unlikely(!uaddr_len)) {
  338. res = tipc_withdraw(&tsk->port, 0, NULL);
  339. goto exit;
  340. }
  341. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  342. res = -EINVAL;
  343. goto exit;
  344. }
  345. if (addr->family != AF_TIPC) {
  346. res = -EAFNOSUPPORT;
  347. goto exit;
  348. }
  349. if (addr->addrtype == TIPC_ADDR_NAME)
  350. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  351. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  352. res = -EAFNOSUPPORT;
  353. goto exit;
  354. }
  355. if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
  356. (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
  357. (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
  358. res = -EACCES;
  359. goto exit;
  360. }
  361. res = (addr->scope > 0) ?
  362. tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
  363. tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
  364. exit:
  365. release_sock(sk);
  366. return res;
  367. }
  368. /**
  369. * tipc_getname - get port ID of socket or peer socket
  370. * @sock: socket structure
  371. * @uaddr: area for returned socket address
  372. * @uaddr_len: area for returned length of socket address
  373. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  374. *
  375. * Returns 0 on success, errno otherwise
  376. *
  377. * NOTE: This routine doesn't need to take the socket lock since it only
  378. * accesses socket information that is unchanging (or which changes in
  379. * a completely predictable manner).
  380. */
  381. static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
  382. int *uaddr_len, int peer)
  383. {
  384. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  385. struct tipc_sock *tsk = tipc_sk(sock->sk);
  386. memset(addr, 0, sizeof(*addr));
  387. if (peer) {
  388. if ((sock->state != SS_CONNECTED) &&
  389. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  390. return -ENOTCONN;
  391. addr->addr.id.ref = tipc_port_peerport(&tsk->port);
  392. addr->addr.id.node = tipc_port_peernode(&tsk->port);
  393. } else {
  394. addr->addr.id.ref = tsk->port.ref;
  395. addr->addr.id.node = tipc_own_addr;
  396. }
  397. *uaddr_len = sizeof(*addr);
  398. addr->addrtype = TIPC_ADDR_ID;
  399. addr->family = AF_TIPC;
  400. addr->scope = 0;
  401. addr->addr.name.domain = 0;
  402. return 0;
  403. }
  404. /**
  405. * tipc_poll - read and possibly block on pollmask
  406. * @file: file structure associated with the socket
  407. * @sock: socket for which to calculate the poll bits
  408. * @wait: ???
  409. *
  410. * Returns pollmask value
  411. *
  412. * COMMENTARY:
  413. * It appears that the usual socket locking mechanisms are not useful here
  414. * since the pollmask info is potentially out-of-date the moment this routine
  415. * exits. TCP and other protocols seem to rely on higher level poll routines
  416. * to handle any preventable race conditions, so TIPC will do the same ...
  417. *
  418. * TIPC sets the returned events as follows:
  419. *
  420. * socket state flags set
  421. * ------------ ---------
  422. * unconnected no read flags
  423. * POLLOUT if port is not congested
  424. *
  425. * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
  426. * no write flags
  427. *
  428. * connected POLLIN/POLLRDNORM if data in rx queue
  429. * POLLOUT if port is not congested
  430. *
  431. * disconnecting POLLIN/POLLRDNORM/POLLHUP
  432. * no write flags
  433. *
  434. * listening POLLIN if SYN in rx queue
  435. * no write flags
  436. *
  437. * ready POLLIN/POLLRDNORM if data in rx queue
  438. * [connectionless] POLLOUT (since port cannot be congested)
  439. *
  440. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  441. * imply that the operation will succeed, merely that it should be performed
  442. * and will not block.
  443. */
  444. static unsigned int tipc_poll(struct file *file, struct socket *sock,
  445. poll_table *wait)
  446. {
  447. struct sock *sk = sock->sk;
  448. struct tipc_sock *tsk = tipc_sk(sk);
  449. u32 mask = 0;
  450. sock_poll_wait(file, sk_sleep(sk), wait);
  451. switch ((int)sock->state) {
  452. case SS_UNCONNECTED:
  453. if (!tsk->port.congested)
  454. mask |= POLLOUT;
  455. break;
  456. case SS_READY:
  457. case SS_CONNECTED:
  458. if (!tsk->port.congested)
  459. mask |= POLLOUT;
  460. /* fall thru' */
  461. case SS_CONNECTING:
  462. case SS_LISTENING:
  463. if (!skb_queue_empty(&sk->sk_receive_queue))
  464. mask |= (POLLIN | POLLRDNORM);
  465. break;
  466. case SS_DISCONNECTING:
  467. mask = (POLLIN | POLLRDNORM | POLLHUP);
  468. break;
  469. }
  470. return mask;
  471. }
  472. /**
  473. * dest_name_check - verify user is permitted to send to specified port name
  474. * @dest: destination address
  475. * @m: descriptor for message to be sent
  476. *
  477. * Prevents restricted configuration commands from being issued by
  478. * unauthorized users.
  479. *
  480. * Returns 0 if permission is granted, otherwise errno
  481. */
  482. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  483. {
  484. struct tipc_cfg_msg_hdr hdr;
  485. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  486. return 0;
  487. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  488. return 0;
  489. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  490. return -EACCES;
  491. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  492. return -EMSGSIZE;
  493. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  494. return -EFAULT;
  495. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  496. return -EACCES;
  497. return 0;
  498. }
  499. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  500. {
  501. struct sock *sk = sock->sk;
  502. struct tipc_sock *tsk = tipc_sk(sk);
  503. DEFINE_WAIT(wait);
  504. int done;
  505. do {
  506. int err = sock_error(sk);
  507. if (err)
  508. return err;
  509. if (sock->state == SS_DISCONNECTING)
  510. return -EPIPE;
  511. if (!*timeo_p)
  512. return -EAGAIN;
  513. if (signal_pending(current))
  514. return sock_intr_errno(*timeo_p);
  515. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  516. done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
  517. finish_wait(sk_sleep(sk), &wait);
  518. } while (!done);
  519. return 0;
  520. }
  521. /**
  522. * tipc_sendmsg - send message in connectionless manner
  523. * @iocb: if NULL, indicates that socket lock is already held
  524. * @sock: socket structure
  525. * @m: message to send
  526. * @total_len: length of message
  527. *
  528. * Message must have an destination specified explicitly.
  529. * Used for SOCK_RDM and SOCK_DGRAM messages,
  530. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  531. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  532. *
  533. * Returns the number of bytes sent on success, or errno otherwise
  534. */
  535. static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
  536. struct msghdr *m, size_t total_len)
  537. {
  538. struct sock *sk = sock->sk;
  539. struct tipc_sock *tsk = tipc_sk(sk);
  540. struct tipc_port *port = &tsk->port;
  541. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  542. int needs_conn;
  543. long timeo;
  544. int res = -EINVAL;
  545. if (unlikely(!dest))
  546. return -EDESTADDRREQ;
  547. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  548. (dest->family != AF_TIPC)))
  549. return -EINVAL;
  550. if (total_len > TIPC_MAX_USER_MSG_SIZE)
  551. return -EMSGSIZE;
  552. if (iocb)
  553. lock_sock(sk);
  554. needs_conn = (sock->state != SS_READY);
  555. if (unlikely(needs_conn)) {
  556. if (sock->state == SS_LISTENING) {
  557. res = -EPIPE;
  558. goto exit;
  559. }
  560. if (sock->state != SS_UNCONNECTED) {
  561. res = -EISCONN;
  562. goto exit;
  563. }
  564. if (tsk->port.published) {
  565. res = -EOPNOTSUPP;
  566. goto exit;
  567. }
  568. if (dest->addrtype == TIPC_ADDR_NAME) {
  569. tsk->port.conn_type = dest->addr.name.name.type;
  570. tsk->port.conn_instance = dest->addr.name.name.instance;
  571. }
  572. /* Abort any pending connection attempts (very unlikely) */
  573. reject_rx_queue(sk);
  574. }
  575. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  576. do {
  577. if (dest->addrtype == TIPC_ADDR_NAME) {
  578. res = dest_name_check(dest, m);
  579. if (res)
  580. break;
  581. res = tipc_send2name(port,
  582. &dest->addr.name.name,
  583. dest->addr.name.domain,
  584. m->msg_iov,
  585. total_len);
  586. } else if (dest->addrtype == TIPC_ADDR_ID) {
  587. res = tipc_send2port(port,
  588. &dest->addr.id,
  589. m->msg_iov,
  590. total_len);
  591. } else if (dest->addrtype == TIPC_ADDR_MCAST) {
  592. if (needs_conn) {
  593. res = -EOPNOTSUPP;
  594. break;
  595. }
  596. res = dest_name_check(dest, m);
  597. if (res)
  598. break;
  599. res = tipc_port_mcast_xmit(port,
  600. &dest->addr.nameseq,
  601. m->msg_iov,
  602. total_len);
  603. }
  604. if (likely(res != -ELINKCONG)) {
  605. if (needs_conn && (res >= 0))
  606. sock->state = SS_CONNECTING;
  607. break;
  608. }
  609. res = tipc_wait_for_sndmsg(sock, &timeo);
  610. if (res)
  611. break;
  612. } while (1);
  613. exit:
  614. if (iocb)
  615. release_sock(sk);
  616. return res;
  617. }
  618. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  619. {
  620. struct sock *sk = sock->sk;
  621. struct tipc_sock *tsk = tipc_sk(sk);
  622. struct tipc_port *port = &tsk->port;
  623. DEFINE_WAIT(wait);
  624. int done;
  625. do {
  626. int err = sock_error(sk);
  627. if (err)
  628. return err;
  629. if (sock->state == SS_DISCONNECTING)
  630. return -EPIPE;
  631. else if (sock->state != SS_CONNECTED)
  632. return -ENOTCONN;
  633. if (!*timeo_p)
  634. return -EAGAIN;
  635. if (signal_pending(current))
  636. return sock_intr_errno(*timeo_p);
  637. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  638. done = sk_wait_event(sk, timeo_p,
  639. (!port->congested || !port->connected));
  640. finish_wait(sk_sleep(sk), &wait);
  641. } while (!done);
  642. return 0;
  643. }
  644. /**
  645. * tipc_send_packet - send a connection-oriented message
  646. * @iocb: if NULL, indicates that socket lock is already held
  647. * @sock: socket structure
  648. * @m: message to send
  649. * @total_len: length of message
  650. *
  651. * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
  652. *
  653. * Returns the number of bytes sent on success, or errno otherwise
  654. */
  655. static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
  656. struct msghdr *m, size_t total_len)
  657. {
  658. struct sock *sk = sock->sk;
  659. struct tipc_sock *tsk = tipc_sk(sk);
  660. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  661. int res = -EINVAL;
  662. long timeo;
  663. /* Handle implied connection establishment */
  664. if (unlikely(dest))
  665. return tipc_sendmsg(iocb, sock, m, total_len);
  666. if (total_len > TIPC_MAX_USER_MSG_SIZE)
  667. return -EMSGSIZE;
  668. if (iocb)
  669. lock_sock(sk);
  670. if (unlikely(sock->state != SS_CONNECTED)) {
  671. if (sock->state == SS_DISCONNECTING)
  672. res = -EPIPE;
  673. else
  674. res = -ENOTCONN;
  675. goto exit;
  676. }
  677. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  678. do {
  679. res = tipc_send(&tsk->port, m->msg_iov, total_len);
  680. if (likely(res != -ELINKCONG))
  681. break;
  682. res = tipc_wait_for_sndpkt(sock, &timeo);
  683. if (res)
  684. break;
  685. } while (1);
  686. exit:
  687. if (iocb)
  688. release_sock(sk);
  689. return res;
  690. }
  691. /**
  692. * tipc_send_stream - send stream-oriented data
  693. * @iocb: (unused)
  694. * @sock: socket structure
  695. * @m: data to send
  696. * @total_len: total length of data to be sent
  697. *
  698. * Used for SOCK_STREAM data.
  699. *
  700. * Returns the number of bytes sent on success (or partial success),
  701. * or errno if no data sent
  702. */
  703. static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
  704. struct msghdr *m, size_t total_len)
  705. {
  706. struct sock *sk = sock->sk;
  707. struct tipc_sock *tsk = tipc_sk(sk);
  708. struct msghdr my_msg;
  709. struct iovec my_iov;
  710. struct iovec *curr_iov;
  711. int curr_iovlen;
  712. char __user *curr_start;
  713. u32 hdr_size;
  714. int curr_left;
  715. int bytes_to_send;
  716. int bytes_sent;
  717. int res;
  718. lock_sock(sk);
  719. /* Handle special cases where there is no connection */
  720. if (unlikely(sock->state != SS_CONNECTED)) {
  721. if (sock->state == SS_UNCONNECTED)
  722. res = tipc_send_packet(NULL, sock, m, total_len);
  723. else
  724. res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
  725. goto exit;
  726. }
  727. if (unlikely(m->msg_name)) {
  728. res = -EISCONN;
  729. goto exit;
  730. }
  731. if (total_len > (unsigned int)INT_MAX) {
  732. res = -EMSGSIZE;
  733. goto exit;
  734. }
  735. /*
  736. * Send each iovec entry using one or more messages
  737. *
  738. * Note: This algorithm is good for the most likely case
  739. * (i.e. one large iovec entry), but could be improved to pass sets
  740. * of small iovec entries into send_packet().
  741. */
  742. curr_iov = m->msg_iov;
  743. curr_iovlen = m->msg_iovlen;
  744. my_msg.msg_iov = &my_iov;
  745. my_msg.msg_iovlen = 1;
  746. my_msg.msg_flags = m->msg_flags;
  747. my_msg.msg_name = NULL;
  748. bytes_sent = 0;
  749. hdr_size = msg_hdr_sz(&tsk->port.phdr);
  750. while (curr_iovlen--) {
  751. curr_start = curr_iov->iov_base;
  752. curr_left = curr_iov->iov_len;
  753. while (curr_left) {
  754. bytes_to_send = tsk->port.max_pkt - hdr_size;
  755. if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
  756. bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
  757. if (curr_left < bytes_to_send)
  758. bytes_to_send = curr_left;
  759. my_iov.iov_base = curr_start;
  760. my_iov.iov_len = bytes_to_send;
  761. res = tipc_send_packet(NULL, sock, &my_msg,
  762. bytes_to_send);
  763. if (res < 0) {
  764. if (bytes_sent)
  765. res = bytes_sent;
  766. goto exit;
  767. }
  768. curr_left -= bytes_to_send;
  769. curr_start += bytes_to_send;
  770. bytes_sent += bytes_to_send;
  771. }
  772. curr_iov++;
  773. }
  774. res = bytes_sent;
  775. exit:
  776. release_sock(sk);
  777. return res;
  778. }
  779. /**
  780. * auto_connect - complete connection setup to a remote port
  781. * @tsk: tipc socket structure
  782. * @msg: peer's response message
  783. *
  784. * Returns 0 on success, errno otherwise
  785. */
  786. static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
  787. {
  788. struct tipc_port *port = &tsk->port;
  789. struct socket *sock = tsk->sk.sk_socket;
  790. struct tipc_portid peer;
  791. peer.ref = msg_origport(msg);
  792. peer.node = msg_orignode(msg);
  793. __tipc_port_connect(port->ref, port, &peer);
  794. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  795. return -EINVAL;
  796. msg_set_importance(&port->phdr, (u32)msg_importance(msg));
  797. sock->state = SS_CONNECTED;
  798. return 0;
  799. }
  800. /**
  801. * set_orig_addr - capture sender's address for received message
  802. * @m: descriptor for message info
  803. * @msg: received message header
  804. *
  805. * Note: Address is not captured if not requested by receiver.
  806. */
  807. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  808. {
  809. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  810. if (addr) {
  811. addr->family = AF_TIPC;
  812. addr->addrtype = TIPC_ADDR_ID;
  813. memset(&addr->addr, 0, sizeof(addr->addr));
  814. addr->addr.id.ref = msg_origport(msg);
  815. addr->addr.id.node = msg_orignode(msg);
  816. addr->addr.name.domain = 0; /* could leave uninitialized */
  817. addr->scope = 0; /* could leave uninitialized */
  818. m->msg_namelen = sizeof(struct sockaddr_tipc);
  819. }
  820. }
  821. /**
  822. * anc_data_recv - optionally capture ancillary data for received message
  823. * @m: descriptor for message info
  824. * @msg: received message header
  825. * @tport: TIPC port associated with message
  826. *
  827. * Note: Ancillary data is not captured if not requested by receiver.
  828. *
  829. * Returns 0 if successful, otherwise errno
  830. */
  831. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  832. struct tipc_port *tport)
  833. {
  834. u32 anc_data[3];
  835. u32 err;
  836. u32 dest_type;
  837. int has_name;
  838. int res;
  839. if (likely(m->msg_controllen == 0))
  840. return 0;
  841. /* Optionally capture errored message object(s) */
  842. err = msg ? msg_errcode(msg) : 0;
  843. if (unlikely(err)) {
  844. anc_data[0] = err;
  845. anc_data[1] = msg_data_sz(msg);
  846. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  847. if (res)
  848. return res;
  849. if (anc_data[1]) {
  850. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  851. msg_data(msg));
  852. if (res)
  853. return res;
  854. }
  855. }
  856. /* Optionally capture message destination object */
  857. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  858. switch (dest_type) {
  859. case TIPC_NAMED_MSG:
  860. has_name = 1;
  861. anc_data[0] = msg_nametype(msg);
  862. anc_data[1] = msg_namelower(msg);
  863. anc_data[2] = msg_namelower(msg);
  864. break;
  865. case TIPC_MCAST_MSG:
  866. has_name = 1;
  867. anc_data[0] = msg_nametype(msg);
  868. anc_data[1] = msg_namelower(msg);
  869. anc_data[2] = msg_nameupper(msg);
  870. break;
  871. case TIPC_CONN_MSG:
  872. has_name = (tport->conn_type != 0);
  873. anc_data[0] = tport->conn_type;
  874. anc_data[1] = tport->conn_instance;
  875. anc_data[2] = tport->conn_instance;
  876. break;
  877. default:
  878. has_name = 0;
  879. }
  880. if (has_name) {
  881. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  882. if (res)
  883. return res;
  884. }
  885. return 0;
  886. }
  887. static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
  888. {
  889. struct sock *sk = sock->sk;
  890. DEFINE_WAIT(wait);
  891. int err;
  892. for (;;) {
  893. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  894. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  895. if (sock->state == SS_DISCONNECTING) {
  896. err = -ENOTCONN;
  897. break;
  898. }
  899. release_sock(sk);
  900. timeo = schedule_timeout(timeo);
  901. lock_sock(sk);
  902. }
  903. err = 0;
  904. if (!skb_queue_empty(&sk->sk_receive_queue))
  905. break;
  906. err = sock_intr_errno(timeo);
  907. if (signal_pending(current))
  908. break;
  909. err = -EAGAIN;
  910. if (!timeo)
  911. break;
  912. }
  913. finish_wait(sk_sleep(sk), &wait);
  914. return err;
  915. }
  916. /**
  917. * tipc_recvmsg - receive packet-oriented message
  918. * @iocb: (unused)
  919. * @m: descriptor for message info
  920. * @buf_len: total size of user buffer area
  921. * @flags: receive flags
  922. *
  923. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  924. * If the complete message doesn't fit in user area, truncate it.
  925. *
  926. * Returns size of returned message data, errno otherwise
  927. */
  928. static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
  929. struct msghdr *m, size_t buf_len, int flags)
  930. {
  931. struct sock *sk = sock->sk;
  932. struct tipc_sock *tsk = tipc_sk(sk);
  933. struct tipc_port *port = &tsk->port;
  934. struct sk_buff *buf;
  935. struct tipc_msg *msg;
  936. long timeo;
  937. unsigned int sz;
  938. u32 err;
  939. int res;
  940. /* Catch invalid receive requests */
  941. if (unlikely(!buf_len))
  942. return -EINVAL;
  943. lock_sock(sk);
  944. if (unlikely(sock->state == SS_UNCONNECTED)) {
  945. res = -ENOTCONN;
  946. goto exit;
  947. }
  948. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  949. restart:
  950. /* Look for a message in receive queue; wait if necessary */
  951. res = tipc_wait_for_rcvmsg(sock, timeo);
  952. if (res)
  953. goto exit;
  954. /* Look at first message in receive queue */
  955. buf = skb_peek(&sk->sk_receive_queue);
  956. msg = buf_msg(buf);
  957. sz = msg_data_sz(msg);
  958. err = msg_errcode(msg);
  959. /* Discard an empty non-errored message & try again */
  960. if ((!sz) && (!err)) {
  961. advance_rx_queue(sk);
  962. goto restart;
  963. }
  964. /* Capture sender's address (optional) */
  965. set_orig_addr(m, msg);
  966. /* Capture ancillary data (optional) */
  967. res = anc_data_recv(m, msg, port);
  968. if (res)
  969. goto exit;
  970. /* Capture message data (if valid) & compute return value (always) */
  971. if (!err) {
  972. if (unlikely(buf_len < sz)) {
  973. sz = buf_len;
  974. m->msg_flags |= MSG_TRUNC;
  975. }
  976. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  977. m->msg_iov, sz);
  978. if (res)
  979. goto exit;
  980. res = sz;
  981. } else {
  982. if ((sock->state == SS_READY) ||
  983. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  984. res = 0;
  985. else
  986. res = -ECONNRESET;
  987. }
  988. /* Consume received message (optional) */
  989. if (likely(!(flags & MSG_PEEK))) {
  990. if ((sock->state != SS_READY) &&
  991. (++port->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  992. tipc_acknowledge(port->ref, port->conn_unacked);
  993. advance_rx_queue(sk);
  994. }
  995. exit:
  996. release_sock(sk);
  997. return res;
  998. }
  999. /**
  1000. * tipc_recv_stream - receive stream-oriented data
  1001. * @iocb: (unused)
  1002. * @m: descriptor for message info
  1003. * @buf_len: total size of user buffer area
  1004. * @flags: receive flags
  1005. *
  1006. * Used for SOCK_STREAM messages only. If not enough data is available
  1007. * will optionally wait for more; never truncates data.
  1008. *
  1009. * Returns size of returned message data, errno otherwise
  1010. */
  1011. static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
  1012. struct msghdr *m, size_t buf_len, int flags)
  1013. {
  1014. struct sock *sk = sock->sk;
  1015. struct tipc_sock *tsk = tipc_sk(sk);
  1016. struct tipc_port *port = &tsk->port;
  1017. struct sk_buff *buf;
  1018. struct tipc_msg *msg;
  1019. long timeo;
  1020. unsigned int sz;
  1021. int sz_to_copy, target, needed;
  1022. int sz_copied = 0;
  1023. u32 err;
  1024. int res = 0;
  1025. /* Catch invalid receive attempts */
  1026. if (unlikely(!buf_len))
  1027. return -EINVAL;
  1028. lock_sock(sk);
  1029. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1030. res = -ENOTCONN;
  1031. goto exit;
  1032. }
  1033. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1034. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1035. restart:
  1036. /* Look for a message in receive queue; wait if necessary */
  1037. res = tipc_wait_for_rcvmsg(sock, timeo);
  1038. if (res)
  1039. goto exit;
  1040. /* Look at first message in receive queue */
  1041. buf = skb_peek(&sk->sk_receive_queue);
  1042. msg = buf_msg(buf);
  1043. sz = msg_data_sz(msg);
  1044. err = msg_errcode(msg);
  1045. /* Discard an empty non-errored message & try again */
  1046. if ((!sz) && (!err)) {
  1047. advance_rx_queue(sk);
  1048. goto restart;
  1049. }
  1050. /* Optionally capture sender's address & ancillary data of first msg */
  1051. if (sz_copied == 0) {
  1052. set_orig_addr(m, msg);
  1053. res = anc_data_recv(m, msg, port);
  1054. if (res)
  1055. goto exit;
  1056. }
  1057. /* Capture message data (if valid) & compute return value (always) */
  1058. if (!err) {
  1059. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1060. sz -= offset;
  1061. needed = (buf_len - sz_copied);
  1062. sz_to_copy = (sz <= needed) ? sz : needed;
  1063. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  1064. m->msg_iov, sz_to_copy);
  1065. if (res)
  1066. goto exit;
  1067. sz_copied += sz_to_copy;
  1068. if (sz_to_copy < sz) {
  1069. if (!(flags & MSG_PEEK))
  1070. TIPC_SKB_CB(buf)->handle =
  1071. (void *)(unsigned long)(offset + sz_to_copy);
  1072. goto exit;
  1073. }
  1074. } else {
  1075. if (sz_copied != 0)
  1076. goto exit; /* can't add error msg to valid data */
  1077. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1078. res = 0;
  1079. else
  1080. res = -ECONNRESET;
  1081. }
  1082. /* Consume received message (optional) */
  1083. if (likely(!(flags & MSG_PEEK))) {
  1084. if (unlikely(++port->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  1085. tipc_acknowledge(port->ref, port->conn_unacked);
  1086. advance_rx_queue(sk);
  1087. }
  1088. /* Loop around if more data is required */
  1089. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1090. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1091. (sz_copied < target)) && /* and more is ready or required */
  1092. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1093. (!err)) /* and haven't reached a FIN */
  1094. goto restart;
  1095. exit:
  1096. release_sock(sk);
  1097. return sz_copied ? sz_copied : res;
  1098. }
  1099. /**
  1100. * tipc_write_space - wake up thread if port congestion is released
  1101. * @sk: socket
  1102. */
  1103. static void tipc_write_space(struct sock *sk)
  1104. {
  1105. struct socket_wq *wq;
  1106. rcu_read_lock();
  1107. wq = rcu_dereference(sk->sk_wq);
  1108. if (wq_has_sleeper(wq))
  1109. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1110. POLLWRNORM | POLLWRBAND);
  1111. rcu_read_unlock();
  1112. }
  1113. /**
  1114. * tipc_data_ready - wake up threads to indicate messages have been received
  1115. * @sk: socket
  1116. * @len: the length of messages
  1117. */
  1118. static void tipc_data_ready(struct sock *sk)
  1119. {
  1120. struct socket_wq *wq;
  1121. rcu_read_lock();
  1122. wq = rcu_dereference(sk->sk_wq);
  1123. if (wq_has_sleeper(wq))
  1124. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1125. POLLRDNORM | POLLRDBAND);
  1126. rcu_read_unlock();
  1127. }
  1128. /**
  1129. * filter_connect - Handle all incoming messages for a connection-based socket
  1130. * @tsk: TIPC socket
  1131. * @msg: message
  1132. *
  1133. * Returns TIPC error status code and socket error status code
  1134. * once it encounters some errors
  1135. */
  1136. static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
  1137. {
  1138. struct sock *sk = &tsk->sk;
  1139. struct tipc_port *port = &tsk->port;
  1140. struct socket *sock = sk->sk_socket;
  1141. struct tipc_msg *msg = buf_msg(*buf);
  1142. u32 retval = TIPC_ERR_NO_PORT;
  1143. int res;
  1144. if (msg_mcast(msg))
  1145. return retval;
  1146. switch ((int)sock->state) {
  1147. case SS_CONNECTED:
  1148. /* Accept only connection-based messages sent by peer */
  1149. if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
  1150. if (unlikely(msg_errcode(msg))) {
  1151. sock->state = SS_DISCONNECTING;
  1152. __tipc_port_disconnect(port);
  1153. }
  1154. retval = TIPC_OK;
  1155. }
  1156. break;
  1157. case SS_CONNECTING:
  1158. /* Accept only ACK or NACK message */
  1159. if (unlikely(msg_errcode(msg))) {
  1160. sock->state = SS_DISCONNECTING;
  1161. sk->sk_err = ECONNREFUSED;
  1162. retval = TIPC_OK;
  1163. break;
  1164. }
  1165. if (unlikely(!msg_connected(msg)))
  1166. break;
  1167. res = auto_connect(tsk, msg);
  1168. if (res) {
  1169. sock->state = SS_DISCONNECTING;
  1170. sk->sk_err = -res;
  1171. retval = TIPC_OK;
  1172. break;
  1173. }
  1174. /* If an incoming message is an 'ACK-', it should be
  1175. * discarded here because it doesn't contain useful
  1176. * data. In addition, we should try to wake up
  1177. * connect() routine if sleeping.
  1178. */
  1179. if (msg_data_sz(msg) == 0) {
  1180. kfree_skb(*buf);
  1181. *buf = NULL;
  1182. if (waitqueue_active(sk_sleep(sk)))
  1183. wake_up_interruptible(sk_sleep(sk));
  1184. }
  1185. retval = TIPC_OK;
  1186. break;
  1187. case SS_LISTENING:
  1188. case SS_UNCONNECTED:
  1189. /* Accept only SYN message */
  1190. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1191. retval = TIPC_OK;
  1192. break;
  1193. case SS_DISCONNECTING:
  1194. break;
  1195. default:
  1196. pr_err("Unknown socket state %u\n", sock->state);
  1197. }
  1198. return retval;
  1199. }
  1200. /**
  1201. * rcvbuf_limit - get proper overload limit of socket receive queue
  1202. * @sk: socket
  1203. * @buf: message
  1204. *
  1205. * For all connection oriented messages, irrespective of importance,
  1206. * the default overload value (i.e. 67MB) is set as limit.
  1207. *
  1208. * For all connectionless messages, by default new queue limits are
  1209. * as belows:
  1210. *
  1211. * TIPC_LOW_IMPORTANCE (4 MB)
  1212. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1213. * TIPC_HIGH_IMPORTANCE (16 MB)
  1214. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1215. *
  1216. * Returns overload limit according to corresponding message importance
  1217. */
  1218. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1219. {
  1220. struct tipc_msg *msg = buf_msg(buf);
  1221. if (msg_connected(msg))
  1222. return sysctl_tipc_rmem[2];
  1223. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1224. msg_importance(msg);
  1225. }
  1226. /**
  1227. * filter_rcv - validate incoming message
  1228. * @sk: socket
  1229. * @buf: message
  1230. *
  1231. * Enqueues message on receive queue if acceptable; optionally handles
  1232. * disconnect indication for a connected socket.
  1233. *
  1234. * Called with socket lock already taken; port lock may also be taken.
  1235. *
  1236. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1237. */
  1238. static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
  1239. {
  1240. struct socket *sock = sk->sk_socket;
  1241. struct tipc_sock *tsk = tipc_sk(sk);
  1242. struct tipc_msg *msg = buf_msg(buf);
  1243. unsigned int limit = rcvbuf_limit(sk, buf);
  1244. u32 res = TIPC_OK;
  1245. /* Reject message if it is wrong sort of message for socket */
  1246. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1247. return TIPC_ERR_NO_PORT;
  1248. if (sock->state == SS_READY) {
  1249. if (msg_connected(msg))
  1250. return TIPC_ERR_NO_PORT;
  1251. } else {
  1252. res = filter_connect(tsk, &buf);
  1253. if (res != TIPC_OK || buf == NULL)
  1254. return res;
  1255. }
  1256. /* Reject message if there isn't room to queue it */
  1257. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1258. return TIPC_ERR_OVERLOAD;
  1259. /* Enqueue message */
  1260. TIPC_SKB_CB(buf)->handle = NULL;
  1261. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1262. skb_set_owner_r(buf, sk);
  1263. sk->sk_data_ready(sk);
  1264. return TIPC_OK;
  1265. }
  1266. /**
  1267. * backlog_rcv - handle incoming message from backlog queue
  1268. * @sk: socket
  1269. * @buf: message
  1270. *
  1271. * Caller must hold socket lock, but not port lock.
  1272. *
  1273. * Returns 0
  1274. */
  1275. static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1276. {
  1277. u32 res;
  1278. res = filter_rcv(sk, buf);
  1279. if (res)
  1280. tipc_reject_msg(buf, res);
  1281. return 0;
  1282. }
  1283. /**
  1284. * tipc_sk_rcv - handle incoming message
  1285. * @sk: socket receiving message
  1286. * @buf: message
  1287. *
  1288. * Called with port lock already taken.
  1289. *
  1290. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1291. */
  1292. u32 tipc_sk_rcv(struct sock *sk, struct sk_buff *buf)
  1293. {
  1294. u32 res;
  1295. /*
  1296. * Process message if socket is unlocked; otherwise add to backlog queue
  1297. *
  1298. * This code is based on sk_receive_skb(), but must be distinct from it
  1299. * since a TIPC-specific filter/reject mechanism is utilized
  1300. */
  1301. bh_lock_sock(sk);
  1302. if (!sock_owned_by_user(sk)) {
  1303. res = filter_rcv(sk, buf);
  1304. } else {
  1305. if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
  1306. res = TIPC_ERR_OVERLOAD;
  1307. else
  1308. res = TIPC_OK;
  1309. }
  1310. bh_unlock_sock(sk);
  1311. return res;
  1312. }
  1313. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1314. {
  1315. struct sock *sk = sock->sk;
  1316. DEFINE_WAIT(wait);
  1317. int done;
  1318. do {
  1319. int err = sock_error(sk);
  1320. if (err)
  1321. return err;
  1322. if (!*timeo_p)
  1323. return -ETIMEDOUT;
  1324. if (signal_pending(current))
  1325. return sock_intr_errno(*timeo_p);
  1326. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1327. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1328. finish_wait(sk_sleep(sk), &wait);
  1329. } while (!done);
  1330. return 0;
  1331. }
  1332. /**
  1333. * tipc_connect - establish a connection to another TIPC port
  1334. * @sock: socket structure
  1335. * @dest: socket address for destination port
  1336. * @destlen: size of socket address data structure
  1337. * @flags: file-related flags associated with socket
  1338. *
  1339. * Returns 0 on success, errno otherwise
  1340. */
  1341. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1342. int destlen, int flags)
  1343. {
  1344. struct sock *sk = sock->sk;
  1345. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1346. struct msghdr m = {NULL,};
  1347. long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1348. socket_state previous;
  1349. int res;
  1350. lock_sock(sk);
  1351. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1352. if (sock->state == SS_READY) {
  1353. res = -EOPNOTSUPP;
  1354. goto exit;
  1355. }
  1356. /*
  1357. * Reject connection attempt using multicast address
  1358. *
  1359. * Note: send_msg() validates the rest of the address fields,
  1360. * so there's no need to do it here
  1361. */
  1362. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1363. res = -EINVAL;
  1364. goto exit;
  1365. }
  1366. previous = sock->state;
  1367. switch (sock->state) {
  1368. case SS_UNCONNECTED:
  1369. /* Send a 'SYN-' to destination */
  1370. m.msg_name = dest;
  1371. m.msg_namelen = destlen;
  1372. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1373. * indicate send_msg() is never blocked.
  1374. */
  1375. if (!timeout)
  1376. m.msg_flags = MSG_DONTWAIT;
  1377. res = tipc_sendmsg(NULL, sock, &m, 0);
  1378. if ((res < 0) && (res != -EWOULDBLOCK))
  1379. goto exit;
  1380. /* Just entered SS_CONNECTING state; the only
  1381. * difference is that return value in non-blocking
  1382. * case is EINPROGRESS, rather than EALREADY.
  1383. */
  1384. res = -EINPROGRESS;
  1385. case SS_CONNECTING:
  1386. if (previous == SS_CONNECTING)
  1387. res = -EALREADY;
  1388. if (!timeout)
  1389. goto exit;
  1390. timeout = msecs_to_jiffies(timeout);
  1391. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1392. res = tipc_wait_for_connect(sock, &timeout);
  1393. break;
  1394. case SS_CONNECTED:
  1395. res = -EISCONN;
  1396. break;
  1397. default:
  1398. res = -EINVAL;
  1399. break;
  1400. }
  1401. exit:
  1402. release_sock(sk);
  1403. return res;
  1404. }
  1405. /**
  1406. * tipc_listen - allow socket to listen for incoming connections
  1407. * @sock: socket structure
  1408. * @len: (unused)
  1409. *
  1410. * Returns 0 on success, errno otherwise
  1411. */
  1412. static int tipc_listen(struct socket *sock, int len)
  1413. {
  1414. struct sock *sk = sock->sk;
  1415. int res;
  1416. lock_sock(sk);
  1417. if (sock->state != SS_UNCONNECTED)
  1418. res = -EINVAL;
  1419. else {
  1420. sock->state = SS_LISTENING;
  1421. res = 0;
  1422. }
  1423. release_sock(sk);
  1424. return res;
  1425. }
  1426. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1427. {
  1428. struct sock *sk = sock->sk;
  1429. DEFINE_WAIT(wait);
  1430. int err;
  1431. /* True wake-one mechanism for incoming connections: only
  1432. * one process gets woken up, not the 'whole herd'.
  1433. * Since we do not 'race & poll' for established sockets
  1434. * anymore, the common case will execute the loop only once.
  1435. */
  1436. for (;;) {
  1437. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1438. TASK_INTERRUPTIBLE);
  1439. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1440. release_sock(sk);
  1441. timeo = schedule_timeout(timeo);
  1442. lock_sock(sk);
  1443. }
  1444. err = 0;
  1445. if (!skb_queue_empty(&sk->sk_receive_queue))
  1446. break;
  1447. err = -EINVAL;
  1448. if (sock->state != SS_LISTENING)
  1449. break;
  1450. err = sock_intr_errno(timeo);
  1451. if (signal_pending(current))
  1452. break;
  1453. err = -EAGAIN;
  1454. if (!timeo)
  1455. break;
  1456. }
  1457. finish_wait(sk_sleep(sk), &wait);
  1458. return err;
  1459. }
  1460. /**
  1461. * tipc_accept - wait for connection request
  1462. * @sock: listening socket
  1463. * @newsock: new socket that is to be connected
  1464. * @flags: file-related flags associated with socket
  1465. *
  1466. * Returns 0 on success, errno otherwise
  1467. */
  1468. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
  1469. {
  1470. struct sock *new_sk, *sk = sock->sk;
  1471. struct sk_buff *buf;
  1472. struct tipc_port *new_port;
  1473. struct tipc_msg *msg;
  1474. struct tipc_portid peer;
  1475. u32 new_ref;
  1476. long timeo;
  1477. int res;
  1478. lock_sock(sk);
  1479. if (sock->state != SS_LISTENING) {
  1480. res = -EINVAL;
  1481. goto exit;
  1482. }
  1483. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1484. res = tipc_wait_for_accept(sock, timeo);
  1485. if (res)
  1486. goto exit;
  1487. buf = skb_peek(&sk->sk_receive_queue);
  1488. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1489. if (res)
  1490. goto exit;
  1491. new_sk = new_sock->sk;
  1492. new_port = &tipc_sk(new_sk)->port;
  1493. new_ref = new_port->ref;
  1494. msg = buf_msg(buf);
  1495. /* we lock on new_sk; but lockdep sees the lock on sk */
  1496. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1497. /*
  1498. * Reject any stray messages received by new socket
  1499. * before the socket lock was taken (very, very unlikely)
  1500. */
  1501. reject_rx_queue(new_sk);
  1502. /* Connect new socket to it's peer */
  1503. peer.ref = msg_origport(msg);
  1504. peer.node = msg_orignode(msg);
  1505. tipc_port_connect(new_ref, &peer);
  1506. new_sock->state = SS_CONNECTED;
  1507. tipc_port_set_importance(new_port, msg_importance(msg));
  1508. if (msg_named(msg)) {
  1509. new_port->conn_type = msg_nametype(msg);
  1510. new_port->conn_instance = msg_nameinst(msg);
  1511. }
  1512. /*
  1513. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1514. * Respond to 'SYN+' by queuing it on new socket.
  1515. */
  1516. if (!msg_data_sz(msg)) {
  1517. struct msghdr m = {NULL,};
  1518. advance_rx_queue(sk);
  1519. tipc_send_packet(NULL, new_sock, &m, 0);
  1520. } else {
  1521. __skb_dequeue(&sk->sk_receive_queue);
  1522. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1523. skb_set_owner_r(buf, new_sk);
  1524. }
  1525. release_sock(new_sk);
  1526. exit:
  1527. release_sock(sk);
  1528. return res;
  1529. }
  1530. /**
  1531. * tipc_shutdown - shutdown socket connection
  1532. * @sock: socket structure
  1533. * @how: direction to close (must be SHUT_RDWR)
  1534. *
  1535. * Terminates connection (if necessary), then purges socket's receive queue.
  1536. *
  1537. * Returns 0 on success, errno otherwise
  1538. */
  1539. static int tipc_shutdown(struct socket *sock, int how)
  1540. {
  1541. struct sock *sk = sock->sk;
  1542. struct tipc_sock *tsk = tipc_sk(sk);
  1543. struct tipc_port *port = &tsk->port;
  1544. struct sk_buff *buf;
  1545. int res;
  1546. if (how != SHUT_RDWR)
  1547. return -EINVAL;
  1548. lock_sock(sk);
  1549. switch (sock->state) {
  1550. case SS_CONNECTING:
  1551. case SS_CONNECTED:
  1552. restart:
  1553. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1554. buf = __skb_dequeue(&sk->sk_receive_queue);
  1555. if (buf) {
  1556. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1557. kfree_skb(buf);
  1558. goto restart;
  1559. }
  1560. tipc_port_disconnect(port->ref);
  1561. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1562. } else {
  1563. tipc_port_shutdown(port->ref);
  1564. }
  1565. sock->state = SS_DISCONNECTING;
  1566. /* fall through */
  1567. case SS_DISCONNECTING:
  1568. /* Discard any unreceived messages */
  1569. __skb_queue_purge(&sk->sk_receive_queue);
  1570. /* Wake up anyone sleeping in poll */
  1571. sk->sk_state_change(sk);
  1572. res = 0;
  1573. break;
  1574. default:
  1575. res = -ENOTCONN;
  1576. }
  1577. release_sock(sk);
  1578. return res;
  1579. }
  1580. /**
  1581. * tipc_setsockopt - set socket option
  1582. * @sock: socket structure
  1583. * @lvl: option level
  1584. * @opt: option identifier
  1585. * @ov: pointer to new option value
  1586. * @ol: length of option value
  1587. *
  1588. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1589. * (to ease compatibility).
  1590. *
  1591. * Returns 0 on success, errno otherwise
  1592. */
  1593. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  1594. char __user *ov, unsigned int ol)
  1595. {
  1596. struct sock *sk = sock->sk;
  1597. struct tipc_sock *tsk = tipc_sk(sk);
  1598. struct tipc_port *port = &tsk->port;
  1599. u32 value;
  1600. int res;
  1601. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1602. return 0;
  1603. if (lvl != SOL_TIPC)
  1604. return -ENOPROTOOPT;
  1605. if (ol < sizeof(value))
  1606. return -EINVAL;
  1607. res = get_user(value, (u32 __user *)ov);
  1608. if (res)
  1609. return res;
  1610. lock_sock(sk);
  1611. switch (opt) {
  1612. case TIPC_IMPORTANCE:
  1613. tipc_port_set_importance(port, value);
  1614. break;
  1615. case TIPC_SRC_DROPPABLE:
  1616. if (sock->type != SOCK_STREAM)
  1617. tipc_port_set_unreliable(port, value);
  1618. else
  1619. res = -ENOPROTOOPT;
  1620. break;
  1621. case TIPC_DEST_DROPPABLE:
  1622. tipc_port_set_unreturnable(port, value);
  1623. break;
  1624. case TIPC_CONN_TIMEOUT:
  1625. tipc_sk(sk)->conn_timeout = value;
  1626. /* no need to set "res", since already 0 at this point */
  1627. break;
  1628. default:
  1629. res = -EINVAL;
  1630. }
  1631. release_sock(sk);
  1632. return res;
  1633. }
  1634. /**
  1635. * tipc_getsockopt - get socket option
  1636. * @sock: socket structure
  1637. * @lvl: option level
  1638. * @opt: option identifier
  1639. * @ov: receptacle for option value
  1640. * @ol: receptacle for length of option value
  1641. *
  1642. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1643. * (to ease compatibility).
  1644. *
  1645. * Returns 0 on success, errno otherwise
  1646. */
  1647. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  1648. char __user *ov, int __user *ol)
  1649. {
  1650. struct sock *sk = sock->sk;
  1651. struct tipc_sock *tsk = tipc_sk(sk);
  1652. struct tipc_port *port = &tsk->port;
  1653. int len;
  1654. u32 value;
  1655. int res;
  1656. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1657. return put_user(0, ol);
  1658. if (lvl != SOL_TIPC)
  1659. return -ENOPROTOOPT;
  1660. res = get_user(len, ol);
  1661. if (res)
  1662. return res;
  1663. lock_sock(sk);
  1664. switch (opt) {
  1665. case TIPC_IMPORTANCE:
  1666. value = tipc_port_importance(port);
  1667. break;
  1668. case TIPC_SRC_DROPPABLE:
  1669. value = tipc_port_unreliable(port);
  1670. break;
  1671. case TIPC_DEST_DROPPABLE:
  1672. value = tipc_port_unreturnable(port);
  1673. break;
  1674. case TIPC_CONN_TIMEOUT:
  1675. value = tipc_sk(sk)->conn_timeout;
  1676. /* no need to set "res", since already 0 at this point */
  1677. break;
  1678. case TIPC_NODE_RECVQ_DEPTH:
  1679. value = 0; /* was tipc_queue_size, now obsolete */
  1680. break;
  1681. case TIPC_SOCK_RECVQ_DEPTH:
  1682. value = skb_queue_len(&sk->sk_receive_queue);
  1683. break;
  1684. default:
  1685. res = -EINVAL;
  1686. }
  1687. release_sock(sk);
  1688. if (res)
  1689. return res; /* "get" failed */
  1690. if (len < sizeof(value))
  1691. return -EINVAL;
  1692. if (copy_to_user(ov, &value, sizeof(value)))
  1693. return -EFAULT;
  1694. return put_user(sizeof(value), ol);
  1695. }
  1696. /* Protocol switches for the various types of TIPC sockets */
  1697. static const struct proto_ops msg_ops = {
  1698. .owner = THIS_MODULE,
  1699. .family = AF_TIPC,
  1700. .release = tipc_release,
  1701. .bind = tipc_bind,
  1702. .connect = tipc_connect,
  1703. .socketpair = sock_no_socketpair,
  1704. .accept = sock_no_accept,
  1705. .getname = tipc_getname,
  1706. .poll = tipc_poll,
  1707. .ioctl = sock_no_ioctl,
  1708. .listen = sock_no_listen,
  1709. .shutdown = tipc_shutdown,
  1710. .setsockopt = tipc_setsockopt,
  1711. .getsockopt = tipc_getsockopt,
  1712. .sendmsg = tipc_sendmsg,
  1713. .recvmsg = tipc_recvmsg,
  1714. .mmap = sock_no_mmap,
  1715. .sendpage = sock_no_sendpage
  1716. };
  1717. static const struct proto_ops packet_ops = {
  1718. .owner = THIS_MODULE,
  1719. .family = AF_TIPC,
  1720. .release = tipc_release,
  1721. .bind = tipc_bind,
  1722. .connect = tipc_connect,
  1723. .socketpair = sock_no_socketpair,
  1724. .accept = tipc_accept,
  1725. .getname = tipc_getname,
  1726. .poll = tipc_poll,
  1727. .ioctl = sock_no_ioctl,
  1728. .listen = tipc_listen,
  1729. .shutdown = tipc_shutdown,
  1730. .setsockopt = tipc_setsockopt,
  1731. .getsockopt = tipc_getsockopt,
  1732. .sendmsg = tipc_send_packet,
  1733. .recvmsg = tipc_recvmsg,
  1734. .mmap = sock_no_mmap,
  1735. .sendpage = sock_no_sendpage
  1736. };
  1737. static const struct proto_ops stream_ops = {
  1738. .owner = THIS_MODULE,
  1739. .family = AF_TIPC,
  1740. .release = tipc_release,
  1741. .bind = tipc_bind,
  1742. .connect = tipc_connect,
  1743. .socketpair = sock_no_socketpair,
  1744. .accept = tipc_accept,
  1745. .getname = tipc_getname,
  1746. .poll = tipc_poll,
  1747. .ioctl = sock_no_ioctl,
  1748. .listen = tipc_listen,
  1749. .shutdown = tipc_shutdown,
  1750. .setsockopt = tipc_setsockopt,
  1751. .getsockopt = tipc_getsockopt,
  1752. .sendmsg = tipc_send_stream,
  1753. .recvmsg = tipc_recv_stream,
  1754. .mmap = sock_no_mmap,
  1755. .sendpage = sock_no_sendpage
  1756. };
  1757. static const struct net_proto_family tipc_family_ops = {
  1758. .owner = THIS_MODULE,
  1759. .family = AF_TIPC,
  1760. .create = tipc_sk_create
  1761. };
  1762. static struct proto tipc_proto = {
  1763. .name = "TIPC",
  1764. .owner = THIS_MODULE,
  1765. .obj_size = sizeof(struct tipc_sock),
  1766. .sysctl_rmem = sysctl_tipc_rmem
  1767. };
  1768. static struct proto tipc_proto_kern = {
  1769. .name = "TIPC",
  1770. .obj_size = sizeof(struct tipc_sock),
  1771. .sysctl_rmem = sysctl_tipc_rmem
  1772. };
  1773. /**
  1774. * tipc_socket_init - initialize TIPC socket interface
  1775. *
  1776. * Returns 0 on success, errno otherwise
  1777. */
  1778. int tipc_socket_init(void)
  1779. {
  1780. int res;
  1781. res = proto_register(&tipc_proto, 1);
  1782. if (res) {
  1783. pr_err("Failed to register TIPC protocol type\n");
  1784. goto out;
  1785. }
  1786. res = sock_register(&tipc_family_ops);
  1787. if (res) {
  1788. pr_err("Failed to register TIPC socket type\n");
  1789. proto_unregister(&tipc_proto);
  1790. goto out;
  1791. }
  1792. out:
  1793. return res;
  1794. }
  1795. /**
  1796. * tipc_socket_stop - stop TIPC socket interface
  1797. */
  1798. void tipc_socket_stop(void)
  1799. {
  1800. sock_unregister(tipc_family_ops.family);
  1801. proto_unregister(&tipc_proto);
  1802. }