socket.c 46 KB

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