socket.c 43 KB

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