socket.c 47 KB

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