socket.c 43 KB

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