socket.c 43 KB

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