socket.c 45 KB

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