socket.c 46 KB

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