socket.c 46 KB

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