socket.c 41 KB

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