socket.c 43 KB

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