socket.c 43 KB

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