socket.c 46 KB

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