socket.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * File: socket.c
  3. *
  4. * Phonet sockets
  5. *
  6. * Copyright (C) 2008 Nokia Corporation.
  7. *
  8. * Authors: Sakari Ailus <sakari.ailus@nokia.com>
  9. * Rémi Denis-Courmont
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * version 2 as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA
  24. */
  25. #include <linux/gfp.h>
  26. #include <linux/kernel.h>
  27. #include <linux/net.h>
  28. #include <linux/poll.h>
  29. #include <net/sock.h>
  30. #include <net/tcp_states.h>
  31. #include <linux/phonet.h>
  32. #include <linux/export.h>
  33. #include <net/phonet/phonet.h>
  34. #include <net/phonet/pep.h>
  35. #include <net/phonet/pn_dev.h>
  36. static int pn_socket_release(struct socket *sock)
  37. {
  38. struct sock *sk = sock->sk;
  39. if (sk) {
  40. sock->sk = NULL;
  41. sk->sk_prot->close(sk, 0);
  42. }
  43. return 0;
  44. }
  45. #define PN_HASHSIZE 16
  46. #define PN_HASHMASK (PN_HASHSIZE-1)
  47. static struct {
  48. struct hlist_head hlist[PN_HASHSIZE];
  49. struct mutex lock;
  50. } pnsocks;
  51. void __init pn_sock_init(void)
  52. {
  53. unsigned int i;
  54. for (i = 0; i < PN_HASHSIZE; i++)
  55. INIT_HLIST_HEAD(pnsocks.hlist + i);
  56. mutex_init(&pnsocks.lock);
  57. }
  58. static struct hlist_head *pn_hash_list(u16 obj)
  59. {
  60. return pnsocks.hlist + (obj & PN_HASHMASK);
  61. }
  62. /*
  63. * Find address based on socket address, match only certain fields.
  64. * Also grab sock if it was found. Remember to sock_put it later.
  65. */
  66. struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
  67. {
  68. struct sock *sknode;
  69. struct sock *rval = NULL;
  70. u16 obj = pn_sockaddr_get_object(spn);
  71. u8 res = spn->spn_resource;
  72. struct hlist_head *hlist = pn_hash_list(obj);
  73. rcu_read_lock();
  74. sk_for_each_rcu(sknode, hlist) {
  75. struct pn_sock *pn = pn_sk(sknode);
  76. BUG_ON(!pn->sobject); /* unbound socket */
  77. if (!net_eq(sock_net(sknode), net))
  78. continue;
  79. if (pn_port(obj)) {
  80. /* Look up socket by port */
  81. if (pn_port(pn->sobject) != pn_port(obj))
  82. continue;
  83. } else {
  84. /* If port is zero, look up by resource */
  85. if (pn->resource != res)
  86. continue;
  87. }
  88. if (pn_addr(pn->sobject) &&
  89. pn_addr(pn->sobject) != pn_addr(obj))
  90. continue;
  91. rval = sknode;
  92. sock_hold(sknode);
  93. break;
  94. }
  95. rcu_read_unlock();
  96. return rval;
  97. }
  98. /* Deliver a broadcast packet (only in bottom-half) */
  99. void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)
  100. {
  101. struct hlist_head *hlist = pnsocks.hlist;
  102. unsigned int h;
  103. rcu_read_lock();
  104. for (h = 0; h < PN_HASHSIZE; h++) {
  105. struct sock *sknode;
  106. sk_for_each(sknode, hlist) {
  107. struct sk_buff *clone;
  108. if (!net_eq(sock_net(sknode), net))
  109. continue;
  110. if (!sock_flag(sknode, SOCK_BROADCAST))
  111. continue;
  112. clone = skb_clone(skb, GFP_ATOMIC);
  113. if (clone) {
  114. sock_hold(sknode);
  115. sk_receive_skb(sknode, clone, 0);
  116. }
  117. }
  118. hlist++;
  119. }
  120. rcu_read_unlock();
  121. }
  122. void pn_sock_hash(struct sock *sk)
  123. {
  124. struct hlist_head *hlist = pn_hash_list(pn_sk(sk)->sobject);
  125. mutex_lock(&pnsocks.lock);
  126. sk_add_node_rcu(sk, hlist);
  127. mutex_unlock(&pnsocks.lock);
  128. }
  129. EXPORT_SYMBOL(pn_sock_hash);
  130. void pn_sock_unhash(struct sock *sk)
  131. {
  132. mutex_lock(&pnsocks.lock);
  133. sk_del_node_init_rcu(sk);
  134. mutex_unlock(&pnsocks.lock);
  135. pn_sock_unbind_all_res(sk);
  136. synchronize_rcu();
  137. }
  138. EXPORT_SYMBOL(pn_sock_unhash);
  139. static DEFINE_MUTEX(port_mutex);
  140. static int pn_socket_bind(struct socket *sock, struct sockaddr *addr, int len)
  141. {
  142. struct sock *sk = sock->sk;
  143. struct pn_sock *pn = pn_sk(sk);
  144. struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
  145. int err;
  146. u16 handle;
  147. u8 saddr;
  148. if (sk->sk_prot->bind)
  149. return sk->sk_prot->bind(sk, addr, len);
  150. if (len < sizeof(struct sockaddr_pn))
  151. return -EINVAL;
  152. if (spn->spn_family != AF_PHONET)
  153. return -EAFNOSUPPORT;
  154. handle = pn_sockaddr_get_object((struct sockaddr_pn *)addr);
  155. saddr = pn_addr(handle);
  156. if (saddr && phonet_address_lookup(sock_net(sk), saddr))
  157. return -EADDRNOTAVAIL;
  158. lock_sock(sk);
  159. if (sk->sk_state != TCP_CLOSE || pn_port(pn->sobject)) {
  160. err = -EINVAL; /* attempt to rebind */
  161. goto out;
  162. }
  163. WARN_ON(sk_hashed(sk));
  164. mutex_lock(&port_mutex);
  165. err = sk->sk_prot->get_port(sk, pn_port(handle));
  166. if (err)
  167. goto out_port;
  168. /* get_port() sets the port, bind() sets the address if applicable */
  169. pn->sobject = pn_object(saddr, pn_port(pn->sobject));
  170. pn->resource = spn->spn_resource;
  171. /* Enable RX on the socket */
  172. sk->sk_prot->hash(sk);
  173. out_port:
  174. mutex_unlock(&port_mutex);
  175. out:
  176. release_sock(sk);
  177. return err;
  178. }
  179. static int pn_socket_autobind(struct socket *sock)
  180. {
  181. struct sockaddr_pn sa;
  182. int err;
  183. memset(&sa, 0, sizeof(sa));
  184. sa.spn_family = AF_PHONET;
  185. err = pn_socket_bind(sock, (struct sockaddr *)&sa,
  186. sizeof(struct sockaddr_pn));
  187. if (err != -EINVAL)
  188. return err;
  189. BUG_ON(!pn_port(pn_sk(sock->sk)->sobject));
  190. return 0; /* socket was already bound */
  191. }
  192. static int pn_socket_connect(struct socket *sock, struct sockaddr *addr,
  193. int len, int flags)
  194. {
  195. struct sock *sk = sock->sk;
  196. struct pn_sock *pn = pn_sk(sk);
  197. struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
  198. struct task_struct *tsk = current;
  199. long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  200. int err;
  201. if (pn_socket_autobind(sock))
  202. return -ENOBUFS;
  203. if (len < sizeof(struct sockaddr_pn))
  204. return -EINVAL;
  205. if (spn->spn_family != AF_PHONET)
  206. return -EAFNOSUPPORT;
  207. lock_sock(sk);
  208. switch (sock->state) {
  209. case SS_UNCONNECTED:
  210. if (sk->sk_state != TCP_CLOSE) {
  211. err = -EISCONN;
  212. goto out;
  213. }
  214. break;
  215. case SS_CONNECTING:
  216. err = -EALREADY;
  217. goto out;
  218. default:
  219. err = -EISCONN;
  220. goto out;
  221. }
  222. pn->dobject = pn_sockaddr_get_object(spn);
  223. pn->resource = pn_sockaddr_get_resource(spn);
  224. sock->state = SS_CONNECTING;
  225. err = sk->sk_prot->connect(sk, addr, len);
  226. if (err) {
  227. sock->state = SS_UNCONNECTED;
  228. pn->dobject = 0;
  229. goto out;
  230. }
  231. while (sk->sk_state == TCP_SYN_SENT) {
  232. DEFINE_WAIT(wait);
  233. if (!timeo) {
  234. err = -EINPROGRESS;
  235. goto out;
  236. }
  237. if (signal_pending(tsk)) {
  238. err = sock_intr_errno(timeo);
  239. goto out;
  240. }
  241. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  242. TASK_INTERRUPTIBLE);
  243. release_sock(sk);
  244. timeo = schedule_timeout(timeo);
  245. lock_sock(sk);
  246. finish_wait(sk_sleep(sk), &wait);
  247. }
  248. if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED))
  249. err = 0;
  250. else if (sk->sk_state == TCP_CLOSE_WAIT)
  251. err = -ECONNRESET;
  252. else
  253. err = -ECONNREFUSED;
  254. sock->state = err ? SS_UNCONNECTED : SS_CONNECTED;
  255. out:
  256. release_sock(sk);
  257. return err;
  258. }
  259. static int pn_socket_accept(struct socket *sock, struct socket *newsock,
  260. int flags)
  261. {
  262. struct sock *sk = sock->sk;
  263. struct sock *newsk;
  264. int err;
  265. if (unlikely(sk->sk_state != TCP_LISTEN))
  266. return -EINVAL;
  267. newsk = sk->sk_prot->accept(sk, flags, &err);
  268. if (!newsk)
  269. return err;
  270. lock_sock(newsk);
  271. sock_graft(newsk, newsock);
  272. newsock->state = SS_CONNECTED;
  273. release_sock(newsk);
  274. return 0;
  275. }
  276. static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
  277. int *sockaddr_len, int peer)
  278. {
  279. struct sock *sk = sock->sk;
  280. struct pn_sock *pn = pn_sk(sk);
  281. memset(addr, 0, sizeof(struct sockaddr_pn));
  282. addr->sa_family = AF_PHONET;
  283. if (!peer) /* Race with bind() here is userland's problem. */
  284. pn_sockaddr_set_object((struct sockaddr_pn *)addr,
  285. pn->sobject);
  286. *sockaddr_len = sizeof(struct sockaddr_pn);
  287. return 0;
  288. }
  289. static unsigned int pn_socket_poll(struct file *file, struct socket *sock,
  290. poll_table *wait)
  291. {
  292. struct sock *sk = sock->sk;
  293. struct pep_sock *pn = pep_sk(sk);
  294. unsigned int mask = 0;
  295. poll_wait(file, sk_sleep(sk), wait);
  296. if (sk->sk_state == TCP_CLOSE)
  297. return POLLERR;
  298. if (!skb_queue_empty(&sk->sk_receive_queue))
  299. mask |= POLLIN | POLLRDNORM;
  300. if (!skb_queue_empty(&pn->ctrlreq_queue))
  301. mask |= POLLPRI;
  302. if (!mask && sk->sk_state == TCP_CLOSE_WAIT)
  303. return POLLHUP;
  304. if (sk->sk_state == TCP_ESTABLISHED &&
  305. atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf &&
  306. atomic_read(&pn->tx_credits))
  307. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  308. return mask;
  309. }
  310. static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
  311. unsigned long arg)
  312. {
  313. struct sock *sk = sock->sk;
  314. struct pn_sock *pn = pn_sk(sk);
  315. if (cmd == SIOCPNGETOBJECT) {
  316. struct net_device *dev;
  317. u16 handle;
  318. u8 saddr;
  319. if (get_user(handle, (__u16 __user *)arg))
  320. return -EFAULT;
  321. lock_sock(sk);
  322. if (sk->sk_bound_dev_if)
  323. dev = dev_get_by_index(sock_net(sk),
  324. sk->sk_bound_dev_if);
  325. else
  326. dev = phonet_device_get(sock_net(sk));
  327. if (dev && (dev->flags & IFF_UP))
  328. saddr = phonet_address_get(dev, pn_addr(handle));
  329. else
  330. saddr = PN_NO_ADDR;
  331. release_sock(sk);
  332. if (dev)
  333. dev_put(dev);
  334. if (saddr == PN_NO_ADDR)
  335. return -EHOSTUNREACH;
  336. handle = pn_object(saddr, pn_port(pn->sobject));
  337. return put_user(handle, (__u16 __user *)arg);
  338. }
  339. return sk->sk_prot->ioctl(sk, cmd, arg);
  340. }
  341. static int pn_socket_listen(struct socket *sock, int backlog)
  342. {
  343. struct sock *sk = sock->sk;
  344. int err = 0;
  345. if (pn_socket_autobind(sock))
  346. return -ENOBUFS;
  347. lock_sock(sk);
  348. if (sock->state != SS_UNCONNECTED) {
  349. err = -EINVAL;
  350. goto out;
  351. }
  352. if (sk->sk_state != TCP_LISTEN) {
  353. sk->sk_state = TCP_LISTEN;
  354. sk->sk_ack_backlog = 0;
  355. }
  356. sk->sk_max_ack_backlog = backlog;
  357. out:
  358. release_sock(sk);
  359. return err;
  360. }
  361. static int pn_socket_sendmsg(struct socket *sock, struct msghdr *m,
  362. size_t total_len)
  363. {
  364. struct sock *sk = sock->sk;
  365. if (pn_socket_autobind(sock))
  366. return -EAGAIN;
  367. return sk->sk_prot->sendmsg(sk, m, total_len);
  368. }
  369. const struct proto_ops phonet_dgram_ops = {
  370. .family = AF_PHONET,
  371. .owner = THIS_MODULE,
  372. .release = pn_socket_release,
  373. .bind = pn_socket_bind,
  374. .connect = sock_no_connect,
  375. .socketpair = sock_no_socketpair,
  376. .accept = sock_no_accept,
  377. .getname = pn_socket_getname,
  378. .poll = datagram_poll,
  379. .ioctl = pn_socket_ioctl,
  380. .listen = sock_no_listen,
  381. .shutdown = sock_no_shutdown,
  382. .setsockopt = sock_no_setsockopt,
  383. .getsockopt = sock_no_getsockopt,
  384. #ifdef CONFIG_COMPAT
  385. .compat_setsockopt = sock_no_setsockopt,
  386. .compat_getsockopt = sock_no_getsockopt,
  387. #endif
  388. .sendmsg = pn_socket_sendmsg,
  389. .recvmsg = sock_common_recvmsg,
  390. .mmap = sock_no_mmap,
  391. .sendpage = sock_no_sendpage,
  392. };
  393. const struct proto_ops phonet_stream_ops = {
  394. .family = AF_PHONET,
  395. .owner = THIS_MODULE,
  396. .release = pn_socket_release,
  397. .bind = pn_socket_bind,
  398. .connect = pn_socket_connect,
  399. .socketpair = sock_no_socketpair,
  400. .accept = pn_socket_accept,
  401. .getname = pn_socket_getname,
  402. .poll = pn_socket_poll,
  403. .ioctl = pn_socket_ioctl,
  404. .listen = pn_socket_listen,
  405. .shutdown = sock_no_shutdown,
  406. .setsockopt = sock_common_setsockopt,
  407. .getsockopt = sock_common_getsockopt,
  408. #ifdef CONFIG_COMPAT
  409. .compat_setsockopt = compat_sock_common_setsockopt,
  410. .compat_getsockopt = compat_sock_common_getsockopt,
  411. #endif
  412. .sendmsg = pn_socket_sendmsg,
  413. .recvmsg = sock_common_recvmsg,
  414. .mmap = sock_no_mmap,
  415. .sendpage = sock_no_sendpage,
  416. };
  417. EXPORT_SYMBOL(phonet_stream_ops);
  418. /* allocate port for a socket */
  419. int pn_sock_get_port(struct sock *sk, unsigned short sport)
  420. {
  421. static int port_cur;
  422. struct net *net = sock_net(sk);
  423. struct pn_sock *pn = pn_sk(sk);
  424. struct sockaddr_pn try_sa;
  425. struct sock *tmpsk;
  426. memset(&try_sa, 0, sizeof(struct sockaddr_pn));
  427. try_sa.spn_family = AF_PHONET;
  428. WARN_ON(!mutex_is_locked(&port_mutex));
  429. if (!sport) {
  430. /* search free port */
  431. int port, pmin, pmax;
  432. phonet_get_local_port_range(&pmin, &pmax);
  433. for (port = pmin; port <= pmax; port++) {
  434. port_cur++;
  435. if (port_cur < pmin || port_cur > pmax)
  436. port_cur = pmin;
  437. pn_sockaddr_set_port(&try_sa, port_cur);
  438. tmpsk = pn_find_sock_by_sa(net, &try_sa);
  439. if (tmpsk == NULL) {
  440. sport = port_cur;
  441. goto found;
  442. } else
  443. sock_put(tmpsk);
  444. }
  445. } else {
  446. /* try to find specific port */
  447. pn_sockaddr_set_port(&try_sa, sport);
  448. tmpsk = pn_find_sock_by_sa(net, &try_sa);
  449. if (tmpsk == NULL)
  450. /* No sock there! We can use that port... */
  451. goto found;
  452. else
  453. sock_put(tmpsk);
  454. }
  455. /* the port must be in use already */
  456. return -EADDRINUSE;
  457. found:
  458. pn->sobject = pn_object(pn_addr(pn->sobject), sport);
  459. return 0;
  460. }
  461. EXPORT_SYMBOL(pn_sock_get_port);
  462. #ifdef CONFIG_PROC_FS
  463. static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos)
  464. {
  465. struct net *net = seq_file_net(seq);
  466. struct hlist_head *hlist = pnsocks.hlist;
  467. struct sock *sknode;
  468. unsigned int h;
  469. for (h = 0; h < PN_HASHSIZE; h++) {
  470. sk_for_each_rcu(sknode, hlist) {
  471. if (!net_eq(net, sock_net(sknode)))
  472. continue;
  473. if (!pos)
  474. return sknode;
  475. pos--;
  476. }
  477. hlist++;
  478. }
  479. return NULL;
  480. }
  481. static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk)
  482. {
  483. struct net *net = seq_file_net(seq);
  484. do
  485. sk = sk_next(sk);
  486. while (sk && !net_eq(net, sock_net(sk)));
  487. return sk;
  488. }
  489. static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos)
  490. __acquires(rcu)
  491. {
  492. rcu_read_lock();
  493. return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  494. }
  495. static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  496. {
  497. struct sock *sk;
  498. if (v == SEQ_START_TOKEN)
  499. sk = pn_sock_get_idx(seq, 0);
  500. else
  501. sk = pn_sock_get_next(seq, v);
  502. (*pos)++;
  503. return sk;
  504. }
  505. static void pn_sock_seq_stop(struct seq_file *seq, void *v)
  506. __releases(rcu)
  507. {
  508. rcu_read_unlock();
  509. }
  510. static int pn_sock_seq_show(struct seq_file *seq, void *v)
  511. {
  512. seq_setwidth(seq, 127);
  513. if (v == SEQ_START_TOKEN)
  514. seq_puts(seq, "pt loc rem rs st tx_queue rx_queue "
  515. " uid inode ref pointer drops");
  516. else {
  517. struct sock *sk = v;
  518. struct pn_sock *pn = pn_sk(sk);
  519. seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
  520. "%d %pK %d",
  521. sk->sk_protocol, pn->sobject, pn->dobject,
  522. pn->resource, sk->sk_state,
  523. sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
  524. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
  525. sock_i_ino(sk),
  526. atomic_read(&sk->sk_refcnt), sk,
  527. atomic_read(&sk->sk_drops));
  528. }
  529. seq_pad(seq, '\n');
  530. return 0;
  531. }
  532. static const struct seq_operations pn_sock_seq_ops = {
  533. .start = pn_sock_seq_start,
  534. .next = pn_sock_seq_next,
  535. .stop = pn_sock_seq_stop,
  536. .show = pn_sock_seq_show,
  537. };
  538. static int pn_sock_open(struct inode *inode, struct file *file)
  539. {
  540. return seq_open_net(inode, file, &pn_sock_seq_ops,
  541. sizeof(struct seq_net_private));
  542. }
  543. const struct file_operations pn_sock_seq_fops = {
  544. .owner = THIS_MODULE,
  545. .open = pn_sock_open,
  546. .read = seq_read,
  547. .llseek = seq_lseek,
  548. .release = seq_release_net,
  549. };
  550. #endif
  551. static struct {
  552. struct sock *sk[256];
  553. } pnres;
  554. /*
  555. * Find and hold socket based on resource.
  556. */
  557. struct sock *pn_find_sock_by_res(struct net *net, u8 res)
  558. {
  559. struct sock *sk;
  560. if (!net_eq(net, &init_net))
  561. return NULL;
  562. rcu_read_lock();
  563. sk = rcu_dereference(pnres.sk[res]);
  564. if (sk)
  565. sock_hold(sk);
  566. rcu_read_unlock();
  567. return sk;
  568. }
  569. static DEFINE_MUTEX(resource_mutex);
  570. int pn_sock_bind_res(struct sock *sk, u8 res)
  571. {
  572. int ret = -EADDRINUSE;
  573. if (!net_eq(sock_net(sk), &init_net))
  574. return -ENOIOCTLCMD;
  575. if (!capable(CAP_SYS_ADMIN))
  576. return -EPERM;
  577. if (pn_socket_autobind(sk->sk_socket))
  578. return -EAGAIN;
  579. mutex_lock(&resource_mutex);
  580. if (pnres.sk[res] == NULL) {
  581. sock_hold(sk);
  582. rcu_assign_pointer(pnres.sk[res], sk);
  583. ret = 0;
  584. }
  585. mutex_unlock(&resource_mutex);
  586. return ret;
  587. }
  588. int pn_sock_unbind_res(struct sock *sk, u8 res)
  589. {
  590. int ret = -ENOENT;
  591. if (!capable(CAP_SYS_ADMIN))
  592. return -EPERM;
  593. mutex_lock(&resource_mutex);
  594. if (pnres.sk[res] == sk) {
  595. RCU_INIT_POINTER(pnres.sk[res], NULL);
  596. ret = 0;
  597. }
  598. mutex_unlock(&resource_mutex);
  599. if (ret == 0) {
  600. synchronize_rcu();
  601. sock_put(sk);
  602. }
  603. return ret;
  604. }
  605. void pn_sock_unbind_all_res(struct sock *sk)
  606. {
  607. unsigned int res, match = 0;
  608. mutex_lock(&resource_mutex);
  609. for (res = 0; res < 256; res++) {
  610. if (pnres.sk[res] == sk) {
  611. RCU_INIT_POINTER(pnres.sk[res], NULL);
  612. match++;
  613. }
  614. }
  615. mutex_unlock(&resource_mutex);
  616. while (match > 0) {
  617. __sock_put(sk);
  618. match--;
  619. }
  620. /* Caller is responsible for RCU sync before final sock_put() */
  621. }
  622. #ifdef CONFIG_PROC_FS
  623. static struct sock **pn_res_get_idx(struct seq_file *seq, loff_t pos)
  624. {
  625. struct net *net = seq_file_net(seq);
  626. unsigned int i;
  627. if (!net_eq(net, &init_net))
  628. return NULL;
  629. for (i = 0; i < 256; i++) {
  630. if (pnres.sk[i] == NULL)
  631. continue;
  632. if (!pos)
  633. return pnres.sk + i;
  634. pos--;
  635. }
  636. return NULL;
  637. }
  638. static struct sock **pn_res_get_next(struct seq_file *seq, struct sock **sk)
  639. {
  640. struct net *net = seq_file_net(seq);
  641. unsigned int i;
  642. BUG_ON(!net_eq(net, &init_net));
  643. for (i = (sk - pnres.sk) + 1; i < 256; i++)
  644. if (pnres.sk[i])
  645. return pnres.sk + i;
  646. return NULL;
  647. }
  648. static void *pn_res_seq_start(struct seq_file *seq, loff_t *pos)
  649. __acquires(resource_mutex)
  650. {
  651. mutex_lock(&resource_mutex);
  652. return *pos ? pn_res_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  653. }
  654. static void *pn_res_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  655. {
  656. struct sock **sk;
  657. if (v == SEQ_START_TOKEN)
  658. sk = pn_res_get_idx(seq, 0);
  659. else
  660. sk = pn_res_get_next(seq, v);
  661. (*pos)++;
  662. return sk;
  663. }
  664. static void pn_res_seq_stop(struct seq_file *seq, void *v)
  665. __releases(resource_mutex)
  666. {
  667. mutex_unlock(&resource_mutex);
  668. }
  669. static int pn_res_seq_show(struct seq_file *seq, void *v)
  670. {
  671. seq_setwidth(seq, 63);
  672. if (v == SEQ_START_TOKEN)
  673. seq_puts(seq, "rs uid inode");
  674. else {
  675. struct sock **psk = v;
  676. struct sock *sk = *psk;
  677. seq_printf(seq, "%02X %5u %lu",
  678. (int) (psk - pnres.sk),
  679. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
  680. sock_i_ino(sk));
  681. }
  682. seq_pad(seq, '\n');
  683. return 0;
  684. }
  685. static const struct seq_operations pn_res_seq_ops = {
  686. .start = pn_res_seq_start,
  687. .next = pn_res_seq_next,
  688. .stop = pn_res_seq_stop,
  689. .show = pn_res_seq_show,
  690. };
  691. static int pn_res_open(struct inode *inode, struct file *file)
  692. {
  693. return seq_open_net(inode, file, &pn_res_seq_ops,
  694. sizeof(struct seq_net_private));
  695. }
  696. const struct file_operations pn_res_seq_fops = {
  697. .owner = THIS_MODULE,
  698. .open = pn_res_open,
  699. .read = seq_read,
  700. .llseek = seq_lseek,
  701. .release = seq_release_net,
  702. };
  703. #endif