socket.c 52 KB

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