socket.c 47 KB

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