socket.c 46 KB

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