socket.c 46 KB

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