pep.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /*
  2. * File: pep.c
  3. *
  4. * Phonet pipe protocol end point socket
  5. *
  6. * Copyright (C) 2008 Nokia Corporation.
  7. *
  8. * Author: Rémi Denis-Courmont <remi.denis-courmont@nokia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/socket.h>
  27. #include <net/sock.h>
  28. #include <net/tcp_states.h>
  29. #include <asm/ioctls.h>
  30. #include <linux/phonet.h>
  31. #include <linux/module.h>
  32. #include <net/phonet/phonet.h>
  33. #include <net/phonet/pep.h>
  34. #include <net/phonet/gprs.h>
  35. /* sk_state values:
  36. * TCP_CLOSE sock not in use yet
  37. * TCP_CLOSE_WAIT disconnected pipe
  38. * TCP_LISTEN listening pipe endpoint
  39. * TCP_SYN_RECV connected pipe in disabled state
  40. * TCP_ESTABLISHED connected pipe in enabled state
  41. *
  42. * pep_sock locking:
  43. * - sk_state, hlist: sock lock needed
  44. * - listener: read only
  45. * - pipe_handle: read only
  46. */
  47. #define CREDITS_MAX 10
  48. #define CREDITS_THR 7
  49. #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
  50. /* Get the next TLV sub-block. */
  51. static unsigned char *pep_get_sb(struct sk_buff *skb, u8 *ptype, u8 *plen,
  52. void *buf)
  53. {
  54. void *data = NULL;
  55. struct {
  56. u8 sb_type;
  57. u8 sb_len;
  58. } *ph, h;
  59. int buflen = *plen;
  60. ph = skb_header_pointer(skb, 0, 2, &h);
  61. if (ph == NULL || ph->sb_len < 2 || !pskb_may_pull(skb, ph->sb_len))
  62. return NULL;
  63. ph->sb_len -= 2;
  64. *ptype = ph->sb_type;
  65. *plen = ph->sb_len;
  66. if (buflen > ph->sb_len)
  67. buflen = ph->sb_len;
  68. data = skb_header_pointer(skb, 2, buflen, buf);
  69. __skb_pull(skb, 2 + ph->sb_len);
  70. return data;
  71. }
  72. static struct sk_buff *pep_alloc_skb(struct sock *sk, const void *payload,
  73. int len, gfp_t priority)
  74. {
  75. struct sk_buff *skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
  76. if (!skb)
  77. return NULL;
  78. skb_set_owner_w(skb, sk);
  79. skb_reserve(skb, MAX_PNPIPE_HEADER);
  80. __skb_put(skb, len);
  81. skb_copy_to_linear_data(skb, payload, len);
  82. __skb_push(skb, sizeof(struct pnpipehdr));
  83. skb_reset_transport_header(skb);
  84. return skb;
  85. }
  86. static int pep_reply(struct sock *sk, struct sk_buff *oskb, u8 code,
  87. const void *data, int len, gfp_t priority)
  88. {
  89. const struct pnpipehdr *oph = pnp_hdr(oskb);
  90. struct pnpipehdr *ph;
  91. struct sk_buff *skb;
  92. struct sockaddr_pn peer;
  93. skb = pep_alloc_skb(sk, data, len, priority);
  94. if (!skb)
  95. return -ENOMEM;
  96. ph = pnp_hdr(skb);
  97. ph->utid = oph->utid;
  98. ph->message_id = oph->message_id + 1; /* REQ -> RESP */
  99. ph->pipe_handle = oph->pipe_handle;
  100. ph->error_code = code;
  101. pn_skb_get_src_sockaddr(oskb, &peer);
  102. return pn_skb_send(sk, skb, &peer);
  103. }
  104. static int pep_indicate(struct sock *sk, u8 id, u8 code,
  105. const void *data, int len, gfp_t priority)
  106. {
  107. struct pep_sock *pn = pep_sk(sk);
  108. struct pnpipehdr *ph;
  109. struct sk_buff *skb;
  110. skb = pep_alloc_skb(sk, data, len, priority);
  111. if (!skb)
  112. return -ENOMEM;
  113. ph = pnp_hdr(skb);
  114. ph->utid = 0;
  115. ph->message_id = id;
  116. ph->pipe_handle = pn->pipe_handle;
  117. ph->data[0] = code;
  118. return pn_skb_send(sk, skb, NULL);
  119. }
  120. #define PAD 0x00
  121. static int pipe_handler_request(struct sock *sk, u8 id, u8 code,
  122. const void *data, int len)
  123. {
  124. struct pep_sock *pn = pep_sk(sk);
  125. struct pnpipehdr *ph;
  126. struct sk_buff *skb;
  127. skb = pep_alloc_skb(sk, data, len, GFP_KERNEL);
  128. if (!skb)
  129. return -ENOMEM;
  130. ph = pnp_hdr(skb);
  131. ph->utid = id; /* whatever */
  132. ph->message_id = id;
  133. ph->pipe_handle = pn->pipe_handle;
  134. ph->data[0] = code;
  135. return pn_skb_send(sk, skb, NULL);
  136. }
  137. static int pipe_handler_send_created_ind(struct sock *sk)
  138. {
  139. struct pep_sock *pn = pep_sk(sk);
  140. u8 data[4] = {
  141. PN_PIPE_SB_NEGOTIATED_FC, pep_sb_size(2),
  142. pn->tx_fc, pn->rx_fc,
  143. };
  144. return pep_indicate(sk, PNS_PIPE_CREATED_IND, 1 /* sub-blocks */,
  145. data, 4, GFP_ATOMIC);
  146. }
  147. static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
  148. {
  149. static const u8 data[20] = {
  150. PAD, PAD, PAD, 2 /* sub-blocks */,
  151. PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD,
  152. PN_MULTI_CREDIT_FLOW_CONTROL,
  153. PN_ONE_CREDIT_FLOW_CONTROL,
  154. PN_LEGACY_FLOW_CONTROL,
  155. PAD,
  156. PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD,
  157. PN_MULTI_CREDIT_FLOW_CONTROL,
  158. PN_ONE_CREDIT_FLOW_CONTROL,
  159. PN_LEGACY_FLOW_CONTROL,
  160. PAD,
  161. };
  162. might_sleep();
  163. return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
  164. GFP_KERNEL);
  165. }
  166. static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code,
  167. gfp_t priority)
  168. {
  169. static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
  170. WARN_ON(code == PN_PIPE_NO_ERROR);
  171. return pep_reply(sk, skb, code, data, sizeof(data), priority);
  172. }
  173. /* Control requests are not sent by the pipe service and have a specific
  174. * message format. */
  175. static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
  176. gfp_t priority)
  177. {
  178. const struct pnpipehdr *oph = pnp_hdr(oskb);
  179. struct sk_buff *skb;
  180. struct pnpipehdr *ph;
  181. struct sockaddr_pn dst;
  182. u8 data[4] = {
  183. oph->data[0], /* PEP type */
  184. code, /* error code, at an unusual offset */
  185. PAD, PAD,
  186. };
  187. skb = pep_alloc_skb(sk, data, 4, priority);
  188. if (!skb)
  189. return -ENOMEM;
  190. ph = pnp_hdr(skb);
  191. ph->utid = oph->utid;
  192. ph->message_id = PNS_PEP_CTRL_RESP;
  193. ph->pipe_handle = oph->pipe_handle;
  194. ph->data[0] = oph->data[1]; /* CTRL id */
  195. pn_skb_get_src_sockaddr(oskb, &dst);
  196. return pn_skb_send(sk, skb, &dst);
  197. }
  198. static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
  199. {
  200. u8 data[4] = { type, PAD, PAD, status };
  201. return pep_indicate(sk, PNS_PEP_STATUS_IND, PN_PEP_TYPE_COMMON,
  202. data, 4, priority);
  203. }
  204. /* Send our RX flow control information to the sender.
  205. * Socket must be locked. */
  206. static void pipe_grant_credits(struct sock *sk, gfp_t priority)
  207. {
  208. struct pep_sock *pn = pep_sk(sk);
  209. BUG_ON(sk->sk_state != TCP_ESTABLISHED);
  210. switch (pn->rx_fc) {
  211. case PN_LEGACY_FLOW_CONTROL: /* TODO */
  212. break;
  213. case PN_ONE_CREDIT_FLOW_CONTROL:
  214. if (pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
  215. PEP_IND_READY, priority) == 0)
  216. pn->rx_credits = 1;
  217. break;
  218. case PN_MULTI_CREDIT_FLOW_CONTROL:
  219. if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
  220. break;
  221. if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
  222. CREDITS_MAX - pn->rx_credits,
  223. priority) == 0)
  224. pn->rx_credits = CREDITS_MAX;
  225. break;
  226. }
  227. }
  228. static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
  229. {
  230. struct pep_sock *pn = pep_sk(sk);
  231. struct pnpipehdr *hdr;
  232. int wake = 0;
  233. if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
  234. return -EINVAL;
  235. hdr = pnp_hdr(skb);
  236. if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
  237. LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
  238. (unsigned int)hdr->data[0]);
  239. return -EOPNOTSUPP;
  240. }
  241. switch (hdr->data[1]) {
  242. case PN_PEP_IND_FLOW_CONTROL:
  243. switch (pn->tx_fc) {
  244. case PN_LEGACY_FLOW_CONTROL:
  245. switch (hdr->data[4]) {
  246. case PEP_IND_BUSY:
  247. atomic_set(&pn->tx_credits, 0);
  248. break;
  249. case PEP_IND_READY:
  250. atomic_set(&pn->tx_credits, wake = 1);
  251. break;
  252. }
  253. break;
  254. case PN_ONE_CREDIT_FLOW_CONTROL:
  255. if (hdr->data[4] == PEP_IND_READY)
  256. atomic_set(&pn->tx_credits, wake = 1);
  257. break;
  258. }
  259. break;
  260. case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
  261. if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
  262. break;
  263. atomic_add(wake = hdr->data[4], &pn->tx_credits);
  264. break;
  265. default:
  266. LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
  267. (unsigned int)hdr->data[1]);
  268. return -EOPNOTSUPP;
  269. }
  270. if (wake)
  271. sk->sk_write_space(sk);
  272. return 0;
  273. }
  274. static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
  275. {
  276. struct pep_sock *pn = pep_sk(sk);
  277. struct pnpipehdr *hdr = pnp_hdr(skb);
  278. u8 n_sb = hdr->data[0];
  279. pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
  280. __skb_pull(skb, sizeof(*hdr));
  281. while (n_sb > 0) {
  282. u8 type, buf[2], len = sizeof(buf);
  283. u8 *data = pep_get_sb(skb, &type, &len, buf);
  284. if (data == NULL)
  285. return -EINVAL;
  286. switch (type) {
  287. case PN_PIPE_SB_NEGOTIATED_FC:
  288. if (len < 2 || (data[0] | data[1]) > 3)
  289. break;
  290. pn->tx_fc = data[0] & 3;
  291. pn->rx_fc = data[1] & 3;
  292. break;
  293. }
  294. n_sb--;
  295. }
  296. return 0;
  297. }
  298. /* Queue an skb to a connected sock.
  299. * Socket lock must be held. */
  300. static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
  301. {
  302. struct pep_sock *pn = pep_sk(sk);
  303. struct pnpipehdr *hdr = pnp_hdr(skb);
  304. struct sk_buff_head *queue;
  305. int err = 0;
  306. BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
  307. switch (hdr->message_id) {
  308. case PNS_PEP_CONNECT_REQ:
  309. pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_ATOMIC);
  310. break;
  311. case PNS_PEP_DISCONNECT_REQ:
  312. pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
  313. sk->sk_state = TCP_CLOSE_WAIT;
  314. if (!sock_flag(sk, SOCK_DEAD))
  315. sk->sk_state_change(sk);
  316. break;
  317. case PNS_PEP_ENABLE_REQ:
  318. /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
  319. pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
  320. break;
  321. case PNS_PEP_RESET_REQ:
  322. switch (hdr->state_after_reset) {
  323. case PN_PIPE_DISABLE:
  324. pn->init_enable = 0;
  325. break;
  326. case PN_PIPE_ENABLE:
  327. pn->init_enable = 1;
  328. break;
  329. default: /* not allowed to send an error here!? */
  330. err = -EINVAL;
  331. goto out;
  332. }
  333. /* fall through */
  334. case PNS_PEP_DISABLE_REQ:
  335. atomic_set(&pn->tx_credits, 0);
  336. pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
  337. break;
  338. case PNS_PEP_CTRL_REQ:
  339. if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
  340. atomic_inc(&sk->sk_drops);
  341. break;
  342. }
  343. __skb_pull(skb, 4);
  344. queue = &pn->ctrlreq_queue;
  345. goto queue;
  346. case PNS_PIPE_ALIGNED_DATA:
  347. __skb_pull(skb, 1);
  348. /* fall through */
  349. case PNS_PIPE_DATA:
  350. __skb_pull(skb, 3); /* Pipe data header */
  351. if (!pn_flow_safe(pn->rx_fc)) {
  352. err = sock_queue_rcv_skb(sk, skb);
  353. if (!err)
  354. return NET_RX_SUCCESS;
  355. err = -ENOBUFS;
  356. break;
  357. }
  358. if (pn->rx_credits == 0) {
  359. atomic_inc(&sk->sk_drops);
  360. err = -ENOBUFS;
  361. break;
  362. }
  363. pn->rx_credits--;
  364. queue = &sk->sk_receive_queue;
  365. goto queue;
  366. case PNS_PEP_STATUS_IND:
  367. pipe_rcv_status(sk, skb);
  368. break;
  369. case PNS_PIPE_REDIRECTED_IND:
  370. err = pipe_rcv_created(sk, skb);
  371. break;
  372. case PNS_PIPE_CREATED_IND:
  373. err = pipe_rcv_created(sk, skb);
  374. if (err)
  375. break;
  376. /* fall through */
  377. case PNS_PIPE_RESET_IND:
  378. if (!pn->init_enable)
  379. break;
  380. /* fall through */
  381. case PNS_PIPE_ENABLED_IND:
  382. if (!pn_flow_safe(pn->tx_fc)) {
  383. atomic_set(&pn->tx_credits, 1);
  384. sk->sk_write_space(sk);
  385. }
  386. if (sk->sk_state == TCP_ESTABLISHED)
  387. break; /* Nothing to do */
  388. sk->sk_state = TCP_ESTABLISHED;
  389. pipe_grant_credits(sk, GFP_ATOMIC);
  390. break;
  391. case PNS_PIPE_DISABLED_IND:
  392. sk->sk_state = TCP_SYN_RECV;
  393. pn->rx_credits = 0;
  394. break;
  395. default:
  396. LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
  397. hdr->message_id);
  398. err = -EINVAL;
  399. }
  400. out:
  401. kfree_skb(skb);
  402. return (err == -ENOBUFS) ? NET_RX_DROP : NET_RX_SUCCESS;
  403. queue:
  404. skb->dev = NULL;
  405. skb_set_owner_r(skb, sk);
  406. err = skb->len;
  407. skb_queue_tail(queue, skb);
  408. if (!sock_flag(sk, SOCK_DEAD))
  409. sk->sk_data_ready(sk, err);
  410. return NET_RX_SUCCESS;
  411. }
  412. /* Destroy connected sock. */
  413. static void pipe_destruct(struct sock *sk)
  414. {
  415. struct pep_sock *pn = pep_sk(sk);
  416. skb_queue_purge(&sk->sk_receive_queue);
  417. skb_queue_purge(&pn->ctrlreq_queue);
  418. }
  419. static u8 pipe_negotiate_fc(const u8 *fcs, unsigned int n)
  420. {
  421. unsigned int i;
  422. u8 final_fc = PN_NO_FLOW_CONTROL;
  423. for (i = 0; i < n; i++) {
  424. u8 fc = fcs[i];
  425. if (fc > final_fc && fc < PN_MAX_FLOW_CONTROL)
  426. final_fc = fc;
  427. }
  428. return final_fc;
  429. }
  430. static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb)
  431. {
  432. struct pep_sock *pn = pep_sk(sk);
  433. struct pnpipehdr *hdr;
  434. u8 n_sb;
  435. if (!pskb_pull(skb, sizeof(*hdr) + 4))
  436. return -EINVAL;
  437. hdr = pnp_hdr(skb);
  438. if (hdr->error_code != PN_PIPE_NO_ERROR)
  439. return -ECONNREFUSED;
  440. /* Parse sub-blocks */
  441. n_sb = hdr->data[4];
  442. while (n_sb > 0) {
  443. u8 type, buf[6], len = sizeof(buf);
  444. const u8 *data = pep_get_sb(skb, &type, &len, buf);
  445. if (data == NULL)
  446. return -EINVAL;
  447. switch (type) {
  448. case PN_PIPE_SB_REQUIRED_FC_TX:
  449. if (len < 2 || len < data[0])
  450. break;
  451. pn->tx_fc = pipe_negotiate_fc(data + 2, len - 2);
  452. break;
  453. case PN_PIPE_SB_PREFERRED_FC_RX:
  454. if (len < 2 || len < data[0])
  455. break;
  456. pn->rx_fc = pipe_negotiate_fc(data + 2, len - 2);
  457. break;
  458. }
  459. n_sb--;
  460. }
  461. return pipe_handler_send_created_ind(sk);
  462. }
  463. static int pep_enableresp_rcv(struct sock *sk, struct sk_buff *skb)
  464. {
  465. struct pnpipehdr *hdr = pnp_hdr(skb);
  466. if (hdr->error_code != PN_PIPE_NO_ERROR)
  467. return -ECONNREFUSED;
  468. return pep_indicate(sk, PNS_PIPE_ENABLED_IND, 0 /* sub-blocks */,
  469. NULL, 0, GFP_ATOMIC);
  470. }
  471. static void pipe_start_flow_control(struct sock *sk)
  472. {
  473. struct pep_sock *pn = pep_sk(sk);
  474. if (!pn_flow_safe(pn->tx_fc)) {
  475. atomic_set(&pn->tx_credits, 1);
  476. sk->sk_write_space(sk);
  477. }
  478. pipe_grant_credits(sk, GFP_ATOMIC);
  479. }
  480. /* Queue an skb to an actively connected sock.
  481. * Socket lock must be held. */
  482. static int pipe_handler_do_rcv(struct sock *sk, struct sk_buff *skb)
  483. {
  484. struct pep_sock *pn = pep_sk(sk);
  485. struct pnpipehdr *hdr = pnp_hdr(skb);
  486. int err = NET_RX_SUCCESS;
  487. switch (hdr->message_id) {
  488. case PNS_PIPE_ALIGNED_DATA:
  489. __skb_pull(skb, 1);
  490. /* fall through */
  491. case PNS_PIPE_DATA:
  492. __skb_pull(skb, 3); /* Pipe data header */
  493. if (!pn_flow_safe(pn->rx_fc)) {
  494. err = sock_queue_rcv_skb(sk, skb);
  495. if (!err)
  496. return NET_RX_SUCCESS;
  497. err = NET_RX_DROP;
  498. break;
  499. }
  500. if (pn->rx_credits == 0) {
  501. atomic_inc(&sk->sk_drops);
  502. err = NET_RX_DROP;
  503. break;
  504. }
  505. pn->rx_credits--;
  506. skb->dev = NULL;
  507. skb_set_owner_r(skb, sk);
  508. err = skb->len;
  509. skb_queue_tail(&sk->sk_receive_queue, skb);
  510. if (!sock_flag(sk, SOCK_DEAD))
  511. sk->sk_data_ready(sk, err);
  512. return NET_RX_SUCCESS;
  513. case PNS_PEP_CONNECT_RESP:
  514. if (sk->sk_state != TCP_SYN_SENT)
  515. break;
  516. if (!sock_flag(sk, SOCK_DEAD))
  517. sk->sk_state_change(sk);
  518. if (pep_connresp_rcv(sk, skb)) {
  519. sk->sk_state = TCP_CLOSE_WAIT;
  520. break;
  521. }
  522. if (pn->init_enable == PN_PIPE_DISABLE)
  523. sk->sk_state = TCP_SYN_RECV;
  524. else {
  525. sk->sk_state = TCP_ESTABLISHED;
  526. pipe_start_flow_control(sk);
  527. }
  528. break;
  529. case PNS_PEP_ENABLE_RESP:
  530. if (sk->sk_state != TCP_SYN_SENT)
  531. break;
  532. if (pep_enableresp_rcv(sk, skb)) {
  533. sk->sk_state = TCP_CLOSE_WAIT;
  534. break;
  535. }
  536. sk->sk_state = TCP_ESTABLISHED;
  537. pipe_start_flow_control(sk);
  538. break;
  539. case PNS_PEP_DISCONNECT_RESP:
  540. /* sock should already be dead, nothing to do */
  541. break;
  542. case PNS_PEP_STATUS_IND:
  543. pipe_rcv_status(sk, skb);
  544. break;
  545. }
  546. kfree_skb(skb);
  547. return err;
  548. }
  549. /* Listening sock must be locked */
  550. static struct sock *pep_find_pipe(const struct hlist_head *hlist,
  551. const struct sockaddr_pn *dst,
  552. u8 pipe_handle)
  553. {
  554. struct hlist_node *node;
  555. struct sock *sknode;
  556. u16 dobj = pn_sockaddr_get_object(dst);
  557. sk_for_each(sknode, node, hlist) {
  558. struct pep_sock *pnnode = pep_sk(sknode);
  559. /* Ports match, but addresses might not: */
  560. if (pnnode->pn_sk.sobject != dobj)
  561. continue;
  562. if (pnnode->pipe_handle != pipe_handle)
  563. continue;
  564. if (sknode->sk_state == TCP_CLOSE_WAIT)
  565. continue;
  566. sock_hold(sknode);
  567. return sknode;
  568. }
  569. return NULL;
  570. }
  571. /*
  572. * Deliver an skb to a listening sock.
  573. * Socket lock must be held.
  574. * We then queue the skb to the right connected sock (if any).
  575. */
  576. static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
  577. {
  578. struct pep_sock *pn = pep_sk(sk);
  579. struct sock *sknode;
  580. struct pnpipehdr *hdr;
  581. struct sockaddr_pn dst;
  582. u8 pipe_handle;
  583. if (!pskb_may_pull(skb, sizeof(*hdr)))
  584. goto drop;
  585. hdr = pnp_hdr(skb);
  586. pipe_handle = hdr->pipe_handle;
  587. if (pipe_handle == PN_PIPE_INVALID_HANDLE)
  588. goto drop;
  589. pn_skb_get_dst_sockaddr(skb, &dst);
  590. /* Look for an existing pipe handle */
  591. sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
  592. if (sknode)
  593. return sk_receive_skb(sknode, skb, 1);
  594. switch (hdr->message_id) {
  595. case PNS_PEP_CONNECT_REQ:
  596. if (sk->sk_state != TCP_LISTEN || sk_acceptq_is_full(sk)) {
  597. pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE,
  598. GFP_ATOMIC);
  599. break;
  600. }
  601. skb_queue_head(&sk->sk_receive_queue, skb);
  602. sk_acceptq_added(sk);
  603. if (!sock_flag(sk, SOCK_DEAD))
  604. sk->sk_data_ready(sk, 0);
  605. return NET_RX_SUCCESS;
  606. case PNS_PEP_DISCONNECT_REQ:
  607. pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
  608. break;
  609. case PNS_PEP_CTRL_REQ:
  610. pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
  611. break;
  612. case PNS_PEP_RESET_REQ:
  613. case PNS_PEP_ENABLE_REQ:
  614. case PNS_PEP_DISABLE_REQ:
  615. /* invalid handle is not even allowed here! */
  616. break;
  617. default:
  618. if ((1 << sk->sk_state)
  619. & ~(TCPF_CLOSE|TCPF_LISTEN|TCPF_CLOSE_WAIT))
  620. /* actively connected socket */
  621. return pipe_handler_do_rcv(sk, skb);
  622. }
  623. drop:
  624. kfree_skb(skb);
  625. return NET_RX_SUCCESS;
  626. }
  627. static int pipe_do_remove(struct sock *sk)
  628. {
  629. struct pep_sock *pn = pep_sk(sk);
  630. struct pnpipehdr *ph;
  631. struct sk_buff *skb;
  632. skb = pep_alloc_skb(sk, NULL, 0, GFP_KERNEL);
  633. if (!skb)
  634. return -ENOMEM;
  635. ph = pnp_hdr(skb);
  636. ph->utid = 0;
  637. ph->message_id = PNS_PIPE_REMOVE_REQ;
  638. ph->pipe_handle = pn->pipe_handle;
  639. ph->data[0] = PAD;
  640. return pn_skb_send(sk, skb, NULL);
  641. }
  642. /* associated socket ceases to exist */
  643. static void pep_sock_close(struct sock *sk, long timeout)
  644. {
  645. struct pep_sock *pn = pep_sk(sk);
  646. int ifindex = 0;
  647. sock_hold(sk); /* keep a reference after sk_common_release() */
  648. sk_common_release(sk);
  649. lock_sock(sk);
  650. if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
  651. if (sk->sk_backlog_rcv == pipe_do_rcv)
  652. /* Forcefully remove dangling Phonet pipe */
  653. pipe_do_remove(sk);
  654. else
  655. pipe_handler_request(sk, PNS_PEP_DISCONNECT_REQ, PAD,
  656. NULL, 0);
  657. }
  658. sk->sk_state = TCP_CLOSE;
  659. ifindex = pn->ifindex;
  660. pn->ifindex = 0;
  661. release_sock(sk);
  662. if (ifindex)
  663. gprs_detach(sk);
  664. sock_put(sk);
  665. }
  666. static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
  667. {
  668. struct pep_sock *pn = pep_sk(sk), *newpn;
  669. struct sock *newsk = NULL;
  670. struct sk_buff *skb;
  671. struct pnpipehdr *hdr;
  672. struct sockaddr_pn dst, src;
  673. int err;
  674. u16 peer_type;
  675. u8 pipe_handle, enabled, n_sb;
  676. u8 aligned = 0;
  677. skb = skb_recv_datagram(sk, 0, flags & O_NONBLOCK, errp);
  678. if (!skb)
  679. return NULL;
  680. lock_sock(sk);
  681. if (sk->sk_state != TCP_LISTEN) {
  682. err = -EINVAL;
  683. goto drop;
  684. }
  685. sk_acceptq_removed(sk);
  686. err = -EPROTO;
  687. if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
  688. goto drop;
  689. hdr = pnp_hdr(skb);
  690. pipe_handle = hdr->pipe_handle;
  691. switch (hdr->state_after_connect) {
  692. case PN_PIPE_DISABLE:
  693. enabled = 0;
  694. break;
  695. case PN_PIPE_ENABLE:
  696. enabled = 1;
  697. break;
  698. default:
  699. pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM,
  700. GFP_KERNEL);
  701. goto drop;
  702. }
  703. peer_type = hdr->other_pep_type << 8;
  704. /* Parse sub-blocks (options) */
  705. n_sb = hdr->data[4];
  706. while (n_sb > 0) {
  707. u8 type, buf[1], len = sizeof(buf);
  708. const u8 *data = pep_get_sb(skb, &type, &len, buf);
  709. if (data == NULL)
  710. goto drop;
  711. switch (type) {
  712. case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
  713. if (len < 1)
  714. goto drop;
  715. peer_type = (peer_type & 0xff00) | data[0];
  716. break;
  717. case PN_PIPE_SB_ALIGNED_DATA:
  718. aligned = data[0] != 0;
  719. break;
  720. }
  721. n_sb--;
  722. }
  723. /* Check for duplicate pipe handle */
  724. newsk = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
  725. if (unlikely(newsk)) {
  726. __sock_put(newsk);
  727. newsk = NULL;
  728. pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_KERNEL);
  729. goto drop;
  730. }
  731. /* Create a new to-be-accepted sock */
  732. newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_KERNEL, sk->sk_prot);
  733. if (!newsk) {
  734. pep_reject_conn(sk, skb, PN_PIPE_ERR_OVERLOAD, GFP_KERNEL);
  735. err = -ENOBUFS;
  736. goto drop;
  737. }
  738. sock_init_data(NULL, newsk);
  739. newsk->sk_state = TCP_SYN_RECV;
  740. newsk->sk_backlog_rcv = pipe_do_rcv;
  741. newsk->sk_protocol = sk->sk_protocol;
  742. newsk->sk_destruct = pipe_destruct;
  743. newpn = pep_sk(newsk);
  744. pn_skb_get_dst_sockaddr(skb, &dst);
  745. pn_skb_get_src_sockaddr(skb, &src);
  746. newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
  747. newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
  748. newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
  749. sock_hold(sk);
  750. newpn->listener = sk;
  751. skb_queue_head_init(&newpn->ctrlreq_queue);
  752. newpn->pipe_handle = pipe_handle;
  753. atomic_set(&newpn->tx_credits, 0);
  754. newpn->ifindex = 0;
  755. newpn->peer_type = peer_type;
  756. newpn->rx_credits = 0;
  757. newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
  758. newpn->init_enable = enabled;
  759. newpn->aligned = aligned;
  760. err = pep_accept_conn(newsk, skb);
  761. if (err) {
  762. sock_put(newsk);
  763. newsk = NULL;
  764. goto drop;
  765. }
  766. sk_add_node(newsk, &pn->hlist);
  767. drop:
  768. release_sock(sk);
  769. kfree_skb(skb);
  770. *errp = err;
  771. return newsk;
  772. }
  773. static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
  774. {
  775. struct pep_sock *pn = pep_sk(sk);
  776. int err;
  777. u8 data[4] = { 0 /* sub-blocks */, PAD, PAD, PAD };
  778. if (pn->pipe_handle == PN_PIPE_INVALID_HANDLE)
  779. pn->pipe_handle = 1; /* anything but INVALID_HANDLE */
  780. err = pipe_handler_request(sk, PNS_PEP_CONNECT_REQ,
  781. pn->init_enable, data, 4);
  782. if (err) {
  783. pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
  784. return err;
  785. }
  786. sk->sk_state = TCP_SYN_SENT;
  787. return 0;
  788. }
  789. static int pep_sock_enable(struct sock *sk, struct sockaddr *addr, int len)
  790. {
  791. int err;
  792. err = pipe_handler_request(sk, PNS_PEP_ENABLE_REQ, PAD,
  793. NULL, 0);
  794. if (err)
  795. return err;
  796. sk->sk_state = TCP_SYN_SENT;
  797. return 0;
  798. }
  799. static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
  800. {
  801. struct pep_sock *pn = pep_sk(sk);
  802. int answ;
  803. int ret = -ENOIOCTLCMD;
  804. switch (cmd) {
  805. case SIOCINQ:
  806. if (sk->sk_state == TCP_LISTEN) {
  807. ret = -EINVAL;
  808. break;
  809. }
  810. lock_sock(sk);
  811. if (sock_flag(sk, SOCK_URGINLINE) &&
  812. !skb_queue_empty(&pn->ctrlreq_queue))
  813. answ = skb_peek(&pn->ctrlreq_queue)->len;
  814. else if (!skb_queue_empty(&sk->sk_receive_queue))
  815. answ = skb_peek(&sk->sk_receive_queue)->len;
  816. else
  817. answ = 0;
  818. release_sock(sk);
  819. ret = put_user(answ, (int __user *)arg);
  820. break;
  821. case SIOCPNENABLEPIPE:
  822. lock_sock(sk);
  823. if (sk->sk_state == TCP_SYN_SENT)
  824. ret = -EBUSY;
  825. else if (sk->sk_state == TCP_ESTABLISHED)
  826. ret = -EISCONN;
  827. else
  828. ret = pep_sock_enable(sk, NULL, 0);
  829. release_sock(sk);
  830. break;
  831. }
  832. return ret;
  833. }
  834. static int pep_init(struct sock *sk)
  835. {
  836. struct pep_sock *pn = pep_sk(sk);
  837. sk->sk_destruct = pipe_destruct;
  838. INIT_HLIST_HEAD(&pn->hlist);
  839. pn->listener = NULL;
  840. skb_queue_head_init(&pn->ctrlreq_queue);
  841. atomic_set(&pn->tx_credits, 0);
  842. pn->ifindex = 0;
  843. pn->peer_type = 0;
  844. pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
  845. pn->rx_credits = 0;
  846. pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
  847. pn->init_enable = 1;
  848. pn->aligned = 0;
  849. return 0;
  850. }
  851. static int pep_setsockopt(struct sock *sk, int level, int optname,
  852. char __user *optval, unsigned int optlen)
  853. {
  854. struct pep_sock *pn = pep_sk(sk);
  855. int val = 0, err = 0;
  856. if (level != SOL_PNPIPE)
  857. return -ENOPROTOOPT;
  858. if (optlen >= sizeof(int)) {
  859. if (get_user(val, (int __user *) optval))
  860. return -EFAULT;
  861. }
  862. lock_sock(sk);
  863. switch (optname) {
  864. case PNPIPE_ENCAP:
  865. if (val && val != PNPIPE_ENCAP_IP) {
  866. err = -EINVAL;
  867. break;
  868. }
  869. if (!pn->ifindex == !val)
  870. break; /* Nothing to do! */
  871. if (!capable(CAP_NET_ADMIN)) {
  872. err = -EPERM;
  873. break;
  874. }
  875. if (val) {
  876. release_sock(sk);
  877. err = gprs_attach(sk);
  878. if (err > 0) {
  879. pn->ifindex = err;
  880. err = 0;
  881. }
  882. } else {
  883. pn->ifindex = 0;
  884. release_sock(sk);
  885. gprs_detach(sk);
  886. err = 0;
  887. }
  888. goto out_norel;
  889. case PNPIPE_HANDLE:
  890. if ((sk->sk_state == TCP_CLOSE) &&
  891. (val >= 0) && (val < PN_PIPE_INVALID_HANDLE))
  892. pn->pipe_handle = val;
  893. else
  894. err = -EINVAL;
  895. break;
  896. case PNPIPE_INITSTATE:
  897. pn->init_enable = !!val;
  898. break;
  899. default:
  900. err = -ENOPROTOOPT;
  901. }
  902. release_sock(sk);
  903. out_norel:
  904. return err;
  905. }
  906. static int pep_getsockopt(struct sock *sk, int level, int optname,
  907. char __user *optval, int __user *optlen)
  908. {
  909. struct pep_sock *pn = pep_sk(sk);
  910. int len, val;
  911. if (level != SOL_PNPIPE)
  912. return -ENOPROTOOPT;
  913. if (get_user(len, optlen))
  914. return -EFAULT;
  915. switch (optname) {
  916. case PNPIPE_ENCAP:
  917. val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
  918. break;
  919. case PNPIPE_IFINDEX:
  920. val = pn->ifindex;
  921. break;
  922. case PNPIPE_HANDLE:
  923. val = pn->pipe_handle;
  924. if (val == PN_PIPE_INVALID_HANDLE)
  925. return -EINVAL;
  926. break;
  927. case PNPIPE_INITSTATE:
  928. val = pn->init_enable;
  929. break;
  930. default:
  931. return -ENOPROTOOPT;
  932. }
  933. len = min_t(unsigned int, sizeof(int), len);
  934. if (put_user(len, optlen))
  935. return -EFAULT;
  936. if (put_user(val, (int __user *) optval))
  937. return -EFAULT;
  938. return 0;
  939. }
  940. static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
  941. {
  942. struct pep_sock *pn = pep_sk(sk);
  943. struct pnpipehdr *ph;
  944. int err;
  945. if (pn_flow_safe(pn->tx_fc) &&
  946. !atomic_add_unless(&pn->tx_credits, -1, 0)) {
  947. kfree_skb(skb);
  948. return -ENOBUFS;
  949. }
  950. skb_push(skb, 3 + pn->aligned);
  951. skb_reset_transport_header(skb);
  952. ph = pnp_hdr(skb);
  953. ph->utid = 0;
  954. if (pn->aligned) {
  955. ph->message_id = PNS_PIPE_ALIGNED_DATA;
  956. ph->data[0] = 0; /* padding */
  957. } else
  958. ph->message_id = PNS_PIPE_DATA;
  959. ph->pipe_handle = pn->pipe_handle;
  960. err = pn_skb_send(sk, skb, NULL);
  961. if (err && pn_flow_safe(pn->tx_fc))
  962. atomic_inc(&pn->tx_credits);
  963. return err;
  964. }
  965. static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
  966. struct msghdr *msg, size_t len)
  967. {
  968. struct pep_sock *pn = pep_sk(sk);
  969. struct sk_buff *skb;
  970. long timeo;
  971. int flags = msg->msg_flags;
  972. int err, done;
  973. if (len > USHRT_MAX)
  974. return -EMSGSIZE;
  975. if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
  976. MSG_CMSG_COMPAT)) ||
  977. !(msg->msg_flags & MSG_EOR))
  978. return -EOPNOTSUPP;
  979. skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
  980. flags & MSG_DONTWAIT, &err);
  981. if (!skb)
  982. return err;
  983. skb_reserve(skb, MAX_PHONET_HEADER + 3 + pn->aligned);
  984. err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  985. if (err < 0)
  986. goto outfree;
  987. lock_sock(sk);
  988. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  989. if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
  990. err = -ENOTCONN;
  991. goto out;
  992. }
  993. if (sk->sk_state != TCP_ESTABLISHED) {
  994. /* Wait until the pipe gets to enabled state */
  995. disabled:
  996. err = sk_stream_wait_connect(sk, &timeo);
  997. if (err)
  998. goto out;
  999. if (sk->sk_state == TCP_CLOSE_WAIT) {
  1000. err = -ECONNRESET;
  1001. goto out;
  1002. }
  1003. }
  1004. BUG_ON(sk->sk_state != TCP_ESTABLISHED);
  1005. /* Wait until flow control allows TX */
  1006. done = atomic_read(&pn->tx_credits);
  1007. while (!done) {
  1008. DEFINE_WAIT(wait);
  1009. if (!timeo) {
  1010. err = -EAGAIN;
  1011. goto out;
  1012. }
  1013. if (signal_pending(current)) {
  1014. err = sock_intr_errno(timeo);
  1015. goto out;
  1016. }
  1017. prepare_to_wait(sk_sleep(sk), &wait,
  1018. TASK_INTERRUPTIBLE);
  1019. done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
  1020. finish_wait(sk_sleep(sk), &wait);
  1021. if (sk->sk_state != TCP_ESTABLISHED)
  1022. goto disabled;
  1023. }
  1024. err = pipe_skb_send(sk, skb);
  1025. if (err >= 0)
  1026. err = len; /* success! */
  1027. skb = NULL;
  1028. out:
  1029. release_sock(sk);
  1030. outfree:
  1031. kfree_skb(skb);
  1032. return err;
  1033. }
  1034. int pep_writeable(struct sock *sk)
  1035. {
  1036. struct pep_sock *pn = pep_sk(sk);
  1037. return atomic_read(&pn->tx_credits);
  1038. }
  1039. int pep_write(struct sock *sk, struct sk_buff *skb)
  1040. {
  1041. struct sk_buff *rskb, *fs;
  1042. int flen = 0;
  1043. if (pep_sk(sk)->aligned)
  1044. return pipe_skb_send(sk, skb);
  1045. rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
  1046. if (!rskb) {
  1047. kfree_skb(skb);
  1048. return -ENOMEM;
  1049. }
  1050. skb_shinfo(rskb)->frag_list = skb;
  1051. rskb->len += skb->len;
  1052. rskb->data_len += rskb->len;
  1053. rskb->truesize += rskb->len;
  1054. /* Avoid nested fragments */
  1055. skb_walk_frags(skb, fs)
  1056. flen += fs->len;
  1057. skb->next = skb_shinfo(skb)->frag_list;
  1058. skb_frag_list_init(skb);
  1059. skb->len -= flen;
  1060. skb->data_len -= flen;
  1061. skb->truesize -= flen;
  1062. skb_reserve(rskb, MAX_PHONET_HEADER + 3);
  1063. return pipe_skb_send(sk, rskb);
  1064. }
  1065. struct sk_buff *pep_read(struct sock *sk)
  1066. {
  1067. struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
  1068. if (sk->sk_state == TCP_ESTABLISHED)
  1069. pipe_grant_credits(sk, GFP_ATOMIC);
  1070. return skb;
  1071. }
  1072. static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
  1073. struct msghdr *msg, size_t len, int noblock,
  1074. int flags, int *addr_len)
  1075. {
  1076. struct sk_buff *skb;
  1077. int err;
  1078. if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
  1079. MSG_NOSIGNAL|MSG_CMSG_COMPAT))
  1080. return -EOPNOTSUPP;
  1081. if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
  1082. return -ENOTCONN;
  1083. if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
  1084. /* Dequeue and acknowledge control request */
  1085. struct pep_sock *pn = pep_sk(sk);
  1086. if (flags & MSG_PEEK)
  1087. return -EOPNOTSUPP;
  1088. skb = skb_dequeue(&pn->ctrlreq_queue);
  1089. if (skb) {
  1090. pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
  1091. GFP_KERNEL);
  1092. msg->msg_flags |= MSG_OOB;
  1093. goto copy;
  1094. }
  1095. if (flags & MSG_OOB)
  1096. return -EINVAL;
  1097. }
  1098. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1099. lock_sock(sk);
  1100. if (skb == NULL) {
  1101. if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
  1102. err = -ECONNRESET;
  1103. release_sock(sk);
  1104. return err;
  1105. }
  1106. if (sk->sk_state == TCP_ESTABLISHED)
  1107. pipe_grant_credits(sk, GFP_KERNEL);
  1108. release_sock(sk);
  1109. copy:
  1110. msg->msg_flags |= MSG_EOR;
  1111. if (skb->len > len)
  1112. msg->msg_flags |= MSG_TRUNC;
  1113. else
  1114. len = skb->len;
  1115. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
  1116. if (!err)
  1117. err = (flags & MSG_TRUNC) ? skb->len : len;
  1118. skb_free_datagram(sk, skb);
  1119. return err;
  1120. }
  1121. static void pep_sock_unhash(struct sock *sk)
  1122. {
  1123. struct pep_sock *pn = pep_sk(sk);
  1124. struct sock *skparent = NULL;
  1125. lock_sock(sk);
  1126. if (pn->listener != NULL) {
  1127. skparent = pn->listener;
  1128. pn->listener = NULL;
  1129. release_sock(sk);
  1130. pn = pep_sk(skparent);
  1131. lock_sock(skparent);
  1132. sk_del_node_init(sk);
  1133. sk = skparent;
  1134. }
  1135. /* Unhash a listening sock only when it is closed
  1136. * and all of its active connected pipes are closed. */
  1137. if (hlist_empty(&pn->hlist))
  1138. pn_sock_unhash(&pn->pn_sk.sk);
  1139. release_sock(sk);
  1140. if (skparent)
  1141. sock_put(skparent);
  1142. }
  1143. static struct proto pep_proto = {
  1144. .close = pep_sock_close,
  1145. .accept = pep_sock_accept,
  1146. .connect = pep_sock_connect,
  1147. .ioctl = pep_ioctl,
  1148. .init = pep_init,
  1149. .setsockopt = pep_setsockopt,
  1150. .getsockopt = pep_getsockopt,
  1151. .sendmsg = pep_sendmsg,
  1152. .recvmsg = pep_recvmsg,
  1153. .backlog_rcv = pep_do_rcv,
  1154. .hash = pn_sock_hash,
  1155. .unhash = pep_sock_unhash,
  1156. .get_port = pn_sock_get_port,
  1157. .obj_size = sizeof(struct pep_sock),
  1158. .owner = THIS_MODULE,
  1159. .name = "PNPIPE",
  1160. };
  1161. static struct phonet_protocol pep_pn_proto = {
  1162. .ops = &phonet_stream_ops,
  1163. .prot = &pep_proto,
  1164. .sock_type = SOCK_SEQPACKET,
  1165. };
  1166. static int __init pep_register(void)
  1167. {
  1168. return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
  1169. }
  1170. static void __exit pep_unregister(void)
  1171. {
  1172. phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
  1173. }
  1174. module_init(pep_register);
  1175. module_exit(pep_unregister);
  1176. MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
  1177. MODULE_DESCRIPTION("Phonet pipe protocol");
  1178. MODULE_LICENSE("GPL");
  1179. MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);