socket.c 41 KB

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