socket.c 45 KB

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