socket.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  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(dest->family != AF_TIPC))
  388. return -EINVAL;
  389. needs_conn = (sock->state != SS_READY);
  390. if (unlikely(needs_conn)) {
  391. if (sock->state == SS_LISTENING)
  392. return -EPIPE;
  393. if (sock->state != SS_UNCONNECTED)
  394. return -EISCONN;
  395. if ((tsock->p->published) ||
  396. ((sock->type == SOCK_STREAM) && (total_len != 0)))
  397. return -EOPNOTSUPP;
  398. if (dest->addrtype == TIPC_ADDR_NAME) {
  399. tsock->p->conn_type = dest->addr.name.name.type;
  400. tsock->p->conn_instance = dest->addr.name.name.instance;
  401. }
  402. }
  403. if (down_interruptible(&tsock->sem))
  404. return -ERESTARTSYS;
  405. if (needs_conn) {
  406. /* Abort any pending connection attempts (very unlikely) */
  407. while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
  408. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  409. atomic_dec(&tipc_queue_size);
  410. }
  411. sock->state = SS_CONNECTING;
  412. }
  413. do {
  414. if (dest->addrtype == TIPC_ADDR_NAME) {
  415. if ((res = dest_name_check(dest, m)))
  416. goto exit;
  417. res = tipc_send2name(tsock->p->ref,
  418. &dest->addr.name.name,
  419. dest->addr.name.domain,
  420. m->msg_iovlen,
  421. m->msg_iov);
  422. }
  423. else if (dest->addrtype == TIPC_ADDR_ID) {
  424. res = tipc_send2port(tsock->p->ref,
  425. &dest->addr.id,
  426. m->msg_iovlen,
  427. m->msg_iov);
  428. }
  429. else if (dest->addrtype == TIPC_ADDR_MCAST) {
  430. if (needs_conn) {
  431. res = -EOPNOTSUPP;
  432. goto exit;
  433. }
  434. if ((res = dest_name_check(dest, m)))
  435. goto exit;
  436. res = tipc_multicast(tsock->p->ref,
  437. &dest->addr.nameseq,
  438. 0,
  439. m->msg_iovlen,
  440. m->msg_iov);
  441. }
  442. if (likely(res != -ELINKCONG)) {
  443. exit:
  444. up(&tsock->sem);
  445. return res;
  446. }
  447. if (m->msg_flags & MSG_DONTWAIT) {
  448. res = -EWOULDBLOCK;
  449. goto exit;
  450. }
  451. if (wait_event_interruptible(*sock->sk->sk_sleep,
  452. !tsock->p->congested)) {
  453. res = -ERESTARTSYS;
  454. goto exit;
  455. }
  456. } while (1);
  457. }
  458. /**
  459. * send_packet - send a connection-oriented message
  460. * @iocb: (unused)
  461. * @sock: socket structure
  462. * @m: message to send
  463. * @total_len: length of message
  464. *
  465. * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
  466. *
  467. * Returns the number of bytes sent on success, or errno otherwise
  468. */
  469. static int send_packet(struct kiocb *iocb, struct socket *sock,
  470. struct msghdr *m, size_t total_len)
  471. {
  472. struct tipc_sock *tsock = tipc_sk(sock->sk);
  473. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  474. int res;
  475. /* Handle implied connection establishment */
  476. if (unlikely(dest))
  477. return send_msg(iocb, sock, m, total_len);
  478. if (down_interruptible(&tsock->sem)) {
  479. return -ERESTARTSYS;
  480. }
  481. do {
  482. if (unlikely(sock->state != SS_CONNECTED)) {
  483. if (sock->state == SS_DISCONNECTING)
  484. res = -EPIPE;
  485. else
  486. res = -ENOTCONN;
  487. goto exit;
  488. }
  489. res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
  490. if (likely(res != -ELINKCONG)) {
  491. exit:
  492. up(&tsock->sem);
  493. return res;
  494. }
  495. if (m->msg_flags & MSG_DONTWAIT) {
  496. res = -EWOULDBLOCK;
  497. goto exit;
  498. }
  499. if (wait_event_interruptible(*sock->sk->sk_sleep,
  500. !tsock->p->congested)) {
  501. res = -ERESTARTSYS;
  502. goto exit;
  503. }
  504. } while (1);
  505. }
  506. /**
  507. * send_stream - send stream-oriented data
  508. * @iocb: (unused)
  509. * @sock: socket structure
  510. * @m: data to send
  511. * @total_len: total length of data to be sent
  512. *
  513. * Used for SOCK_STREAM data.
  514. *
  515. * Returns the number of bytes sent on success (or partial success),
  516. * or errno if no data sent
  517. */
  518. static int send_stream(struct kiocb *iocb, struct socket *sock,
  519. struct msghdr *m, size_t total_len)
  520. {
  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. int curr_left;
  527. int bytes_to_send;
  528. int bytes_sent;
  529. int res;
  530. if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE))
  531. return send_packet(iocb, sock, m, total_len);
  532. /* Can only send large data streams if already connected */
  533. if (unlikely(sock->state != SS_CONNECTED)) {
  534. if (sock->state == SS_DISCONNECTING)
  535. return -EPIPE;
  536. else
  537. return -ENOTCONN;
  538. }
  539. /*
  540. * Send each iovec entry using one or more messages
  541. *
  542. * Note: This algorithm is good for the most likely case
  543. * (i.e. one large iovec entry), but could be improved to pass sets
  544. * of small iovec entries into send_packet().
  545. */
  546. curr_iov = m->msg_iov;
  547. curr_iovlen = m->msg_iovlen;
  548. my_msg.msg_iov = &my_iov;
  549. my_msg.msg_iovlen = 1;
  550. bytes_sent = 0;
  551. while (curr_iovlen--) {
  552. curr_start = curr_iov->iov_base;
  553. curr_left = curr_iov->iov_len;
  554. while (curr_left) {
  555. bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE)
  556. ? curr_left : TIPC_MAX_USER_MSG_SIZE;
  557. my_iov.iov_base = curr_start;
  558. my_iov.iov_len = bytes_to_send;
  559. if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) {
  560. return bytes_sent ? bytes_sent : res;
  561. }
  562. curr_left -= bytes_to_send;
  563. curr_start += bytes_to_send;
  564. bytes_sent += bytes_to_send;
  565. }
  566. curr_iov++;
  567. }
  568. return bytes_sent;
  569. }
  570. /**
  571. * auto_connect - complete connection setup to a remote port
  572. * @sock: socket structure
  573. * @tsock: TIPC-specific socket structure
  574. * @msg: peer's response message
  575. *
  576. * Returns 0 on success, errno otherwise
  577. */
  578. static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
  579. struct tipc_msg *msg)
  580. {
  581. struct tipc_portid peer;
  582. if (msg_errcode(msg)) {
  583. sock->state = SS_DISCONNECTING;
  584. return -ECONNREFUSED;
  585. }
  586. peer.ref = msg_origport(msg);
  587. peer.node = msg_orignode(msg);
  588. tipc_connect2port(tsock->p->ref, &peer);
  589. tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
  590. sock->state = SS_CONNECTED;
  591. return 0;
  592. }
  593. /**
  594. * set_orig_addr - capture sender's address for received message
  595. * @m: descriptor for message info
  596. * @msg: received message header
  597. *
  598. * Note: Address is not captured if not requested by receiver.
  599. */
  600. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  601. {
  602. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
  603. if (addr) {
  604. addr->family = AF_TIPC;
  605. addr->addrtype = TIPC_ADDR_ID;
  606. addr->addr.id.ref = msg_origport(msg);
  607. addr->addr.id.node = msg_orignode(msg);
  608. addr->addr.name.domain = 0; /* could leave uninitialized */
  609. addr->scope = 0; /* could leave uninitialized */
  610. m->msg_namelen = sizeof(struct sockaddr_tipc);
  611. }
  612. }
  613. /**
  614. * anc_data_recv - optionally capture ancillary data for received message
  615. * @m: descriptor for message info
  616. * @msg: received message header
  617. * @tport: TIPC port associated with message
  618. *
  619. * Note: Ancillary data is not captured if not requested by receiver.
  620. *
  621. * Returns 0 if successful, otherwise errno
  622. */
  623. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  624. struct tipc_port *tport)
  625. {
  626. u32 anc_data[3];
  627. u32 err;
  628. u32 dest_type;
  629. int has_name;
  630. int res;
  631. if (likely(m->msg_controllen == 0))
  632. return 0;
  633. /* Optionally capture errored message object(s) */
  634. err = msg ? msg_errcode(msg) : 0;
  635. if (unlikely(err)) {
  636. anc_data[0] = err;
  637. anc_data[1] = msg_data_sz(msg);
  638. if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
  639. return res;
  640. if (anc_data[1] &&
  641. (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  642. msg_data(msg))))
  643. return res;
  644. }
  645. /* Optionally capture message destination object */
  646. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  647. switch (dest_type) {
  648. case TIPC_NAMED_MSG:
  649. has_name = 1;
  650. anc_data[0] = msg_nametype(msg);
  651. anc_data[1] = msg_namelower(msg);
  652. anc_data[2] = msg_namelower(msg);
  653. break;
  654. case TIPC_MCAST_MSG:
  655. has_name = 1;
  656. anc_data[0] = msg_nametype(msg);
  657. anc_data[1] = msg_namelower(msg);
  658. anc_data[2] = msg_nameupper(msg);
  659. break;
  660. case TIPC_CONN_MSG:
  661. has_name = (tport->conn_type != 0);
  662. anc_data[0] = tport->conn_type;
  663. anc_data[1] = tport->conn_instance;
  664. anc_data[2] = tport->conn_instance;
  665. break;
  666. default:
  667. has_name = 0;
  668. }
  669. if (has_name &&
  670. (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
  671. return res;
  672. return 0;
  673. }
  674. /**
  675. * recv_msg - receive packet-oriented message
  676. * @iocb: (unused)
  677. * @m: descriptor for message info
  678. * @buf_len: total size of user buffer area
  679. * @flags: receive flags
  680. *
  681. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  682. * If the complete message doesn't fit in user area, truncate it.
  683. *
  684. * Returns size of returned message data, errno otherwise
  685. */
  686. static int recv_msg(struct kiocb *iocb, struct socket *sock,
  687. struct msghdr *m, size_t buf_len, int flags)
  688. {
  689. struct tipc_sock *tsock = tipc_sk(sock->sk);
  690. struct sk_buff *buf;
  691. struct tipc_msg *msg;
  692. unsigned int q_len;
  693. unsigned int sz;
  694. u32 err;
  695. int res;
  696. /* Currently doesn't support receiving into multiple iovec entries */
  697. if (m->msg_iovlen != 1)
  698. return -EOPNOTSUPP;
  699. /* Catch invalid receive attempts */
  700. if (unlikely(!buf_len))
  701. return -EINVAL;
  702. if (sock->type == SOCK_SEQPACKET) {
  703. if (unlikely(sock->state == SS_UNCONNECTED))
  704. return -ENOTCONN;
  705. if (unlikely((sock->state == SS_DISCONNECTING) &&
  706. (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
  707. return -ENOTCONN;
  708. }
  709. /* Look for a message in receive queue; wait if necessary */
  710. if (unlikely(down_interruptible(&tsock->sem)))
  711. return -ERESTARTSYS;
  712. restart:
  713. if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
  714. (flags & MSG_DONTWAIT))) {
  715. res = -EWOULDBLOCK;
  716. goto exit;
  717. }
  718. if ((res = wait_event_interruptible(
  719. *sock->sk->sk_sleep,
  720. ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
  721. (sock->state == SS_DISCONNECTING))) )) {
  722. goto exit;
  723. }
  724. /* Catch attempt to receive on an already terminated connection */
  725. /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
  726. if (!q_len) {
  727. res = -ENOTCONN;
  728. goto exit;
  729. }
  730. /* Get access to first message in receive queue */
  731. buf = skb_peek(&sock->sk->sk_receive_queue);
  732. msg = buf_msg(buf);
  733. sz = msg_data_sz(msg);
  734. err = msg_errcode(msg);
  735. /* Complete connection setup for an implied connect */
  736. if (unlikely(sock->state == SS_CONNECTING)) {
  737. if ((res = auto_connect(sock, tsock, msg)))
  738. goto exit;
  739. }
  740. /* Discard an empty non-errored message & try again */
  741. if ((!sz) && (!err)) {
  742. advance_queue(tsock);
  743. goto restart;
  744. }
  745. /* Capture sender's address (optional) */
  746. set_orig_addr(m, msg);
  747. /* Capture ancillary data (optional) */
  748. if ((res = anc_data_recv(m, msg, tsock->p)))
  749. goto exit;
  750. /* Capture message data (if valid) & compute return value (always) */
  751. if (!err) {
  752. if (unlikely(buf_len < sz)) {
  753. sz = buf_len;
  754. m->msg_flags |= MSG_TRUNC;
  755. }
  756. if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
  757. sz))) {
  758. res = -EFAULT;
  759. goto exit;
  760. }
  761. res = sz;
  762. } else {
  763. if ((sock->state == SS_READY) ||
  764. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  765. res = 0;
  766. else
  767. res = -ECONNRESET;
  768. }
  769. /* Consume received message (optional) */
  770. if (likely(!(flags & MSG_PEEK))) {
  771. if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  772. tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
  773. advance_queue(tsock);
  774. }
  775. exit:
  776. up(&tsock->sem);
  777. return res;
  778. }
  779. /**
  780. * recv_stream - receive stream-oriented data
  781. * @iocb: (unused)
  782. * @m: descriptor for message info
  783. * @buf_len: total size of user buffer area
  784. * @flags: receive flags
  785. *
  786. * Used for SOCK_STREAM messages only. If not enough data is available
  787. * will optionally wait for more; never truncates data.
  788. *
  789. * Returns size of returned message data, errno otherwise
  790. */
  791. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  792. struct msghdr *m, size_t buf_len, int flags)
  793. {
  794. struct tipc_sock *tsock = tipc_sk(sock->sk);
  795. struct sk_buff *buf;
  796. struct tipc_msg *msg;
  797. unsigned int q_len;
  798. unsigned int sz;
  799. int sz_to_copy;
  800. int sz_copied = 0;
  801. int needed;
  802. char *crs = m->msg_iov->iov_base;
  803. unsigned char *buf_crs;
  804. u32 err;
  805. int res;
  806. /* Currently doesn't support receiving into multiple iovec entries */
  807. if (m->msg_iovlen != 1)
  808. return -EOPNOTSUPP;
  809. /* Catch invalid receive attempts */
  810. if (unlikely(!buf_len))
  811. return -EINVAL;
  812. if (unlikely(sock->state == SS_DISCONNECTING)) {
  813. if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
  814. return -ENOTCONN;
  815. } else if (unlikely(sock->state != SS_CONNECTED))
  816. return -ENOTCONN;
  817. /* Look for a message in receive queue; wait if necessary */
  818. if (unlikely(down_interruptible(&tsock->sem)))
  819. return -ERESTARTSYS;
  820. restart:
  821. if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
  822. (flags & MSG_DONTWAIT))) {
  823. res = -EWOULDBLOCK;
  824. goto exit;
  825. }
  826. if ((res = wait_event_interruptible(
  827. *sock->sk->sk_sleep,
  828. ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
  829. (sock->state == SS_DISCONNECTING))) )) {
  830. goto exit;
  831. }
  832. /* Catch attempt to receive on an already terminated connection */
  833. /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
  834. if (!q_len) {
  835. res = -ENOTCONN;
  836. goto exit;
  837. }
  838. /* Get access to first message in receive queue */
  839. buf = skb_peek(&sock->sk->sk_receive_queue);
  840. msg = buf_msg(buf);
  841. sz = msg_data_sz(msg);
  842. err = msg_errcode(msg);
  843. /* Discard an empty non-errored message & try again */
  844. if ((!sz) && (!err)) {
  845. advance_queue(tsock);
  846. goto restart;
  847. }
  848. /* Optionally capture sender's address & ancillary data of first msg */
  849. if (sz_copied == 0) {
  850. set_orig_addr(m, msg);
  851. if ((res = anc_data_recv(m, msg, tsock->p)))
  852. goto exit;
  853. }
  854. /* Capture message data (if valid) & compute return value (always) */
  855. if (!err) {
  856. buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
  857. sz = buf->tail - buf_crs;
  858. needed = (buf_len - sz_copied);
  859. sz_to_copy = (sz <= needed) ? sz : needed;
  860. if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
  861. res = -EFAULT;
  862. goto exit;
  863. }
  864. sz_copied += sz_to_copy;
  865. if (sz_to_copy < sz) {
  866. if (!(flags & MSG_PEEK))
  867. TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
  868. goto exit;
  869. }
  870. crs += sz_to_copy;
  871. } else {
  872. if (sz_copied != 0)
  873. goto exit; /* can't add error msg to valid data */
  874. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  875. res = 0;
  876. else
  877. res = -ECONNRESET;
  878. }
  879. /* Consume received message (optional) */
  880. if (likely(!(flags & MSG_PEEK))) {
  881. if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  882. tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
  883. advance_queue(tsock);
  884. }
  885. /* Loop around if more data is required */
  886. if ((sz_copied < buf_len) /* didn't get all requested data */
  887. && (flags & MSG_WAITALL) /* ... and need to wait for more */
  888. && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
  889. && (!err) /* ... and haven't reached a FIN */
  890. )
  891. goto restart;
  892. exit:
  893. up(&tsock->sem);
  894. return sz_copied ? sz_copied : res;
  895. }
  896. /**
  897. * queue_overloaded - test if queue overload condition exists
  898. * @queue_size: current size of queue
  899. * @base: nominal maximum size of queue
  900. * @msg: message to be added to queue
  901. *
  902. * Returns 1 if queue is currently overloaded, 0 otherwise
  903. */
  904. static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
  905. {
  906. u32 threshold;
  907. u32 imp = msg_importance(msg);
  908. if (imp == TIPC_LOW_IMPORTANCE)
  909. threshold = base;
  910. else if (imp == TIPC_MEDIUM_IMPORTANCE)
  911. threshold = base * 2;
  912. else if (imp == TIPC_HIGH_IMPORTANCE)
  913. threshold = base * 100;
  914. else
  915. return 0;
  916. if (msg_connected(msg))
  917. threshold *= 4;
  918. return (queue_size > threshold);
  919. }
  920. /**
  921. * async_disconnect - wrapper function used to disconnect port
  922. * @portref: TIPC port reference (passed as pointer-sized value)
  923. */
  924. static void async_disconnect(unsigned long portref)
  925. {
  926. tipc_disconnect((u32)portref);
  927. }
  928. /**
  929. * dispatch - handle arriving message
  930. * @tport: TIPC port that received message
  931. * @buf: message
  932. *
  933. * Called with port locked. Must not take socket lock to avoid deadlock risk.
  934. *
  935. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  936. */
  937. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  938. {
  939. struct tipc_msg *msg = buf_msg(buf);
  940. struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
  941. struct socket *sock;
  942. u32 recv_q_len;
  943. /* Reject message if socket is closing */
  944. if (!tsock)
  945. return TIPC_ERR_NO_PORT;
  946. /* Reject message if it is wrong sort of message for socket */
  947. /*
  948. * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
  949. * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
  950. * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
  951. */
  952. sock = tsock->sk.sk_socket;
  953. if (sock->state == SS_READY) {
  954. if (msg_connected(msg)) {
  955. msg_dbg(msg, "dispatch filter 1\n");
  956. return TIPC_ERR_NO_PORT;
  957. }
  958. } else {
  959. if (msg_mcast(msg)) {
  960. msg_dbg(msg, "dispatch filter 2\n");
  961. return TIPC_ERR_NO_PORT;
  962. }
  963. if (sock->state == SS_CONNECTED) {
  964. if (!msg_connected(msg)) {
  965. msg_dbg(msg, "dispatch filter 3\n");
  966. return TIPC_ERR_NO_PORT;
  967. }
  968. }
  969. else if (sock->state == SS_CONNECTING) {
  970. if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
  971. msg_dbg(msg, "dispatch filter 4\n");
  972. return TIPC_ERR_NO_PORT;
  973. }
  974. }
  975. else if (sock->state == SS_LISTENING) {
  976. if (msg_connected(msg) || msg_errcode(msg)) {
  977. msg_dbg(msg, "dispatch filter 5\n");
  978. return TIPC_ERR_NO_PORT;
  979. }
  980. }
  981. else if (sock->state == SS_DISCONNECTING) {
  982. msg_dbg(msg, "dispatch filter 6\n");
  983. return TIPC_ERR_NO_PORT;
  984. }
  985. else /* (sock->state == SS_UNCONNECTED) */ {
  986. if (msg_connected(msg) || msg_errcode(msg)) {
  987. msg_dbg(msg, "dispatch filter 7\n");
  988. return TIPC_ERR_NO_PORT;
  989. }
  990. }
  991. }
  992. /* Reject message if there isn't room to queue it */
  993. if (unlikely((u32)atomic_read(&tipc_queue_size) >
  994. OVERLOAD_LIMIT_BASE)) {
  995. if (queue_overloaded(atomic_read(&tipc_queue_size),
  996. OVERLOAD_LIMIT_BASE, msg))
  997. return TIPC_ERR_OVERLOAD;
  998. }
  999. recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
  1000. if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
  1001. if (queue_overloaded(recv_q_len,
  1002. OVERLOAD_LIMIT_BASE / 2, msg))
  1003. return TIPC_ERR_OVERLOAD;
  1004. }
  1005. /* Initiate connection termination for an incoming 'FIN' */
  1006. if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
  1007. sock->state = SS_DISCONNECTING;
  1008. /* Note: Use signal since port lock is already taken! */
  1009. tipc_k_signal((Handler)async_disconnect, tport->ref);
  1010. }
  1011. /* Enqueue message (finally!) */
  1012. msg_dbg(msg,"<DISP<: ");
  1013. TIPC_SKB_CB(buf)->handle = msg_data(msg);
  1014. atomic_inc(&tipc_queue_size);
  1015. skb_queue_tail(&sock->sk->sk_receive_queue, buf);
  1016. wake_up_interruptible(sock->sk->sk_sleep);
  1017. return TIPC_OK;
  1018. }
  1019. /**
  1020. * wakeupdispatch - wake up port after congestion
  1021. * @tport: port to wakeup
  1022. *
  1023. * Called with port lock on.
  1024. */
  1025. static void wakeupdispatch(struct tipc_port *tport)
  1026. {
  1027. struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
  1028. wake_up_interruptible(tsock->sk.sk_sleep);
  1029. }
  1030. /**
  1031. * connect - establish a connection to another TIPC port
  1032. * @sock: socket structure
  1033. * @dest: socket address for destination port
  1034. * @destlen: size of socket address data structure
  1035. * @flags: (unused)
  1036. *
  1037. * Returns 0 on success, errno otherwise
  1038. */
  1039. static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
  1040. int flags)
  1041. {
  1042. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1043. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1044. struct msghdr m = {NULL,};
  1045. struct sk_buff *buf;
  1046. struct tipc_msg *msg;
  1047. int res;
  1048. /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
  1049. if (sock->state == SS_READY)
  1050. return -EOPNOTSUPP;
  1051. /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
  1052. if (sock->state == SS_LISTENING)
  1053. return -EOPNOTSUPP;
  1054. if (sock->state == SS_CONNECTING)
  1055. return -EALREADY;
  1056. if (sock->state != SS_UNCONNECTED)
  1057. return -EISCONN;
  1058. if ((destlen < sizeof(*dst)) || (dst->family != AF_TIPC) ||
  1059. ((dst->addrtype != TIPC_ADDR_NAME) && (dst->addrtype != TIPC_ADDR_ID)))
  1060. return -EINVAL;
  1061. /* Send a 'SYN-' to destination */
  1062. m.msg_name = dest;
  1063. if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
  1064. sock->state = SS_DISCONNECTING;
  1065. return res;
  1066. }
  1067. if (down_interruptible(&tsock->sem))
  1068. return -ERESTARTSYS;
  1069. /* Wait for destination's 'ACK' response */
  1070. res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
  1071. skb_queue_len(&sock->sk->sk_receive_queue),
  1072. sock->sk->sk_rcvtimeo);
  1073. buf = skb_peek(&sock->sk->sk_receive_queue);
  1074. if (res > 0) {
  1075. msg = buf_msg(buf);
  1076. res = auto_connect(sock, tsock, msg);
  1077. if (!res) {
  1078. if (!msg_data_sz(msg))
  1079. advance_queue(tsock);
  1080. }
  1081. } else {
  1082. if (res == 0) {
  1083. res = -ETIMEDOUT;
  1084. } else
  1085. { /* leave "res" unchanged */ }
  1086. sock->state = SS_DISCONNECTING;
  1087. }
  1088. up(&tsock->sem);
  1089. return res;
  1090. }
  1091. /**
  1092. * listen - allow socket to listen for incoming connections
  1093. * @sock: socket structure
  1094. * @len: (unused)
  1095. *
  1096. * Returns 0 on success, errno otherwise
  1097. */
  1098. static int listen(struct socket *sock, int len)
  1099. {
  1100. /* REQUIRES SOCKET LOCKING OF SOME SORT? */
  1101. if (sock->state == SS_READY)
  1102. return -EOPNOTSUPP;
  1103. if (sock->state != SS_UNCONNECTED)
  1104. return -EINVAL;
  1105. sock->state = SS_LISTENING;
  1106. return 0;
  1107. }
  1108. /**
  1109. * accept - wait for connection request
  1110. * @sock: listening socket
  1111. * @newsock: new socket that is to be connected
  1112. * @flags: file-related flags associated with socket
  1113. *
  1114. * Returns 0 on success, errno otherwise
  1115. */
  1116. static int accept(struct socket *sock, struct socket *newsock, int flags)
  1117. {
  1118. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1119. struct sk_buff *buf;
  1120. int res = -EFAULT;
  1121. if (sock->state == SS_READY)
  1122. return -EOPNOTSUPP;
  1123. if (sock->state != SS_LISTENING)
  1124. return -EINVAL;
  1125. if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
  1126. (flags & O_NONBLOCK)))
  1127. return -EWOULDBLOCK;
  1128. if (down_interruptible(&tsock->sem))
  1129. return -ERESTARTSYS;
  1130. if (wait_event_interruptible(*sock->sk->sk_sleep,
  1131. skb_queue_len(&sock->sk->sk_receive_queue))) {
  1132. res = -ERESTARTSYS;
  1133. goto exit;
  1134. }
  1135. buf = skb_peek(&sock->sk->sk_receive_queue);
  1136. res = tipc_create(newsock, 0);
  1137. if (!res) {
  1138. struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
  1139. struct tipc_portid id;
  1140. struct tipc_msg *msg = buf_msg(buf);
  1141. u32 new_ref = new_tsock->p->ref;
  1142. id.ref = msg_origport(msg);
  1143. id.node = msg_orignode(msg);
  1144. tipc_connect2port(new_ref, &id);
  1145. newsock->state = SS_CONNECTED;
  1146. tipc_set_portimportance(new_ref, msg_importance(msg));
  1147. if (msg_named(msg)) {
  1148. new_tsock->p->conn_type = msg_nametype(msg);
  1149. new_tsock->p->conn_instance = msg_nameinst(msg);
  1150. }
  1151. /*
  1152. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1153. * Respond to 'SYN+' by queuing it on new socket.
  1154. */
  1155. msg_dbg(msg,"<ACC<: ");
  1156. if (!msg_data_sz(msg)) {
  1157. struct msghdr m = {NULL,};
  1158. send_packet(NULL, newsock, &m, 0);
  1159. advance_queue(tsock);
  1160. } else {
  1161. sock_lock(tsock);
  1162. skb_dequeue(&sock->sk->sk_receive_queue);
  1163. sock_unlock(tsock);
  1164. skb_queue_head(&newsock->sk->sk_receive_queue, buf);
  1165. }
  1166. }
  1167. exit:
  1168. up(&tsock->sem);
  1169. return res;
  1170. }
  1171. /**
  1172. * shutdown - shutdown socket connection
  1173. * @sock: socket structure
  1174. * @how: direction to close (unused; always treated as read + write)
  1175. *
  1176. * Terminates connection (if necessary), then purges socket's receive queue.
  1177. *
  1178. * Returns 0 on success, errno otherwise
  1179. */
  1180. static int shutdown(struct socket *sock, int how)
  1181. {
  1182. struct tipc_sock* tsock = tipc_sk(sock->sk);
  1183. struct sk_buff *buf;
  1184. int res;
  1185. /* Could return -EINVAL for an invalid "how", but why bother? */
  1186. if (down_interruptible(&tsock->sem))
  1187. return -ERESTARTSYS;
  1188. sock_lock(tsock);
  1189. switch (sock->state) {
  1190. case SS_CONNECTED:
  1191. /* Send 'FIN+' or 'FIN-' message to peer */
  1192. sock_unlock(tsock);
  1193. restart:
  1194. if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
  1195. atomic_dec(&tipc_queue_size);
  1196. if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
  1197. buf_discard(buf);
  1198. goto restart;
  1199. }
  1200. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1201. }
  1202. else {
  1203. tipc_shutdown(tsock->p->ref);
  1204. }
  1205. sock_lock(tsock);
  1206. /* fall through */
  1207. case SS_DISCONNECTING:
  1208. /* Discard any unreceived messages */
  1209. while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
  1210. atomic_dec(&tipc_queue_size);
  1211. buf_discard(buf);
  1212. }
  1213. tsock->p->conn_unacked = 0;
  1214. /* fall through */
  1215. case SS_CONNECTING:
  1216. sock->state = SS_DISCONNECTING;
  1217. res = 0;
  1218. break;
  1219. default:
  1220. res = -ENOTCONN;
  1221. }
  1222. sock_unlock(tsock);
  1223. up(&tsock->sem);
  1224. return res;
  1225. }
  1226. /**
  1227. * setsockopt - set socket option
  1228. * @sock: socket structure
  1229. * @lvl: option level
  1230. * @opt: option identifier
  1231. * @ov: pointer to new option value
  1232. * @ol: length of option value
  1233. *
  1234. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1235. * (to ease compatibility).
  1236. *
  1237. * Returns 0 on success, errno otherwise
  1238. */
  1239. static int setsockopt(struct socket *sock,
  1240. int lvl, int opt, char __user *ov, int ol)
  1241. {
  1242. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1243. u32 value;
  1244. int res;
  1245. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1246. return 0;
  1247. if (lvl != SOL_TIPC)
  1248. return -ENOPROTOOPT;
  1249. if (ol < sizeof(value))
  1250. return -EINVAL;
  1251. if ((res = get_user(value, (u32 *)ov)))
  1252. return res;
  1253. if (down_interruptible(&tsock->sem))
  1254. return -ERESTARTSYS;
  1255. switch (opt) {
  1256. case TIPC_IMPORTANCE:
  1257. res = tipc_set_portimportance(tsock->p->ref, value);
  1258. break;
  1259. case TIPC_SRC_DROPPABLE:
  1260. if (sock->type != SOCK_STREAM)
  1261. res = tipc_set_portunreliable(tsock->p->ref, value);
  1262. else
  1263. res = -ENOPROTOOPT;
  1264. break;
  1265. case TIPC_DEST_DROPPABLE:
  1266. res = tipc_set_portunreturnable(tsock->p->ref, value);
  1267. break;
  1268. case TIPC_CONN_TIMEOUT:
  1269. sock->sk->sk_rcvtimeo = (value * HZ / 1000);
  1270. break;
  1271. default:
  1272. res = -EINVAL;
  1273. }
  1274. up(&tsock->sem);
  1275. return res;
  1276. }
  1277. /**
  1278. * getsockopt - get socket option
  1279. * @sock: socket structure
  1280. * @lvl: option level
  1281. * @opt: option identifier
  1282. * @ov: receptacle for option value
  1283. * @ol: receptacle for length of option value
  1284. *
  1285. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1286. * (to ease compatibility).
  1287. *
  1288. * Returns 0 on success, errno otherwise
  1289. */
  1290. static int getsockopt(struct socket *sock,
  1291. int lvl, int opt, char __user *ov, int *ol)
  1292. {
  1293. struct tipc_sock *tsock = tipc_sk(sock->sk);
  1294. int len;
  1295. u32 value;
  1296. int res;
  1297. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1298. return put_user(0, ol);
  1299. if (lvl != SOL_TIPC)
  1300. return -ENOPROTOOPT;
  1301. if ((res = get_user(len, ol)))
  1302. return res;
  1303. if (down_interruptible(&tsock->sem))
  1304. return -ERESTARTSYS;
  1305. switch (opt) {
  1306. case TIPC_IMPORTANCE:
  1307. res = tipc_portimportance(tsock->p->ref, &value);
  1308. break;
  1309. case TIPC_SRC_DROPPABLE:
  1310. res = tipc_portunreliable(tsock->p->ref, &value);
  1311. break;
  1312. case TIPC_DEST_DROPPABLE:
  1313. res = tipc_portunreturnable(tsock->p->ref, &value);
  1314. break;
  1315. case TIPC_CONN_TIMEOUT:
  1316. value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
  1317. break;
  1318. default:
  1319. res = -EINVAL;
  1320. }
  1321. if (res) {
  1322. /* "get" failed */
  1323. }
  1324. else if (len < sizeof(value)) {
  1325. res = -EINVAL;
  1326. }
  1327. else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
  1328. /* couldn't return value */
  1329. }
  1330. else {
  1331. res = put_user(sizeof(value), ol);
  1332. }
  1333. up(&tsock->sem);
  1334. return res;
  1335. }
  1336. /**
  1337. * Placeholders for non-implemented functionality
  1338. *
  1339. * Returns error code (POSIX-compliant where defined)
  1340. */
  1341. static int ioctl(struct socket *s, u32 cmd, unsigned long arg)
  1342. {
  1343. return -EINVAL;
  1344. }
  1345. static int no_mmap(struct file *file, struct socket *sock,
  1346. struct vm_area_struct *vma)
  1347. {
  1348. return -EINVAL;
  1349. }
  1350. static ssize_t no_sendpage(struct socket *sock, struct page *page,
  1351. int offset, size_t size, int flags)
  1352. {
  1353. return -EINVAL;
  1354. }
  1355. static int no_skpair(struct socket *s1, struct socket *s2)
  1356. {
  1357. return -EOPNOTSUPP;
  1358. }
  1359. /**
  1360. * Protocol switches for the various types of TIPC sockets
  1361. */
  1362. static struct proto_ops msg_ops = {
  1363. .owner = THIS_MODULE,
  1364. .family = AF_TIPC,
  1365. .release = release,
  1366. .bind = bind,
  1367. .connect = connect,
  1368. .socketpair = no_skpair,
  1369. .accept = accept,
  1370. .getname = get_name,
  1371. .poll = poll,
  1372. .ioctl = ioctl,
  1373. .listen = listen,
  1374. .shutdown = shutdown,
  1375. .setsockopt = setsockopt,
  1376. .getsockopt = getsockopt,
  1377. .sendmsg = send_msg,
  1378. .recvmsg = recv_msg,
  1379. .mmap = no_mmap,
  1380. .sendpage = no_sendpage
  1381. };
  1382. static struct proto_ops packet_ops = {
  1383. .owner = THIS_MODULE,
  1384. .family = AF_TIPC,
  1385. .release = release,
  1386. .bind = bind,
  1387. .connect = connect,
  1388. .socketpair = no_skpair,
  1389. .accept = accept,
  1390. .getname = get_name,
  1391. .poll = poll,
  1392. .ioctl = ioctl,
  1393. .listen = listen,
  1394. .shutdown = shutdown,
  1395. .setsockopt = setsockopt,
  1396. .getsockopt = getsockopt,
  1397. .sendmsg = send_packet,
  1398. .recvmsg = recv_msg,
  1399. .mmap = no_mmap,
  1400. .sendpage = no_sendpage
  1401. };
  1402. static struct proto_ops stream_ops = {
  1403. .owner = THIS_MODULE,
  1404. .family = AF_TIPC,
  1405. .release = release,
  1406. .bind = bind,
  1407. .connect = connect,
  1408. .socketpair = no_skpair,
  1409. .accept = accept,
  1410. .getname = get_name,
  1411. .poll = poll,
  1412. .ioctl = ioctl,
  1413. .listen = listen,
  1414. .shutdown = shutdown,
  1415. .setsockopt = setsockopt,
  1416. .getsockopt = getsockopt,
  1417. .sendmsg = send_stream,
  1418. .recvmsg = recv_stream,
  1419. .mmap = no_mmap,
  1420. .sendpage = no_sendpage
  1421. };
  1422. static struct net_proto_family tipc_family_ops = {
  1423. .owner = THIS_MODULE,
  1424. .family = AF_TIPC,
  1425. .create = tipc_create
  1426. };
  1427. static struct proto tipc_proto = {
  1428. .name = "TIPC",
  1429. .owner = THIS_MODULE,
  1430. .obj_size = sizeof(struct tipc_sock)
  1431. };
  1432. /**
  1433. * tipc_socket_init - initialize TIPC socket interface
  1434. *
  1435. * Returns 0 on success, errno otherwise
  1436. */
  1437. int tipc_socket_init(void)
  1438. {
  1439. int res;
  1440. res = proto_register(&tipc_proto, 1);
  1441. if (res) {
  1442. err("Failed to register TIPC protocol type\n");
  1443. goto out;
  1444. }
  1445. res = sock_register(&tipc_family_ops);
  1446. if (res) {
  1447. err("Failed to register TIPC socket type\n");
  1448. proto_unregister(&tipc_proto);
  1449. goto out;
  1450. }
  1451. sockets_enabled = 1;
  1452. out:
  1453. return res;
  1454. }
  1455. /**
  1456. * tipc_socket_stop - stop TIPC socket interface
  1457. */
  1458. void tipc_socket_stop(void)
  1459. {
  1460. if (!sockets_enabled)
  1461. return;
  1462. sockets_enabled = 0;
  1463. sock_unregister(tipc_family_ops.family);
  1464. proto_unregister(&tipc_proto);
  1465. }