socket.c 43 KB

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