socket.c 47 KB

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