socket.c 43 KB

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