pep.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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
  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->error_code = 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->error_code = 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->pep_type, /* 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->data0 = oph->data[0]; /* 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->pep_type != PN_PEP_TYPE_COMMON) {
  237. net_dbg_ratelimited("Phonet unknown PEP type: %u\n",
  238. (unsigned int)hdr->pep_type);
  239. return -EOPNOTSUPP;
  240. }
  241. switch (hdr->data[0]) {
  242. case PN_PEP_IND_FLOW_CONTROL:
  243. switch (pn->tx_fc) {
  244. case PN_LEGACY_FLOW_CONTROL:
  245. switch (hdr->data[3]) {
  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[3] == 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[3], &pn->tx_credits);
  264. break;
  265. default:
  266. net_dbg_ratelimited("Phonet unknown PEP indication: %u\n",
  267. (unsigned int)hdr->data[0]);
  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->data0;
  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. net_dbg_ratelimited("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. skb_queue_tail(queue, skb);
  407. if (!sock_flag(sk, SOCK_DEAD))
  408. sk->sk_data_ready(sk);
  409. return NET_RX_SUCCESS;
  410. }
  411. /* Destroy connected sock. */
  412. static void pipe_destruct(struct sock *sk)
  413. {
  414. struct pep_sock *pn = pep_sk(sk);
  415. skb_queue_purge(&sk->sk_receive_queue);
  416. skb_queue_purge(&pn->ctrlreq_queue);
  417. }
  418. static u8 pipe_negotiate_fc(const u8 *fcs, unsigned int n)
  419. {
  420. unsigned int i;
  421. u8 final_fc = PN_NO_FLOW_CONTROL;
  422. for (i = 0; i < n; i++) {
  423. u8 fc = fcs[i];
  424. if (fc > final_fc && fc < PN_MAX_FLOW_CONTROL)
  425. final_fc = fc;
  426. }
  427. return final_fc;
  428. }
  429. static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb)
  430. {
  431. struct pep_sock *pn = pep_sk(sk);
  432. struct pnpipehdr *hdr;
  433. u8 n_sb;
  434. if (!pskb_pull(skb, sizeof(*hdr) + 4))
  435. return -EINVAL;
  436. hdr = pnp_hdr(skb);
  437. if (hdr->error_code != PN_PIPE_NO_ERROR)
  438. return -ECONNREFUSED;
  439. /* Parse sub-blocks */
  440. n_sb = hdr->data[3];
  441. while (n_sb > 0) {
  442. u8 type, buf[6], len = sizeof(buf);
  443. const u8 *data = pep_get_sb(skb, &type, &len, buf);
  444. if (data == NULL)
  445. return -EINVAL;
  446. switch (type) {
  447. case PN_PIPE_SB_REQUIRED_FC_TX:
  448. if (len < 2 || len < data[0])
  449. break;
  450. pn->tx_fc = pipe_negotiate_fc(data + 2, len - 2);
  451. break;
  452. case PN_PIPE_SB_PREFERRED_FC_RX:
  453. if (len < 2 || len < data[0])
  454. break;
  455. pn->rx_fc = pipe_negotiate_fc(data + 2, len - 2);
  456. break;
  457. }
  458. n_sb--;
  459. }
  460. return pipe_handler_send_created_ind(sk);
  461. }
  462. static int pep_enableresp_rcv(struct sock *sk, struct sk_buff *skb)
  463. {
  464. struct pnpipehdr *hdr = pnp_hdr(skb);
  465. if (hdr->error_code != PN_PIPE_NO_ERROR)
  466. return -ECONNREFUSED;
  467. return pep_indicate(sk, PNS_PIPE_ENABLED_IND, 0 /* sub-blocks */,
  468. NULL, 0, GFP_ATOMIC);
  469. }
  470. static void pipe_start_flow_control(struct sock *sk)
  471. {
  472. struct pep_sock *pn = pep_sk(sk);
  473. if (!pn_flow_safe(pn->tx_fc)) {
  474. atomic_set(&pn->tx_credits, 1);
  475. sk->sk_write_space(sk);
  476. }
  477. pipe_grant_credits(sk, GFP_ATOMIC);
  478. }
  479. /* Queue an skb to an actively connected sock.
  480. * Socket lock must be held. */
  481. static int pipe_handler_do_rcv(struct sock *sk, struct sk_buff *skb)
  482. {
  483. struct pep_sock *pn = pep_sk(sk);
  484. struct pnpipehdr *hdr = pnp_hdr(skb);
  485. int err = NET_RX_SUCCESS;
  486. switch (hdr->message_id) {
  487. case PNS_PIPE_ALIGNED_DATA:
  488. __skb_pull(skb, 1);
  489. /* fall through */
  490. case PNS_PIPE_DATA:
  491. __skb_pull(skb, 3); /* Pipe data header */
  492. if (!pn_flow_safe(pn->rx_fc)) {
  493. err = sock_queue_rcv_skb(sk, skb);
  494. if (!err)
  495. return NET_RX_SUCCESS;
  496. err = NET_RX_DROP;
  497. break;
  498. }
  499. if (pn->rx_credits == 0) {
  500. atomic_inc(&sk->sk_drops);
  501. err = NET_RX_DROP;
  502. break;
  503. }
  504. pn->rx_credits--;
  505. skb->dev = NULL;
  506. skb_set_owner_r(skb, sk);
  507. skb_queue_tail(&sk->sk_receive_queue, skb);
  508. if (!sock_flag(sk, SOCK_DEAD))
  509. sk->sk_data_ready(sk);
  510. return NET_RX_SUCCESS;
  511. case PNS_PEP_CONNECT_RESP:
  512. if (sk->sk_state != TCP_SYN_SENT)
  513. break;
  514. if (!sock_flag(sk, SOCK_DEAD))
  515. sk->sk_state_change(sk);
  516. if (pep_connresp_rcv(sk, skb)) {
  517. sk->sk_state = TCP_CLOSE_WAIT;
  518. break;
  519. }
  520. if (pn->init_enable == PN_PIPE_DISABLE)
  521. sk->sk_state = TCP_SYN_RECV;
  522. else {
  523. sk->sk_state = TCP_ESTABLISHED;
  524. pipe_start_flow_control(sk);
  525. }
  526. break;
  527. case PNS_PEP_ENABLE_RESP:
  528. if (sk->sk_state != TCP_SYN_SENT)
  529. break;
  530. if (pep_enableresp_rcv(sk, skb)) {
  531. sk->sk_state = TCP_CLOSE_WAIT;
  532. break;
  533. }
  534. sk->sk_state = TCP_ESTABLISHED;
  535. pipe_start_flow_control(sk);
  536. break;
  537. case PNS_PEP_DISCONNECT_RESP:
  538. /* sock should already be dead, nothing to do */
  539. break;
  540. case PNS_PEP_STATUS_IND:
  541. pipe_rcv_status(sk, skb);
  542. break;
  543. }
  544. kfree_skb(skb);
  545. return err;
  546. }
  547. /* Listening sock must be locked */
  548. static struct sock *pep_find_pipe(const struct hlist_head *hlist,
  549. const struct sockaddr_pn *dst,
  550. u8 pipe_handle)
  551. {
  552. struct sock *sknode;
  553. u16 dobj = pn_sockaddr_get_object(dst);
  554. sk_for_each(sknode, hlist) {
  555. struct pep_sock *pnnode = pep_sk(sknode);
  556. /* Ports match, but addresses might not: */
  557. if (pnnode->pn_sk.sobject != dobj)
  558. continue;
  559. if (pnnode->pipe_handle != pipe_handle)
  560. continue;
  561. if (sknode->sk_state == TCP_CLOSE_WAIT)
  562. continue;
  563. sock_hold(sknode);
  564. return sknode;
  565. }
  566. return NULL;
  567. }
  568. /*
  569. * Deliver an skb to a listening sock.
  570. * Socket lock must be held.
  571. * We then queue the skb to the right connected sock (if any).
  572. */
  573. static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
  574. {
  575. struct pep_sock *pn = pep_sk(sk);
  576. struct sock *sknode;
  577. struct pnpipehdr *hdr;
  578. struct sockaddr_pn dst;
  579. u8 pipe_handle;
  580. if (!pskb_may_pull(skb, sizeof(*hdr)))
  581. goto drop;
  582. hdr = pnp_hdr(skb);
  583. pipe_handle = hdr->pipe_handle;
  584. if (pipe_handle == PN_PIPE_INVALID_HANDLE)
  585. goto drop;
  586. pn_skb_get_dst_sockaddr(skb, &dst);
  587. /* Look for an existing pipe handle */
  588. sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
  589. if (sknode)
  590. return sk_receive_skb(sknode, skb, 1);
  591. switch (hdr->message_id) {
  592. case PNS_PEP_CONNECT_REQ:
  593. if (sk->sk_state != TCP_LISTEN || sk_acceptq_is_full(sk)) {
  594. pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE,
  595. GFP_ATOMIC);
  596. break;
  597. }
  598. skb_queue_head(&sk->sk_receive_queue, skb);
  599. sk_acceptq_added(sk);
  600. if (!sock_flag(sk, SOCK_DEAD))
  601. sk->sk_data_ready(sk);
  602. return NET_RX_SUCCESS;
  603. case PNS_PEP_DISCONNECT_REQ:
  604. pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
  605. break;
  606. case PNS_PEP_CTRL_REQ:
  607. pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
  608. break;
  609. case PNS_PEP_RESET_REQ:
  610. case PNS_PEP_ENABLE_REQ:
  611. case PNS_PEP_DISABLE_REQ:
  612. /* invalid handle is not even allowed here! */
  613. break;
  614. default:
  615. if ((1 << sk->sk_state)
  616. & ~(TCPF_CLOSE|TCPF_LISTEN|TCPF_CLOSE_WAIT))
  617. /* actively connected socket */
  618. return pipe_handler_do_rcv(sk, skb);
  619. }
  620. drop:
  621. kfree_skb(skb);
  622. return NET_RX_SUCCESS;
  623. }
  624. static int pipe_do_remove(struct sock *sk)
  625. {
  626. struct pep_sock *pn = pep_sk(sk);
  627. struct pnpipehdr *ph;
  628. struct sk_buff *skb;
  629. skb = pep_alloc_skb(sk, NULL, 0, GFP_KERNEL);
  630. if (!skb)
  631. return -ENOMEM;
  632. ph = pnp_hdr(skb);
  633. ph->utid = 0;
  634. ph->message_id = PNS_PIPE_REMOVE_REQ;
  635. ph->pipe_handle = pn->pipe_handle;
  636. ph->data0 = PAD;
  637. return pn_skb_send(sk, skb, NULL);
  638. }
  639. /* associated socket ceases to exist */
  640. static void pep_sock_close(struct sock *sk, long timeout)
  641. {
  642. struct pep_sock *pn = pep_sk(sk);
  643. int ifindex = 0;
  644. sock_hold(sk); /* keep a reference after sk_common_release() */
  645. sk_common_release(sk);
  646. lock_sock(sk);
  647. if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
  648. if (sk->sk_backlog_rcv == pipe_do_rcv)
  649. /* Forcefully remove dangling Phonet pipe */
  650. pipe_do_remove(sk);
  651. else
  652. pipe_handler_request(sk, PNS_PEP_DISCONNECT_REQ, PAD,
  653. NULL, 0);
  654. }
  655. sk->sk_state = TCP_CLOSE;
  656. ifindex = pn->ifindex;
  657. pn->ifindex = 0;
  658. release_sock(sk);
  659. if (ifindex)
  660. gprs_detach(sk);
  661. sock_put(sk);
  662. }
  663. static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
  664. {
  665. struct pep_sock *pn = pep_sk(sk), *newpn;
  666. struct sock *newsk = NULL;
  667. struct sk_buff *skb;
  668. struct pnpipehdr *hdr;
  669. struct sockaddr_pn dst, src;
  670. int err;
  671. u16 peer_type;
  672. u8 pipe_handle, enabled, n_sb;
  673. u8 aligned = 0;
  674. skb = skb_recv_datagram(sk, 0, flags & O_NONBLOCK, errp);
  675. if (!skb)
  676. return NULL;
  677. lock_sock(sk);
  678. if (sk->sk_state != TCP_LISTEN) {
  679. err = -EINVAL;
  680. goto drop;
  681. }
  682. sk_acceptq_removed(sk);
  683. err = -EPROTO;
  684. if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
  685. goto drop;
  686. hdr = pnp_hdr(skb);
  687. pipe_handle = hdr->pipe_handle;
  688. switch (hdr->state_after_connect) {
  689. case PN_PIPE_DISABLE:
  690. enabled = 0;
  691. break;
  692. case PN_PIPE_ENABLE:
  693. enabled = 1;
  694. break;
  695. default:
  696. pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM,
  697. GFP_KERNEL);
  698. goto drop;
  699. }
  700. peer_type = hdr->other_pep_type << 8;
  701. /* Parse sub-blocks (options) */
  702. n_sb = hdr->data[3];
  703. while (n_sb > 0) {
  704. u8 type, buf[1], len = sizeof(buf);
  705. const u8 *data = pep_get_sb(skb, &type, &len, buf);
  706. if (data == NULL)
  707. goto drop;
  708. switch (type) {
  709. case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
  710. if (len < 1)
  711. goto drop;
  712. peer_type = (peer_type & 0xff00) | data[0];
  713. break;
  714. case PN_PIPE_SB_ALIGNED_DATA:
  715. aligned = data[0] != 0;
  716. break;
  717. }
  718. n_sb--;
  719. }
  720. /* Check for duplicate pipe handle */
  721. newsk = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
  722. if (unlikely(newsk)) {
  723. __sock_put(newsk);
  724. newsk = NULL;
  725. pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_KERNEL);
  726. goto drop;
  727. }
  728. /* Create a new to-be-accepted sock */
  729. newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_KERNEL, sk->sk_prot, 0);
  730. if (!newsk) {
  731. pep_reject_conn(sk, skb, PN_PIPE_ERR_OVERLOAD, GFP_KERNEL);
  732. err = -ENOBUFS;
  733. goto drop;
  734. }
  735. sock_init_data(NULL, newsk);
  736. newsk->sk_state = TCP_SYN_RECV;
  737. newsk->sk_backlog_rcv = pipe_do_rcv;
  738. newsk->sk_protocol = sk->sk_protocol;
  739. newsk->sk_destruct = pipe_destruct;
  740. newpn = pep_sk(newsk);
  741. pn_skb_get_dst_sockaddr(skb, &dst);
  742. pn_skb_get_src_sockaddr(skb, &src);
  743. newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
  744. newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
  745. newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
  746. sock_hold(sk);
  747. newpn->listener = sk;
  748. skb_queue_head_init(&newpn->ctrlreq_queue);
  749. newpn->pipe_handle = pipe_handle;
  750. atomic_set(&newpn->tx_credits, 0);
  751. newpn->ifindex = 0;
  752. newpn->peer_type = peer_type;
  753. newpn->rx_credits = 0;
  754. newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
  755. newpn->init_enable = enabled;
  756. newpn->aligned = aligned;
  757. err = pep_accept_conn(newsk, skb);
  758. if (err) {
  759. sock_put(newsk);
  760. newsk = NULL;
  761. goto drop;
  762. }
  763. sk_add_node(newsk, &pn->hlist);
  764. drop:
  765. release_sock(sk);
  766. kfree_skb(skb);
  767. *errp = err;
  768. return newsk;
  769. }
  770. static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
  771. {
  772. struct pep_sock *pn = pep_sk(sk);
  773. int err;
  774. u8 data[4] = { 0 /* sub-blocks */, PAD, PAD, PAD };
  775. if (pn->pipe_handle == PN_PIPE_INVALID_HANDLE)
  776. pn->pipe_handle = 1; /* anything but INVALID_HANDLE */
  777. err = pipe_handler_request(sk, PNS_PEP_CONNECT_REQ,
  778. pn->init_enable, data, 4);
  779. if (err) {
  780. pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
  781. return err;
  782. }
  783. sk->sk_state = TCP_SYN_SENT;
  784. return 0;
  785. }
  786. static int pep_sock_enable(struct sock *sk, struct sockaddr *addr, int len)
  787. {
  788. int err;
  789. err = pipe_handler_request(sk, PNS_PEP_ENABLE_REQ, PAD,
  790. NULL, 0);
  791. if (err)
  792. return err;
  793. sk->sk_state = TCP_SYN_SENT;
  794. return 0;
  795. }
  796. static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
  797. {
  798. struct pep_sock *pn = pep_sk(sk);
  799. int answ;
  800. int ret = -ENOIOCTLCMD;
  801. switch (cmd) {
  802. case SIOCINQ:
  803. if (sk->sk_state == TCP_LISTEN) {
  804. ret = -EINVAL;
  805. break;
  806. }
  807. lock_sock(sk);
  808. if (sock_flag(sk, SOCK_URGINLINE) &&
  809. !skb_queue_empty(&pn->ctrlreq_queue))
  810. answ = skb_peek(&pn->ctrlreq_queue)->len;
  811. else if (!skb_queue_empty(&sk->sk_receive_queue))
  812. answ = skb_peek(&sk->sk_receive_queue)->len;
  813. else
  814. answ = 0;
  815. release_sock(sk);
  816. ret = put_user(answ, (int __user *)arg);
  817. break;
  818. case SIOCPNENABLEPIPE:
  819. lock_sock(sk);
  820. if (sk->sk_state == TCP_SYN_SENT)
  821. ret = -EBUSY;
  822. else if (sk->sk_state == TCP_ESTABLISHED)
  823. ret = -EISCONN;
  824. else
  825. ret = pep_sock_enable(sk, NULL, 0);
  826. release_sock(sk);
  827. break;
  828. }
  829. return ret;
  830. }
  831. static int pep_init(struct sock *sk)
  832. {
  833. struct pep_sock *pn = pep_sk(sk);
  834. sk->sk_destruct = pipe_destruct;
  835. INIT_HLIST_HEAD(&pn->hlist);
  836. pn->listener = NULL;
  837. skb_queue_head_init(&pn->ctrlreq_queue);
  838. atomic_set(&pn->tx_credits, 0);
  839. pn->ifindex = 0;
  840. pn->peer_type = 0;
  841. pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
  842. pn->rx_credits = 0;
  843. pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
  844. pn->init_enable = 1;
  845. pn->aligned = 0;
  846. return 0;
  847. }
  848. static int pep_setsockopt(struct sock *sk, int level, int optname,
  849. char __user *optval, unsigned int optlen)
  850. {
  851. struct pep_sock *pn = pep_sk(sk);
  852. int val = 0, err = 0;
  853. if (level != SOL_PNPIPE)
  854. return -ENOPROTOOPT;
  855. if (optlen >= sizeof(int)) {
  856. if (get_user(val, (int __user *) optval))
  857. return -EFAULT;
  858. }
  859. lock_sock(sk);
  860. switch (optname) {
  861. case PNPIPE_ENCAP:
  862. if (val && val != PNPIPE_ENCAP_IP) {
  863. err = -EINVAL;
  864. break;
  865. }
  866. if (!pn->ifindex == !val)
  867. break; /* Nothing to do! */
  868. if (!capable(CAP_NET_ADMIN)) {
  869. err = -EPERM;
  870. break;
  871. }
  872. if (val) {
  873. release_sock(sk);
  874. err = gprs_attach(sk);
  875. if (err > 0) {
  876. pn->ifindex = err;
  877. err = 0;
  878. }
  879. } else {
  880. pn->ifindex = 0;
  881. release_sock(sk);
  882. gprs_detach(sk);
  883. err = 0;
  884. }
  885. goto out_norel;
  886. case PNPIPE_HANDLE:
  887. if ((sk->sk_state == TCP_CLOSE) &&
  888. (val >= 0) && (val < PN_PIPE_INVALID_HANDLE))
  889. pn->pipe_handle = val;
  890. else
  891. err = -EINVAL;
  892. break;
  893. case PNPIPE_INITSTATE:
  894. pn->init_enable = !!val;
  895. break;
  896. default:
  897. err = -ENOPROTOOPT;
  898. }
  899. release_sock(sk);
  900. out_norel:
  901. return err;
  902. }
  903. static int pep_getsockopt(struct sock *sk, int level, int optname,
  904. char __user *optval, int __user *optlen)
  905. {
  906. struct pep_sock *pn = pep_sk(sk);
  907. int len, val;
  908. if (level != SOL_PNPIPE)
  909. return -ENOPROTOOPT;
  910. if (get_user(len, optlen))
  911. return -EFAULT;
  912. switch (optname) {
  913. case PNPIPE_ENCAP:
  914. val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
  915. break;
  916. case PNPIPE_IFINDEX:
  917. val = pn->ifindex;
  918. break;
  919. case PNPIPE_HANDLE:
  920. val = pn->pipe_handle;
  921. if (val == PN_PIPE_INVALID_HANDLE)
  922. return -EINVAL;
  923. break;
  924. case PNPIPE_INITSTATE:
  925. val = pn->init_enable;
  926. break;
  927. default:
  928. return -ENOPROTOOPT;
  929. }
  930. len = min_t(unsigned int, sizeof(int), len);
  931. if (put_user(len, optlen))
  932. return -EFAULT;
  933. if (put_user(val, (int __user *) optval))
  934. return -EFAULT;
  935. return 0;
  936. }
  937. static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
  938. {
  939. struct pep_sock *pn = pep_sk(sk);
  940. struct pnpipehdr *ph;
  941. int err;
  942. if (pn_flow_safe(pn->tx_fc) &&
  943. !atomic_add_unless(&pn->tx_credits, -1, 0)) {
  944. kfree_skb(skb);
  945. return -ENOBUFS;
  946. }
  947. skb_push(skb, 3 + pn->aligned);
  948. skb_reset_transport_header(skb);
  949. ph = pnp_hdr(skb);
  950. ph->utid = 0;
  951. if (pn->aligned) {
  952. ph->message_id = PNS_PIPE_ALIGNED_DATA;
  953. ph->data0 = 0; /* padding */
  954. } else
  955. ph->message_id = PNS_PIPE_DATA;
  956. ph->pipe_handle = pn->pipe_handle;
  957. err = pn_skb_send(sk, skb, NULL);
  958. if (err && pn_flow_safe(pn->tx_fc))
  959. atomic_inc(&pn->tx_credits);
  960. return err;
  961. }
  962. static int pep_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  963. {
  964. struct pep_sock *pn = pep_sk(sk);
  965. struct sk_buff *skb;
  966. long timeo;
  967. int flags = msg->msg_flags;
  968. int err, done;
  969. if (len > USHRT_MAX)
  970. return -EMSGSIZE;
  971. if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
  972. MSG_CMSG_COMPAT)) ||
  973. !(msg->msg_flags & MSG_EOR))
  974. return -EOPNOTSUPP;
  975. skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
  976. flags & MSG_DONTWAIT, &err);
  977. if (!skb)
  978. return err;
  979. skb_reserve(skb, MAX_PHONET_HEADER + 3 + pn->aligned);
  980. err = memcpy_from_msg(skb_put(skb, len), msg, len);
  981. if (err < 0)
  982. goto outfree;
  983. lock_sock(sk);
  984. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  985. if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
  986. err = -ENOTCONN;
  987. goto out;
  988. }
  989. if (sk->sk_state != TCP_ESTABLISHED) {
  990. /* Wait until the pipe gets to enabled state */
  991. disabled:
  992. err = sk_stream_wait_connect(sk, &timeo);
  993. if (err)
  994. goto out;
  995. if (sk->sk_state == TCP_CLOSE_WAIT) {
  996. err = -ECONNRESET;
  997. goto out;
  998. }
  999. }
  1000. BUG_ON(sk->sk_state != TCP_ESTABLISHED);
  1001. /* Wait until flow control allows TX */
  1002. done = atomic_read(&pn->tx_credits);
  1003. while (!done) {
  1004. DEFINE_WAIT(wait);
  1005. if (!timeo) {
  1006. err = -EAGAIN;
  1007. goto out;
  1008. }
  1009. if (signal_pending(current)) {
  1010. err = sock_intr_errno(timeo);
  1011. goto out;
  1012. }
  1013. prepare_to_wait(sk_sleep(sk), &wait,
  1014. TASK_INTERRUPTIBLE);
  1015. done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
  1016. finish_wait(sk_sleep(sk), &wait);
  1017. if (sk->sk_state != TCP_ESTABLISHED)
  1018. goto disabled;
  1019. }
  1020. err = pipe_skb_send(sk, skb);
  1021. if (err >= 0)
  1022. err = len; /* success! */
  1023. skb = NULL;
  1024. out:
  1025. release_sock(sk);
  1026. outfree:
  1027. kfree_skb(skb);
  1028. return err;
  1029. }
  1030. int pep_writeable(struct sock *sk)
  1031. {
  1032. struct pep_sock *pn = pep_sk(sk);
  1033. return atomic_read(&pn->tx_credits);
  1034. }
  1035. int pep_write(struct sock *sk, struct sk_buff *skb)
  1036. {
  1037. struct sk_buff *rskb, *fs;
  1038. int flen = 0;
  1039. if (pep_sk(sk)->aligned)
  1040. return pipe_skb_send(sk, skb);
  1041. rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
  1042. if (!rskb) {
  1043. kfree_skb(skb);
  1044. return -ENOMEM;
  1045. }
  1046. skb_shinfo(rskb)->frag_list = skb;
  1047. rskb->len += skb->len;
  1048. rskb->data_len += rskb->len;
  1049. rskb->truesize += rskb->len;
  1050. /* Avoid nested fragments */
  1051. skb_walk_frags(skb, fs)
  1052. flen += fs->len;
  1053. skb->next = skb_shinfo(skb)->frag_list;
  1054. skb_frag_list_init(skb);
  1055. skb->len -= flen;
  1056. skb->data_len -= flen;
  1057. skb->truesize -= flen;
  1058. skb_reserve(rskb, MAX_PHONET_HEADER + 3);
  1059. return pipe_skb_send(sk, rskb);
  1060. }
  1061. struct sk_buff *pep_read(struct sock *sk)
  1062. {
  1063. struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
  1064. if (sk->sk_state == TCP_ESTABLISHED)
  1065. pipe_grant_credits(sk, GFP_ATOMIC);
  1066. return skb;
  1067. }
  1068. static int pep_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  1069. int noblock, int flags, int *addr_len)
  1070. {
  1071. struct sk_buff *skb;
  1072. int err;
  1073. if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
  1074. MSG_NOSIGNAL|MSG_CMSG_COMPAT))
  1075. return -EOPNOTSUPP;
  1076. if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
  1077. return -ENOTCONN;
  1078. if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
  1079. /* Dequeue and acknowledge control request */
  1080. struct pep_sock *pn = pep_sk(sk);
  1081. if (flags & MSG_PEEK)
  1082. return -EOPNOTSUPP;
  1083. skb = skb_dequeue(&pn->ctrlreq_queue);
  1084. if (skb) {
  1085. pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
  1086. GFP_KERNEL);
  1087. msg->msg_flags |= MSG_OOB;
  1088. goto copy;
  1089. }
  1090. if (flags & MSG_OOB)
  1091. return -EINVAL;
  1092. }
  1093. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1094. lock_sock(sk);
  1095. if (skb == NULL) {
  1096. if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
  1097. err = -ECONNRESET;
  1098. release_sock(sk);
  1099. return err;
  1100. }
  1101. if (sk->sk_state == TCP_ESTABLISHED)
  1102. pipe_grant_credits(sk, GFP_KERNEL);
  1103. release_sock(sk);
  1104. copy:
  1105. msg->msg_flags |= MSG_EOR;
  1106. if (skb->len > len)
  1107. msg->msg_flags |= MSG_TRUNC;
  1108. else
  1109. len = skb->len;
  1110. err = skb_copy_datagram_msg(skb, 0, msg, len);
  1111. if (!err)
  1112. err = (flags & MSG_TRUNC) ? skb->len : len;
  1113. skb_free_datagram(sk, skb);
  1114. return err;
  1115. }
  1116. static void pep_sock_unhash(struct sock *sk)
  1117. {
  1118. struct pep_sock *pn = pep_sk(sk);
  1119. struct sock *skparent = NULL;
  1120. lock_sock(sk);
  1121. if (pn->listener != NULL) {
  1122. skparent = pn->listener;
  1123. pn->listener = NULL;
  1124. release_sock(sk);
  1125. pn = pep_sk(skparent);
  1126. lock_sock(skparent);
  1127. sk_del_node_init(sk);
  1128. sk = skparent;
  1129. }
  1130. /* Unhash a listening sock only when it is closed
  1131. * and all of its active connected pipes are closed. */
  1132. if (hlist_empty(&pn->hlist))
  1133. pn_sock_unhash(&pn->pn_sk.sk);
  1134. release_sock(sk);
  1135. if (skparent)
  1136. sock_put(skparent);
  1137. }
  1138. static struct proto pep_proto = {
  1139. .close = pep_sock_close,
  1140. .accept = pep_sock_accept,
  1141. .connect = pep_sock_connect,
  1142. .ioctl = pep_ioctl,
  1143. .init = pep_init,
  1144. .setsockopt = pep_setsockopt,
  1145. .getsockopt = pep_getsockopt,
  1146. .sendmsg = pep_sendmsg,
  1147. .recvmsg = pep_recvmsg,
  1148. .backlog_rcv = pep_do_rcv,
  1149. .hash = pn_sock_hash,
  1150. .unhash = pep_sock_unhash,
  1151. .get_port = pn_sock_get_port,
  1152. .obj_size = sizeof(struct pep_sock),
  1153. .owner = THIS_MODULE,
  1154. .name = "PNPIPE",
  1155. };
  1156. static struct phonet_protocol pep_pn_proto = {
  1157. .ops = &phonet_stream_ops,
  1158. .prot = &pep_proto,
  1159. .sock_type = SOCK_SEQPACKET,
  1160. };
  1161. static int __init pep_register(void)
  1162. {
  1163. return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
  1164. }
  1165. static void __exit pep_unregister(void)
  1166. {
  1167. phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
  1168. }
  1169. module_init(pep_register);
  1170. module_exit(pep_unregister);
  1171. MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
  1172. MODULE_DESCRIPTION("Phonet pipe protocol");
  1173. MODULE_LICENSE("GPL");
  1174. MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);