pep.c 29 KB

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