socket.c 41 KB

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