socket.c 41 KB

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