socket.c 51 KB

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