socket.c 46 KB

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