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