socket.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  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. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  58. struct tipc_sock {
  59. struct sock sk;
  60. struct tipc_port *p;
  61. struct mutex lock;
  62. };
  63. #define tipc_sk(sk) ((struct tipc_sock*)sk)
  64. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
  65. static void wakeupdispatch(struct tipc_port *tport);
  66. static const struct proto_ops packet_ops;
  67. static const struct proto_ops stream_ops;
  68. static const struct proto_ops msg_ops;
  69. static struct proto tipc_proto;
  70. static int sockets_enabled = 0;
  71. static atomic_t tipc_queue_size = ATOMIC_INIT(0);
  72. /*
  73. * sock_lock(): Lock a port/socket pair. lock_sock() can
  74. * not be used here, since the same lock must protect ports
  75. * with non-socket interfaces.
  76. * See net.c for description of locking policy.
  77. */
  78. static void sock_lock(struct tipc_sock* tsock)
  79. {
  80. spin_lock_bh(tsock->p->lock);
  81. }
  82. /*
  83. * sock_unlock(): Unlock a port/socket pair
  84. */
  85. static void sock_unlock(struct tipc_sock* tsock)
  86. {
  87. spin_unlock_bh(tsock->p->lock);
  88. }
  89. /**
  90. * advance_queue - discard first buffer in queue
  91. * @tsock: TIPC socket
  92. */
  93. static void advance_queue(struct tipc_sock *tsock)
  94. {
  95. sock_lock(tsock);
  96. buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
  97. sock_unlock(tsock);
  98. atomic_dec(&tipc_queue_size);
  99. }
  100. /**
  101. * tipc_create - create a TIPC socket
  102. * @sock: pre-allocated socket structure
  103. * @protocol: protocol indicator (must be 0)
  104. *
  105. * This routine creates and attaches a 'struct sock' to the 'struct socket',
  106. * then create and attaches a TIPC port to the 'struct sock' part.
  107. *
  108. * Returns 0 on success, errno otherwise
  109. */
  110. static int tipc_create(struct net *net, struct socket *sock, int protocol)
  111. {
  112. struct tipc_sock *tsock;
  113. struct tipc_port *port;
  114. struct sock *sk;
  115. u32 ref;
  116. if (net != &init_net)
  117. return -EAFNOSUPPORT;
  118. if (unlikely(protocol != 0))
  119. return -EPROTONOSUPPORT;
  120. ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
  121. if (unlikely(!ref))
  122. return -ENOMEM;
  123. sock->state = SS_UNCONNECTED;
  124. switch (sock->type) {
  125. case SOCK_STREAM:
  126. sock->ops = &stream_ops;
  127. break;
  128. case SOCK_SEQPACKET:
  129. sock->ops = &packet_ops;
  130. break;
  131. case SOCK_DGRAM:
  132. tipc_set_portunreliable(ref, 1);
  133. /* fall through */
  134. case SOCK_RDM:
  135. tipc_set_portunreturnable(ref, 1);
  136. sock->ops = &msg_ops;
  137. sock->state = SS_READY;
  138. break;
  139. default:
  140. tipc_deleteport(ref);
  141. return -EPROTOTYPE;
  142. }
  143. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  144. if (!sk) {
  145. tipc_deleteport(ref);
  146. return -ENOMEM;
  147. }
  148. sock_init_data(sock, sk);
  149. sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
  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 ((sock->state != SS_READY) &&
  784. (++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  785. tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
  786. advance_queue(tsock);
  787. }
  788. exit:
  789. mutex_unlock(&tsock->lock);
  790. return res;
  791. }
  792. /**
  793. * recv_stream - receive stream-oriented data
  794. * @iocb: (unused)
  795. * @m: descriptor for message info
  796. * @buf_len: total size of user buffer area
  797. * @flags: receive flags
  798. *
  799. * Used for SOCK_STREAM messages only. If not enough data is available
  800. * will optionally wait for more; never truncates data.
  801. *
  802. * Returns size of returned message data, errno otherwise
  803. */
  804. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  805. struct msghdr *m, size_t buf_len, int flags)
  806. {
  807. struct tipc_sock *tsock = tipc_sk(sock->sk);
  808. struct sk_buff *buf;
  809. struct tipc_msg *msg;
  810. unsigned int q_len;
  811. unsigned int sz;
  812. int sz_to_copy;
  813. int sz_copied = 0;
  814. int needed;
  815. char __user *crs = m->msg_iov->iov_base;
  816. unsigned char *buf_crs;
  817. u32 err;
  818. int res;
  819. /* Currently doesn't support receiving into multiple iovec entries */
  820. if (m->msg_iovlen != 1)
  821. return -EOPNOTSUPP;
  822. /* Catch invalid receive attempts */
  823. if (unlikely(!buf_len))
  824. return -EINVAL;
  825. if (unlikely(sock->state == SS_DISCONNECTING)) {
  826. if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
  827. return -ENOTCONN;
  828. } else if (unlikely(sock->state != SS_CONNECTED))
  829. return -ENOTCONN;
  830. /* Look for a message in receive queue; wait if necessary */
  831. if (unlikely(mutex_lock_interruptible(&tsock->lock)))
  832. return -ERESTARTSYS;
  833. restart:
  834. if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
  835. (flags & MSG_DONTWAIT))) {
  836. res = -EWOULDBLOCK;
  837. goto exit;
  838. }
  839. if ((res = wait_event_interruptible(
  840. *sock->sk->sk_sleep,
  841. ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
  842. (sock->state == SS_DISCONNECTING))) )) {
  843. goto exit;
  844. }
  845. /* Catch attempt to receive on an already terminated connection */
  846. /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
  847. if (!q_len) {
  848. res = -ENOTCONN;
  849. goto exit;
  850. }
  851. /* Get access to first message in receive queue */
  852. buf = skb_peek(&sock->sk->sk_receive_queue);
  853. msg = buf_msg(buf);
  854. sz = msg_data_sz(msg);
  855. err = msg_errcode(msg);
  856. /* Discard an empty non-errored message & try again */
  857. if ((!sz) && (!err)) {
  858. advance_queue(tsock);
  859. goto restart;
  860. }
  861. /* Optionally capture sender's address & ancillary data of first msg */
  862. if (sz_copied == 0) {
  863. set_orig_addr(m, msg);
  864. if ((res = anc_data_recv(m, msg, tsock->p)))
  865. goto exit;
  866. }
  867. /* Capture message data (if valid) & compute return value (always) */
  868. if (!err) {
  869. buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
  870. sz = skb_tail_pointer(buf) - buf_crs;
  871. needed = (buf_len - sz_copied);
  872. sz_to_copy = (sz <= needed) ? sz : needed;
  873. if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
  874. res = -EFAULT;
  875. goto exit;
  876. }
  877. sz_copied += sz_to_copy;
  878. if (sz_to_copy < sz) {
  879. if (!(flags & MSG_PEEK))
  880. TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
  881. goto exit;
  882. }
  883. crs += sz_to_copy;
  884. } else {
  885. if (sz_copied != 0)
  886. goto exit; /* can't add error msg to valid data */
  887. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  888. res = 0;
  889. else
  890. res = -ECONNRESET;
  891. }
  892. /* Consume received message (optional) */
  893. if (likely(!(flags & MSG_PEEK))) {
  894. if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  895. tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
  896. advance_queue(tsock);
  897. }
  898. /* Loop around if more data is required */
  899. if ((sz_copied < buf_len) /* didn't get all requested data */
  900. && (!skb_queue_empty(&sock->sk->sk_receive_queue) ||
  901. (flags & MSG_WAITALL))
  902. /* ... and more is ready or required */
  903. && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
  904. && (!err) /* ... and haven't reached a FIN */
  905. )
  906. goto restart;
  907. exit:
  908. mutex_unlock(&tsock->lock);
  909. return sz_copied ? sz_copied : res;
  910. }
  911. /**
  912. * queue_overloaded - test if queue overload condition exists
  913. * @queue_size: current size of queue
  914. * @base: nominal maximum size of queue
  915. * @msg: message to be added to queue
  916. *
  917. * Returns 1 if queue is currently overloaded, 0 otherwise
  918. */
  919. static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
  920. {
  921. u32 threshold;
  922. u32 imp = msg_importance(msg);
  923. if (imp == TIPC_LOW_IMPORTANCE)
  924. threshold = base;
  925. else if (imp == TIPC_MEDIUM_IMPORTANCE)
  926. threshold = base * 2;
  927. else if (imp == TIPC_HIGH_IMPORTANCE)
  928. threshold = base * 100;
  929. else
  930. return 0;
  931. if (msg_connected(msg))
  932. threshold *= 4;
  933. return (queue_size > threshold);
  934. }
  935. /**
  936. * async_disconnect - wrapper function used to disconnect port
  937. * @portref: TIPC port reference (passed as pointer-sized value)
  938. */
  939. static void async_disconnect(unsigned long portref)
  940. {
  941. tipc_disconnect((u32)portref);
  942. }
  943. /**
  944. * dispatch - handle arriving message
  945. * @tport: TIPC port that received message
  946. * @buf: message
  947. *
  948. * Called with port locked. Must not take socket lock to avoid deadlock risk.
  949. *
  950. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  951. */
  952. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  953. {
  954. struct tipc_msg *msg = buf_msg(buf);
  955. struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
  956. struct socket *sock;
  957. u32 recv_q_len;
  958. /* Reject message if socket is closing */
  959. if (!tsock)
  960. return TIPC_ERR_NO_PORT;
  961. /* Reject message if it is wrong sort of message for socket */
  962. /*
  963. * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
  964. * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
  965. * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
  966. */
  967. sock = tsock->sk.sk_socket;
  968. if (sock->state == SS_READY) {
  969. if (msg_connected(msg)) {
  970. msg_dbg(msg, "dispatch filter 1\n");
  971. return TIPC_ERR_NO_PORT;
  972. }
  973. } else {
  974. if (msg_mcast(msg)) {
  975. msg_dbg(msg, "dispatch filter 2\n");
  976. return TIPC_ERR_NO_PORT;
  977. }
  978. if (sock->state == SS_CONNECTED) {
  979. if (!msg_connected(msg)) {
  980. msg_dbg(msg, "dispatch filter 3\n");
  981. return TIPC_ERR_NO_PORT;
  982. }
  983. }
  984. else if (sock->state == SS_CONNECTING) {
  985. if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
  986. msg_dbg(msg, "dispatch filter 4\n");
  987. return TIPC_ERR_NO_PORT;
  988. }
  989. }
  990. else if (sock->state == SS_LISTENING) {
  991. if (msg_connected(msg) || msg_errcode(msg)) {
  992. msg_dbg(msg, "dispatch filter 5\n");
  993. return TIPC_ERR_NO_PORT;
  994. }
  995. }
  996. else if (sock->state == SS_DISCONNECTING) {
  997. msg_dbg(msg, "dispatch filter 6\n");
  998. return TIPC_ERR_NO_PORT;
  999. }
  1000. else /* (sock->state == SS_UNCONNECTED) */ {
  1001. if (msg_connected(msg) || msg_errcode(msg)) {
  1002. msg_dbg(msg, "dispatch filter 7\n");
  1003. return TIPC_ERR_NO_PORT;
  1004. }
  1005. }
  1006. }
  1007. /* Reject message if there isn't room to queue it */
  1008. if (unlikely((u32)atomic_read(&tipc_queue_size) >
  1009. OVERLOAD_LIMIT_BASE)) {
  1010. if (queue_overloaded(atomic_read(&tipc_queue_size),
  1011. OVERLOAD_LIMIT_BASE, msg))
  1012. return TIPC_ERR_OVERLOAD;
  1013. }
  1014. recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
  1015. if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
  1016. if (queue_overloaded(recv_q_len,
  1017. OVERLOAD_LIMIT_BASE / 2, msg))
  1018. return TIPC_ERR_OVERLOAD;
  1019. }
  1020. /* Initiate connection termination for an incoming 'FIN' */
  1021. if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
  1022. sock->state = SS_DISCONNECTING;
  1023. /* Note: Use signal since port lock is already taken! */
  1024. tipc_k_signal((Handler)async_disconnect, tport->ref);
  1025. }
  1026. /* Enqueue message (finally!) */
  1027. msg_dbg(msg,"<DISP<: ");
  1028. TIPC_SKB_CB(buf)->handle = msg_data(msg);
  1029. atomic_inc(&tipc_queue_size);
  1030. skb_queue_tail(&sock->sk->sk_receive_queue, buf);
  1031. if (waitqueue_active(sock->sk->sk_sleep))
  1032. wake_up_interruptible(sock->sk->sk_sleep);
  1033. return TIPC_OK;
  1034. }
  1035. /**
  1036. * wakeupdispatch - wake up port after congestion
  1037. * @tport: port to wakeup
  1038. *
  1039. * Called with port lock on.
  1040. */
  1041. static void wakeupdispatch(struct tipc_port *tport)
  1042. {
  1043. struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
  1044. if (waitqueue_active(tsock->sk.sk_sleep))
  1045. wake_up_interruptible(tsock->sk.sk_sleep);
  1046. }
  1047. /**
  1048. * connect - establish a connection to another TIPC port
  1049. * @sock: socket structure
  1050. * @dest: socket address for destination port
  1051. * @destlen: size of socket address data structure
  1052. * @flags: (unused)
  1053. *
  1054. * Returns 0 on success, errno otherwise
  1055. */
  1056. static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
  1057. int flags)
  1058. {
  1059. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1060. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1061. struct msghdr m = {NULL,};
  1062. struct sk_buff *buf;
  1063. struct tipc_msg *msg;
  1064. int res;
  1065. /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
  1066. if (sock->state == SS_READY)
  1067. return -EOPNOTSUPP;
  1068. /* Issue Posix-compliant error code if socket is in the wrong state */
  1069. if (sock->state == SS_LISTENING)
  1070. return -EOPNOTSUPP;
  1071. if (sock->state == SS_CONNECTING)
  1072. return -EALREADY;
  1073. if (sock->state != SS_UNCONNECTED)
  1074. return -EISCONN;
  1075. /*
  1076. * Reject connection attempt using multicast address
  1077. *
  1078. * Note: send_msg() validates the rest of the address fields,
  1079. * so there's no need to do it here
  1080. */
  1081. if (dst->addrtype == TIPC_ADDR_MCAST)
  1082. return -EINVAL;
  1083. /* Send a 'SYN-' to destination */
  1084. m.msg_name = dest;
  1085. m.msg_namelen = destlen;
  1086. if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
  1087. sock->state = SS_DISCONNECTING;
  1088. return res;
  1089. }
  1090. if (mutex_lock_interruptible(&tsock->lock))
  1091. return -ERESTARTSYS;
  1092. /* Wait for destination's 'ACK' response */
  1093. res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
  1094. skb_queue_len(&sock->sk->sk_receive_queue),
  1095. sock->sk->sk_rcvtimeo);
  1096. buf = skb_peek(&sock->sk->sk_receive_queue);
  1097. if (res > 0) {
  1098. msg = buf_msg(buf);
  1099. res = auto_connect(sock, tsock, msg);
  1100. if (!res) {
  1101. if (!msg_data_sz(msg))
  1102. advance_queue(tsock);
  1103. }
  1104. } else {
  1105. if (res == 0) {
  1106. res = -ETIMEDOUT;
  1107. } else
  1108. { /* leave "res" unchanged */ }
  1109. sock->state = SS_DISCONNECTING;
  1110. }
  1111. mutex_unlock(&tsock->lock);
  1112. return res;
  1113. }
  1114. /**
  1115. * listen - allow socket to listen for incoming connections
  1116. * @sock: socket structure
  1117. * @len: (unused)
  1118. *
  1119. * Returns 0 on success, errno otherwise
  1120. */
  1121. static int listen(struct socket *sock, int len)
  1122. {
  1123. /* REQUIRES SOCKET LOCKING OF SOME SORT? */
  1124. if (sock->state == SS_READY)
  1125. return -EOPNOTSUPP;
  1126. if (sock->state != SS_UNCONNECTED)
  1127. return -EINVAL;
  1128. sock->state = SS_LISTENING;
  1129. return 0;
  1130. }
  1131. /**
  1132. * accept - wait for connection request
  1133. * @sock: listening socket
  1134. * @newsock: new socket that is to be connected
  1135. * @flags: file-related flags associated with socket
  1136. *
  1137. * Returns 0 on success, errno otherwise
  1138. */
  1139. static int accept(struct socket *sock, struct socket *newsock, int flags)
  1140. {
  1141. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1142. struct sk_buff *buf;
  1143. int res = -EFAULT;
  1144. if (sock->state == SS_READY)
  1145. return -EOPNOTSUPP;
  1146. if (sock->state != SS_LISTENING)
  1147. return -EINVAL;
  1148. if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
  1149. (flags & O_NONBLOCK)))
  1150. return -EWOULDBLOCK;
  1151. if (mutex_lock_interruptible(&tsock->lock))
  1152. return -ERESTARTSYS;
  1153. if (wait_event_interruptible(*sock->sk->sk_sleep,
  1154. skb_queue_len(&sock->sk->sk_receive_queue))) {
  1155. res = -ERESTARTSYS;
  1156. goto exit;
  1157. }
  1158. buf = skb_peek(&sock->sk->sk_receive_queue);
  1159. res = tipc_create(sock_net(sock->sk), newsock, 0);
  1160. if (!res) {
  1161. struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
  1162. struct tipc_portid id;
  1163. struct tipc_msg *msg = buf_msg(buf);
  1164. u32 new_ref = new_tsock->p->ref;
  1165. id.ref = msg_origport(msg);
  1166. id.node = msg_orignode(msg);
  1167. tipc_connect2port(new_ref, &id);
  1168. newsock->state = SS_CONNECTED;
  1169. tipc_set_portimportance(new_ref, msg_importance(msg));
  1170. if (msg_named(msg)) {
  1171. new_tsock->p->conn_type = msg_nametype(msg);
  1172. new_tsock->p->conn_instance = msg_nameinst(msg);
  1173. }
  1174. /*
  1175. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1176. * Respond to 'SYN+' by queuing it on new socket.
  1177. */
  1178. msg_dbg(msg,"<ACC<: ");
  1179. if (!msg_data_sz(msg)) {
  1180. struct msghdr m = {NULL,};
  1181. send_packet(NULL, newsock, &m, 0);
  1182. advance_queue(tsock);
  1183. } else {
  1184. sock_lock(tsock);
  1185. skb_dequeue(&sock->sk->sk_receive_queue);
  1186. sock_unlock(tsock);
  1187. skb_queue_head(&newsock->sk->sk_receive_queue, buf);
  1188. }
  1189. }
  1190. exit:
  1191. mutex_unlock(&tsock->lock);
  1192. return res;
  1193. }
  1194. /**
  1195. * shutdown - shutdown socket connection
  1196. * @sock: socket structure
  1197. * @how: direction to close (must be SHUT_RDWR)
  1198. *
  1199. * Terminates connection (if necessary), then purges socket's receive queue.
  1200. *
  1201. * Returns 0 on success, errno otherwise
  1202. */
  1203. static int shutdown(struct socket *sock, int how)
  1204. {
  1205. struct tipc_sock* tsock = tipc_sk(sock->sk);
  1206. struct sk_buff *buf;
  1207. int res;
  1208. if (how != SHUT_RDWR)
  1209. return -EINVAL;
  1210. if (mutex_lock_interruptible(&tsock->lock))
  1211. return -ERESTARTSYS;
  1212. sock_lock(tsock);
  1213. switch (sock->state) {
  1214. case SS_CONNECTED:
  1215. /* Send 'FIN+' or 'FIN-' message to peer */
  1216. sock_unlock(tsock);
  1217. restart:
  1218. if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
  1219. atomic_dec(&tipc_queue_size);
  1220. if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
  1221. buf_discard(buf);
  1222. goto restart;
  1223. }
  1224. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1225. }
  1226. else {
  1227. tipc_shutdown(tsock->p->ref);
  1228. }
  1229. sock_lock(tsock);
  1230. /* fall through */
  1231. case SS_DISCONNECTING:
  1232. /* Discard any unreceived messages */
  1233. while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
  1234. atomic_dec(&tipc_queue_size);
  1235. buf_discard(buf);
  1236. }
  1237. tsock->p->conn_unacked = 0;
  1238. /* fall through */
  1239. case SS_CONNECTING:
  1240. sock->state = SS_DISCONNECTING;
  1241. res = 0;
  1242. break;
  1243. default:
  1244. res = -ENOTCONN;
  1245. }
  1246. sock_unlock(tsock);
  1247. mutex_unlock(&tsock->lock);
  1248. return res;
  1249. }
  1250. /**
  1251. * setsockopt - set socket option
  1252. * @sock: socket structure
  1253. * @lvl: option level
  1254. * @opt: option identifier
  1255. * @ov: pointer to new option value
  1256. * @ol: length of option value
  1257. *
  1258. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1259. * (to ease compatibility).
  1260. *
  1261. * Returns 0 on success, errno otherwise
  1262. */
  1263. static int setsockopt(struct socket *sock,
  1264. int lvl, int opt, char __user *ov, int ol)
  1265. {
  1266. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1267. u32 value;
  1268. int res;
  1269. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1270. return 0;
  1271. if (lvl != SOL_TIPC)
  1272. return -ENOPROTOOPT;
  1273. if (ol < sizeof(value))
  1274. return -EINVAL;
  1275. if ((res = get_user(value, (u32 __user *)ov)))
  1276. return res;
  1277. if (mutex_lock_interruptible(&tsock->lock))
  1278. return -ERESTARTSYS;
  1279. switch (opt) {
  1280. case TIPC_IMPORTANCE:
  1281. res = tipc_set_portimportance(tsock->p->ref, value);
  1282. break;
  1283. case TIPC_SRC_DROPPABLE:
  1284. if (sock->type != SOCK_STREAM)
  1285. res = tipc_set_portunreliable(tsock->p->ref, value);
  1286. else
  1287. res = -ENOPROTOOPT;
  1288. break;
  1289. case TIPC_DEST_DROPPABLE:
  1290. res = tipc_set_portunreturnable(tsock->p->ref, value);
  1291. break;
  1292. case TIPC_CONN_TIMEOUT:
  1293. sock->sk->sk_rcvtimeo = msecs_to_jiffies(value);
  1294. break;
  1295. default:
  1296. res = -EINVAL;
  1297. }
  1298. mutex_unlock(&tsock->lock);
  1299. return res;
  1300. }
  1301. /**
  1302. * getsockopt - get socket option
  1303. * @sock: socket structure
  1304. * @lvl: option level
  1305. * @opt: option identifier
  1306. * @ov: receptacle for option value
  1307. * @ol: receptacle for length of option value
  1308. *
  1309. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1310. * (to ease compatibility).
  1311. *
  1312. * Returns 0 on success, errno otherwise
  1313. */
  1314. static int getsockopt(struct socket *sock,
  1315. int lvl, int opt, char __user *ov, int __user *ol)
  1316. {
  1317. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1318. int len;
  1319. u32 value;
  1320. int res;
  1321. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1322. return put_user(0, ol);
  1323. if (lvl != SOL_TIPC)
  1324. return -ENOPROTOOPT;
  1325. if ((res = get_user(len, ol)))
  1326. return res;
  1327. if (mutex_lock_interruptible(&tsock->lock))
  1328. return -ERESTARTSYS;
  1329. switch (opt) {
  1330. case TIPC_IMPORTANCE:
  1331. res = tipc_portimportance(tsock->p->ref, &value);
  1332. break;
  1333. case TIPC_SRC_DROPPABLE:
  1334. res = tipc_portunreliable(tsock->p->ref, &value);
  1335. break;
  1336. case TIPC_DEST_DROPPABLE:
  1337. res = tipc_portunreturnable(tsock->p->ref, &value);
  1338. break;
  1339. case TIPC_CONN_TIMEOUT:
  1340. value = jiffies_to_msecs(sock->sk->sk_rcvtimeo);
  1341. break;
  1342. default:
  1343. res = -EINVAL;
  1344. }
  1345. if (res) {
  1346. /* "get" failed */
  1347. }
  1348. else if (len < sizeof(value)) {
  1349. res = -EINVAL;
  1350. }
  1351. else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
  1352. /* couldn't return value */
  1353. }
  1354. else {
  1355. res = put_user(sizeof(value), ol);
  1356. }
  1357. mutex_unlock(&tsock->lock);
  1358. return res;
  1359. }
  1360. /**
  1361. * Protocol switches for the various types of TIPC sockets
  1362. */
  1363. static const struct proto_ops msg_ops = {
  1364. .owner = THIS_MODULE,
  1365. .family = AF_TIPC,
  1366. .release = release,
  1367. .bind = bind,
  1368. .connect = connect,
  1369. .socketpair = sock_no_socketpair,
  1370. .accept = accept,
  1371. .getname = get_name,
  1372. .poll = poll,
  1373. .ioctl = sock_no_ioctl,
  1374. .listen = listen,
  1375. .shutdown = shutdown,
  1376. .setsockopt = setsockopt,
  1377. .getsockopt = getsockopt,
  1378. .sendmsg = send_msg,
  1379. .recvmsg = recv_msg,
  1380. .mmap = sock_no_mmap,
  1381. .sendpage = sock_no_sendpage
  1382. };
  1383. static const struct proto_ops packet_ops = {
  1384. .owner = THIS_MODULE,
  1385. .family = AF_TIPC,
  1386. .release = release,
  1387. .bind = bind,
  1388. .connect = connect,
  1389. .socketpair = sock_no_socketpair,
  1390. .accept = accept,
  1391. .getname = get_name,
  1392. .poll = poll,
  1393. .ioctl = sock_no_ioctl,
  1394. .listen = listen,
  1395. .shutdown = shutdown,
  1396. .setsockopt = setsockopt,
  1397. .getsockopt = getsockopt,
  1398. .sendmsg = send_packet,
  1399. .recvmsg = recv_msg,
  1400. .mmap = sock_no_mmap,
  1401. .sendpage = sock_no_sendpage
  1402. };
  1403. static const struct proto_ops stream_ops = {
  1404. .owner = THIS_MODULE,
  1405. .family = AF_TIPC,
  1406. .release = release,
  1407. .bind = bind,
  1408. .connect = connect,
  1409. .socketpair = sock_no_socketpair,
  1410. .accept = accept,
  1411. .getname = get_name,
  1412. .poll = poll,
  1413. .ioctl = sock_no_ioctl,
  1414. .listen = listen,
  1415. .shutdown = shutdown,
  1416. .setsockopt = setsockopt,
  1417. .getsockopt = getsockopt,
  1418. .sendmsg = send_stream,
  1419. .recvmsg = recv_stream,
  1420. .mmap = sock_no_mmap,
  1421. .sendpage = sock_no_sendpage
  1422. };
  1423. static const struct net_proto_family tipc_family_ops = {
  1424. .owner = THIS_MODULE,
  1425. .family = AF_TIPC,
  1426. .create = tipc_create
  1427. };
  1428. static struct proto tipc_proto = {
  1429. .name = "TIPC",
  1430. .owner = THIS_MODULE,
  1431. .obj_size = sizeof(struct tipc_sock)
  1432. };
  1433. /**
  1434. * tipc_socket_init - initialize TIPC socket interface
  1435. *
  1436. * Returns 0 on success, errno otherwise
  1437. */
  1438. int tipc_socket_init(void)
  1439. {
  1440. int res;
  1441. res = proto_register(&tipc_proto, 1);
  1442. if (res) {
  1443. err("Failed to register TIPC protocol type\n");
  1444. goto out;
  1445. }
  1446. res = sock_register(&tipc_family_ops);
  1447. if (res) {
  1448. err("Failed to register TIPC socket type\n");
  1449. proto_unregister(&tipc_proto);
  1450. goto out;
  1451. }
  1452. sockets_enabled = 1;
  1453. out:
  1454. return res;
  1455. }
  1456. /**
  1457. * tipc_socket_stop - stop TIPC socket interface
  1458. */
  1459. void tipc_socket_stop(void)
  1460. {
  1461. if (!sockets_enabled)
  1462. return;
  1463. sockets_enabled = 0;
  1464. sock_unregister(tipc_family_ops.family);
  1465. proto_unregister(&tipc_proto);
  1466. }