socket.c 51 KB

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