socket.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. *
  3. * Author Karsten Keil <kkeil@novell.com>
  4. *
  5. * Copyright 2008 by Karsten Keil <kkeil@novell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/mISDNif.h>
  18. #include <linux/slab.h>
  19. #include "core.h"
  20. static u_int *debug;
  21. static struct proto mISDN_proto = {
  22. .name = "misdn",
  23. .owner = THIS_MODULE,
  24. .obj_size = sizeof(struct mISDN_sock)
  25. };
  26. #define _pms(sk) ((struct mISDN_sock *)sk)
  27. static struct mISDN_sock_list data_sockets = {
  28. .lock = __RW_LOCK_UNLOCKED(data_sockets.lock)
  29. };
  30. static struct mISDN_sock_list base_sockets = {
  31. .lock = __RW_LOCK_UNLOCKED(base_sockets.lock)
  32. };
  33. #define L2_HEADER_LEN 4
  34. static inline struct sk_buff *
  35. _l2_alloc_skb(unsigned int len, gfp_t gfp_mask)
  36. {
  37. struct sk_buff *skb;
  38. skb = alloc_skb(len + L2_HEADER_LEN, gfp_mask);
  39. if (likely(skb))
  40. skb_reserve(skb, L2_HEADER_LEN);
  41. return skb;
  42. }
  43. static void
  44. mISDN_sock_link(struct mISDN_sock_list *l, struct sock *sk)
  45. {
  46. write_lock_bh(&l->lock);
  47. sk_add_node(sk, &l->head);
  48. write_unlock_bh(&l->lock);
  49. }
  50. static void mISDN_sock_unlink(struct mISDN_sock_list *l, struct sock *sk)
  51. {
  52. write_lock_bh(&l->lock);
  53. sk_del_node_init(sk);
  54. write_unlock_bh(&l->lock);
  55. }
  56. static int
  57. mISDN_send(struct mISDNchannel *ch, struct sk_buff *skb)
  58. {
  59. struct mISDN_sock *msk;
  60. int err;
  61. msk = container_of(ch, struct mISDN_sock, ch);
  62. if (*debug & DEBUG_SOCKET)
  63. printk(KERN_DEBUG "%s len %d %p\n", __func__, skb->len, skb);
  64. if (msk->sk.sk_state == MISDN_CLOSED)
  65. return -EUNATCH;
  66. __net_timestamp(skb);
  67. err = sock_queue_rcv_skb(&msk->sk, skb);
  68. if (err)
  69. printk(KERN_WARNING "%s: error %d\n", __func__, err);
  70. return err;
  71. }
  72. static int
  73. mISDN_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  74. {
  75. struct mISDN_sock *msk;
  76. msk = container_of(ch, struct mISDN_sock, ch);
  77. if (*debug & DEBUG_SOCKET)
  78. printk(KERN_DEBUG "%s(%p, %x, %p)\n", __func__, ch, cmd, arg);
  79. switch (cmd) {
  80. case CLOSE_CHANNEL:
  81. msk->sk.sk_state = MISDN_CLOSED;
  82. break;
  83. }
  84. return 0;
  85. }
  86. static inline void
  87. mISDN_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
  88. {
  89. struct timeval tv;
  90. if (_pms(sk)->cmask & MISDN_TIME_STAMP) {
  91. skb_get_timestamp(skb, &tv);
  92. put_cmsg(msg, SOL_MISDN, MISDN_TIME_STAMP, sizeof(tv), &tv);
  93. }
  94. }
  95. static int
  96. mISDN_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  97. struct msghdr *msg, size_t len, int flags)
  98. {
  99. struct sk_buff *skb;
  100. struct sock *sk = sock->sk;
  101. struct sockaddr_mISDN *maddr;
  102. int copied, err;
  103. if (*debug & DEBUG_SOCKET)
  104. printk(KERN_DEBUG "%s: len %d, flags %x ch.nr %d, proto %x\n",
  105. __func__, (int)len, flags, _pms(sk)->ch.nr,
  106. sk->sk_protocol);
  107. if (flags & (MSG_OOB))
  108. return -EOPNOTSUPP;
  109. if (sk->sk_state == MISDN_CLOSED)
  110. return 0;
  111. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
  112. if (!skb)
  113. return err;
  114. if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
  115. msg->msg_namelen = sizeof(struct sockaddr_mISDN);
  116. maddr = (struct sockaddr_mISDN *)msg->msg_name;
  117. maddr->family = AF_ISDN;
  118. maddr->dev = _pms(sk)->dev->id;
  119. if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
  120. (sk->sk_protocol == ISDN_P_LAPD_NT)) {
  121. maddr->channel = (mISDN_HEAD_ID(skb) >> 16) & 0xff;
  122. maddr->tei = (mISDN_HEAD_ID(skb) >> 8) & 0xff;
  123. maddr->sapi = mISDN_HEAD_ID(skb) & 0xff;
  124. } else {
  125. maddr->channel = _pms(sk)->ch.nr;
  126. maddr->sapi = _pms(sk)->ch.addr & 0xFF;
  127. maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xFF;
  128. }
  129. } else {
  130. if (msg->msg_namelen)
  131. printk(KERN_WARNING "%s: too small namelen %d\n",
  132. __func__, msg->msg_namelen);
  133. msg->msg_namelen = 0;
  134. }
  135. copied = skb->len + MISDN_HEADER_LEN;
  136. if (len < copied) {
  137. if (flags & MSG_PEEK)
  138. atomic_dec(&skb->users);
  139. else
  140. skb_queue_head(&sk->sk_receive_queue, skb);
  141. return -ENOSPC;
  142. }
  143. memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
  144. MISDN_HEADER_LEN);
  145. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  146. mISDN_sock_cmsg(sk, msg, skb);
  147. skb_free_datagram(sk, skb);
  148. return err ? : copied;
  149. }
  150. static int
  151. mISDN_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  152. struct msghdr *msg, size_t len)
  153. {
  154. struct sock *sk = sock->sk;
  155. struct sk_buff *skb;
  156. int err = -ENOMEM;
  157. struct sockaddr_mISDN *maddr;
  158. if (*debug & DEBUG_SOCKET)
  159. printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
  160. __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
  161. sk->sk_protocol);
  162. if (msg->msg_flags & MSG_OOB)
  163. return -EOPNOTSUPP;
  164. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
  165. return -EINVAL;
  166. if (len < MISDN_HEADER_LEN)
  167. return -EINVAL;
  168. if (sk->sk_state != MISDN_BOUND)
  169. return -EBADFD;
  170. lock_sock(sk);
  171. skb = _l2_alloc_skb(len, GFP_KERNEL);
  172. if (!skb)
  173. goto done;
  174. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  175. err = -EFAULT;
  176. goto done;
  177. }
  178. memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
  179. skb_pull(skb, MISDN_HEADER_LEN);
  180. if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
  181. /* if we have a address, we use it */
  182. maddr = (struct sockaddr_mISDN *)msg->msg_name;
  183. mISDN_HEAD_ID(skb) = maddr->channel;
  184. } else { /* use default for L2 messages */
  185. if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
  186. (sk->sk_protocol == ISDN_P_LAPD_NT))
  187. mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
  188. }
  189. if (*debug & DEBUG_SOCKET)
  190. printk(KERN_DEBUG "%s: ID:%x\n",
  191. __func__, mISDN_HEAD_ID(skb));
  192. err = -ENODEV;
  193. if (!_pms(sk)->ch.peer)
  194. goto done;
  195. err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
  196. if (err)
  197. goto done;
  198. else {
  199. skb = NULL;
  200. err = len;
  201. }
  202. done:
  203. if (skb)
  204. kfree_skb(skb);
  205. release_sock(sk);
  206. return err;
  207. }
  208. static int
  209. data_sock_release(struct socket *sock)
  210. {
  211. struct sock *sk = sock->sk;
  212. if (*debug & DEBUG_SOCKET)
  213. printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
  214. if (!sk)
  215. return 0;
  216. switch (sk->sk_protocol) {
  217. case ISDN_P_TE_S0:
  218. case ISDN_P_NT_S0:
  219. case ISDN_P_TE_E1:
  220. case ISDN_P_NT_E1:
  221. if (sk->sk_state == MISDN_BOUND)
  222. delete_channel(&_pms(sk)->ch);
  223. else
  224. mISDN_sock_unlink(&data_sockets, sk);
  225. break;
  226. case ISDN_P_LAPD_TE:
  227. case ISDN_P_LAPD_NT:
  228. case ISDN_P_B_RAW:
  229. case ISDN_P_B_HDLC:
  230. case ISDN_P_B_X75SLP:
  231. case ISDN_P_B_L2DTMF:
  232. case ISDN_P_B_L2DSP:
  233. case ISDN_P_B_L2DSPHDLC:
  234. delete_channel(&_pms(sk)->ch);
  235. mISDN_sock_unlink(&data_sockets, sk);
  236. break;
  237. }
  238. lock_sock(sk);
  239. sock_orphan(sk);
  240. skb_queue_purge(&sk->sk_receive_queue);
  241. release_sock(sk);
  242. sock_put(sk);
  243. return 0;
  244. }
  245. static int
  246. data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
  247. {
  248. struct mISDN_ctrl_req cq;
  249. int err = -EINVAL, val[2];
  250. struct mISDNchannel *bchan, *next;
  251. lock_sock(sk);
  252. if (!_pms(sk)->dev) {
  253. err = -ENODEV;
  254. goto done;
  255. }
  256. switch (cmd) {
  257. case IMCTRLREQ:
  258. if (copy_from_user(&cq, p, sizeof(cq))) {
  259. err = -EFAULT;
  260. break;
  261. }
  262. if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
  263. list_for_each_entry_safe(bchan, next,
  264. &_pms(sk)->dev->bchannels, list) {
  265. if (bchan->nr == cq.channel) {
  266. err = bchan->ctrl(bchan,
  267. CONTROL_CHANNEL, &cq);
  268. break;
  269. }
  270. }
  271. } else
  272. err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
  273. CONTROL_CHANNEL, &cq);
  274. if (err)
  275. break;
  276. if (copy_to_user(p, &cq, sizeof(cq)))
  277. err = -EFAULT;
  278. break;
  279. case IMCLEAR_L2:
  280. if (sk->sk_protocol != ISDN_P_LAPD_NT) {
  281. err = -EINVAL;
  282. break;
  283. }
  284. val[0] = cmd;
  285. if (get_user(val[1], (int __user *)p)) {
  286. err = -EFAULT;
  287. break;
  288. }
  289. err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
  290. CONTROL_CHANNEL, val);
  291. break;
  292. case IMHOLD_L1:
  293. if (sk->sk_protocol != ISDN_P_LAPD_NT
  294. && sk->sk_protocol != ISDN_P_LAPD_TE) {
  295. err = -EINVAL;
  296. break;
  297. }
  298. val[0] = cmd;
  299. if (get_user(val[1], (int __user *)p)) {
  300. err = -EFAULT;
  301. break;
  302. }
  303. err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
  304. CONTROL_CHANNEL, val);
  305. break;
  306. default:
  307. err = -EINVAL;
  308. break;
  309. }
  310. done:
  311. release_sock(sk);
  312. return err;
  313. }
  314. static int
  315. data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  316. {
  317. int err = 0, id;
  318. struct sock *sk = sock->sk;
  319. struct mISDNdevice *dev;
  320. struct mISDNversion ver;
  321. switch (cmd) {
  322. case IMGETVERSION:
  323. ver.major = MISDN_MAJOR_VERSION;
  324. ver.minor = MISDN_MINOR_VERSION;
  325. ver.release = MISDN_RELEASE;
  326. if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
  327. err = -EFAULT;
  328. break;
  329. case IMGETCOUNT:
  330. id = get_mdevice_count();
  331. if (put_user(id, (int __user *)arg))
  332. err = -EFAULT;
  333. break;
  334. case IMGETDEVINFO:
  335. if (get_user(id, (int __user *)arg)) {
  336. err = -EFAULT;
  337. break;
  338. }
  339. dev = get_mdevice(id);
  340. if (dev) {
  341. struct mISDN_devinfo di;
  342. memset(&di, 0, sizeof(di));
  343. di.id = dev->id;
  344. di.Dprotocols = dev->Dprotocols;
  345. di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
  346. di.protocol = dev->D.protocol;
  347. memcpy(di.channelmap, dev->channelmap,
  348. sizeof(di.channelmap));
  349. di.nrbchan = dev->nrbchan;
  350. strcpy(di.name, dev_name(&dev->dev));
  351. if (copy_to_user((void __user *)arg, &di, sizeof(di)))
  352. err = -EFAULT;
  353. } else
  354. err = -ENODEV;
  355. break;
  356. default:
  357. if (sk->sk_state == MISDN_BOUND)
  358. err = data_sock_ioctl_bound(sk, cmd,
  359. (void __user *)arg);
  360. else
  361. err = -ENOTCONN;
  362. }
  363. return err;
  364. }
  365. static int data_sock_setsockopt(struct socket *sock, int level, int optname,
  366. char __user *optval, unsigned int len)
  367. {
  368. struct sock *sk = sock->sk;
  369. int err = 0, opt = 0;
  370. if (*debug & DEBUG_SOCKET)
  371. printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
  372. level, optname, optval, len);
  373. lock_sock(sk);
  374. switch (optname) {
  375. case MISDN_TIME_STAMP:
  376. if (get_user(opt, (int __user *)optval)) {
  377. err = -EFAULT;
  378. break;
  379. }
  380. if (opt)
  381. _pms(sk)->cmask |= MISDN_TIME_STAMP;
  382. else
  383. _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
  384. break;
  385. default:
  386. err = -ENOPROTOOPT;
  387. break;
  388. }
  389. release_sock(sk);
  390. return err;
  391. }
  392. static int data_sock_getsockopt(struct socket *sock, int level, int optname,
  393. char __user *optval, int __user *optlen)
  394. {
  395. struct sock *sk = sock->sk;
  396. int len, opt;
  397. if (get_user(len, optlen))
  398. return -EFAULT;
  399. if (len != sizeof(char))
  400. return -EINVAL;
  401. switch (optname) {
  402. case MISDN_TIME_STAMP:
  403. if (_pms(sk)->cmask & MISDN_TIME_STAMP)
  404. opt = 1;
  405. else
  406. opt = 0;
  407. if (put_user(opt, optval))
  408. return -EFAULT;
  409. break;
  410. default:
  411. return -ENOPROTOOPT;
  412. }
  413. return 0;
  414. }
  415. static int
  416. data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  417. {
  418. struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
  419. struct sock *sk = sock->sk;
  420. struct hlist_node *node;
  421. struct sock *csk;
  422. int err = 0;
  423. if (*debug & DEBUG_SOCKET)
  424. printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
  425. if (addr_len != sizeof(struct sockaddr_mISDN))
  426. return -EINVAL;
  427. if (!maddr || maddr->family != AF_ISDN)
  428. return -EINVAL;
  429. lock_sock(sk);
  430. if (_pms(sk)->dev) {
  431. err = -EALREADY;
  432. goto done;
  433. }
  434. _pms(sk)->dev = get_mdevice(maddr->dev);
  435. if (!_pms(sk)->dev) {
  436. err = -ENODEV;
  437. goto done;
  438. }
  439. if (sk->sk_protocol < ISDN_P_B_START) {
  440. read_lock_bh(&data_sockets.lock);
  441. sk_for_each(csk, node, &data_sockets.head) {
  442. if (sk == csk)
  443. continue;
  444. if (_pms(csk)->dev != _pms(sk)->dev)
  445. continue;
  446. if (csk->sk_protocol >= ISDN_P_B_START)
  447. continue;
  448. if (IS_ISDN_P_TE(csk->sk_protocol)
  449. == IS_ISDN_P_TE(sk->sk_protocol))
  450. continue;
  451. read_unlock_bh(&data_sockets.lock);
  452. err = -EBUSY;
  453. goto done;
  454. }
  455. read_unlock_bh(&data_sockets.lock);
  456. }
  457. _pms(sk)->ch.send = mISDN_send;
  458. _pms(sk)->ch.ctrl = mISDN_ctrl;
  459. switch (sk->sk_protocol) {
  460. case ISDN_P_TE_S0:
  461. case ISDN_P_NT_S0:
  462. case ISDN_P_TE_E1:
  463. case ISDN_P_NT_E1:
  464. mISDN_sock_unlink(&data_sockets, sk);
  465. err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
  466. sk->sk_protocol, maddr);
  467. if (err)
  468. mISDN_sock_link(&data_sockets, sk);
  469. break;
  470. case ISDN_P_LAPD_TE:
  471. case ISDN_P_LAPD_NT:
  472. err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
  473. sk->sk_protocol, maddr);
  474. break;
  475. case ISDN_P_B_RAW:
  476. case ISDN_P_B_HDLC:
  477. case ISDN_P_B_X75SLP:
  478. case ISDN_P_B_L2DTMF:
  479. case ISDN_P_B_L2DSP:
  480. case ISDN_P_B_L2DSPHDLC:
  481. err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
  482. sk->sk_protocol, maddr);
  483. break;
  484. default:
  485. err = -EPROTONOSUPPORT;
  486. }
  487. if (err)
  488. goto done;
  489. sk->sk_state = MISDN_BOUND;
  490. _pms(sk)->ch.protocol = sk->sk_protocol;
  491. done:
  492. release_sock(sk);
  493. return err;
  494. }
  495. static int
  496. data_sock_getname(struct socket *sock, struct sockaddr *addr,
  497. int *addr_len, int peer)
  498. {
  499. struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
  500. struct sock *sk = sock->sk;
  501. if (!_pms(sk)->dev)
  502. return -EBADFD;
  503. lock_sock(sk);
  504. *addr_len = sizeof(*maddr);
  505. maddr->dev = _pms(sk)->dev->id;
  506. maddr->channel = _pms(sk)->ch.nr;
  507. maddr->sapi = _pms(sk)->ch.addr & 0xff;
  508. maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
  509. release_sock(sk);
  510. return 0;
  511. }
  512. static const struct proto_ops data_sock_ops = {
  513. .family = PF_ISDN,
  514. .owner = THIS_MODULE,
  515. .release = data_sock_release,
  516. .ioctl = data_sock_ioctl,
  517. .bind = data_sock_bind,
  518. .getname = data_sock_getname,
  519. .sendmsg = mISDN_sock_sendmsg,
  520. .recvmsg = mISDN_sock_recvmsg,
  521. .poll = datagram_poll,
  522. .listen = sock_no_listen,
  523. .shutdown = sock_no_shutdown,
  524. .setsockopt = data_sock_setsockopt,
  525. .getsockopt = data_sock_getsockopt,
  526. .connect = sock_no_connect,
  527. .socketpair = sock_no_socketpair,
  528. .accept = sock_no_accept,
  529. .mmap = sock_no_mmap
  530. };
  531. static int
  532. data_sock_create(struct net *net, struct socket *sock, int protocol)
  533. {
  534. struct sock *sk;
  535. if (sock->type != SOCK_DGRAM)
  536. return -ESOCKTNOSUPPORT;
  537. sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
  538. if (!sk)
  539. return -ENOMEM;
  540. sock_init_data(sock, sk);
  541. sock->ops = &data_sock_ops;
  542. sock->state = SS_UNCONNECTED;
  543. sock_reset_flag(sk, SOCK_ZAPPED);
  544. sk->sk_protocol = protocol;
  545. sk->sk_state = MISDN_OPEN;
  546. mISDN_sock_link(&data_sockets, sk);
  547. return 0;
  548. }
  549. static int
  550. base_sock_release(struct socket *sock)
  551. {
  552. struct sock *sk = sock->sk;
  553. printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
  554. if (!sk)
  555. return 0;
  556. mISDN_sock_unlink(&base_sockets, sk);
  557. sock_orphan(sk);
  558. sock_put(sk);
  559. return 0;
  560. }
  561. static int
  562. base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  563. {
  564. int err = 0, id;
  565. struct mISDNdevice *dev;
  566. struct mISDNversion ver;
  567. switch (cmd) {
  568. case IMGETVERSION:
  569. ver.major = MISDN_MAJOR_VERSION;
  570. ver.minor = MISDN_MINOR_VERSION;
  571. ver.release = MISDN_RELEASE;
  572. if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
  573. err = -EFAULT;
  574. break;
  575. case IMGETCOUNT:
  576. id = get_mdevice_count();
  577. if (put_user(id, (int __user *)arg))
  578. err = -EFAULT;
  579. break;
  580. case IMGETDEVINFO:
  581. if (get_user(id, (int __user *)arg)) {
  582. err = -EFAULT;
  583. break;
  584. }
  585. dev = get_mdevice(id);
  586. if (dev) {
  587. struct mISDN_devinfo di;
  588. memset(&di, 0, sizeof(di));
  589. di.id = dev->id;
  590. di.Dprotocols = dev->Dprotocols;
  591. di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
  592. di.protocol = dev->D.protocol;
  593. memcpy(di.channelmap, dev->channelmap,
  594. sizeof(di.channelmap));
  595. di.nrbchan = dev->nrbchan;
  596. strcpy(di.name, dev_name(&dev->dev));
  597. if (copy_to_user((void __user *)arg, &di, sizeof(di)))
  598. err = -EFAULT;
  599. } else
  600. err = -ENODEV;
  601. break;
  602. case IMSETDEVNAME:
  603. {
  604. struct mISDN_devrename dn;
  605. if (copy_from_user(&dn, (void __user *)arg,
  606. sizeof(dn))) {
  607. err = -EFAULT;
  608. break;
  609. }
  610. dev = get_mdevice(dn.id);
  611. if (dev)
  612. err = device_rename(&dev->dev, dn.name);
  613. else
  614. err = -ENODEV;
  615. }
  616. break;
  617. default:
  618. err = -EINVAL;
  619. }
  620. return err;
  621. }
  622. static int
  623. base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  624. {
  625. struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
  626. struct sock *sk = sock->sk;
  627. int err = 0;
  628. if (!maddr || maddr->family != AF_ISDN)
  629. return -EINVAL;
  630. lock_sock(sk);
  631. if (_pms(sk)->dev) {
  632. err = -EALREADY;
  633. goto done;
  634. }
  635. _pms(sk)->dev = get_mdevice(maddr->dev);
  636. if (!_pms(sk)->dev) {
  637. err = -ENODEV;
  638. goto done;
  639. }
  640. sk->sk_state = MISDN_BOUND;
  641. done:
  642. release_sock(sk);
  643. return err;
  644. }
  645. static const struct proto_ops base_sock_ops = {
  646. .family = PF_ISDN,
  647. .owner = THIS_MODULE,
  648. .release = base_sock_release,
  649. .ioctl = base_sock_ioctl,
  650. .bind = base_sock_bind,
  651. .getname = sock_no_getname,
  652. .sendmsg = sock_no_sendmsg,
  653. .recvmsg = sock_no_recvmsg,
  654. .poll = sock_no_poll,
  655. .listen = sock_no_listen,
  656. .shutdown = sock_no_shutdown,
  657. .setsockopt = sock_no_setsockopt,
  658. .getsockopt = sock_no_getsockopt,
  659. .connect = sock_no_connect,
  660. .socketpair = sock_no_socketpair,
  661. .accept = sock_no_accept,
  662. .mmap = sock_no_mmap
  663. };
  664. static int
  665. base_sock_create(struct net *net, struct socket *sock, int protocol)
  666. {
  667. struct sock *sk;
  668. if (sock->type != SOCK_RAW)
  669. return -ESOCKTNOSUPPORT;
  670. sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
  671. if (!sk)
  672. return -ENOMEM;
  673. sock_init_data(sock, sk);
  674. sock->ops = &base_sock_ops;
  675. sock->state = SS_UNCONNECTED;
  676. sock_reset_flag(sk, SOCK_ZAPPED);
  677. sk->sk_protocol = protocol;
  678. sk->sk_state = MISDN_OPEN;
  679. mISDN_sock_link(&base_sockets, sk);
  680. return 0;
  681. }
  682. static int
  683. mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
  684. {
  685. int err = -EPROTONOSUPPORT;
  686. switch (proto) {
  687. case ISDN_P_BASE:
  688. err = base_sock_create(net, sock, proto);
  689. break;
  690. case ISDN_P_TE_S0:
  691. case ISDN_P_NT_S0:
  692. case ISDN_P_TE_E1:
  693. case ISDN_P_NT_E1:
  694. case ISDN_P_LAPD_TE:
  695. case ISDN_P_LAPD_NT:
  696. case ISDN_P_B_RAW:
  697. case ISDN_P_B_HDLC:
  698. case ISDN_P_B_X75SLP:
  699. case ISDN_P_B_L2DTMF:
  700. case ISDN_P_B_L2DSP:
  701. case ISDN_P_B_L2DSPHDLC:
  702. err = data_sock_create(net, sock, proto);
  703. break;
  704. default:
  705. return err;
  706. }
  707. return err;
  708. }
  709. static const struct net_proto_family mISDN_sock_family_ops = {
  710. .owner = THIS_MODULE,
  711. .family = PF_ISDN,
  712. .create = mISDN_sock_create,
  713. };
  714. int
  715. misdn_sock_init(u_int *deb)
  716. {
  717. int err;
  718. debug = deb;
  719. err = sock_register(&mISDN_sock_family_ops);
  720. if (err)
  721. printk(KERN_ERR "%s: error(%d)\n", __func__, err);
  722. return err;
  723. }
  724. void
  725. misdn_sock_cleanup(void)
  726. {
  727. sock_unregister(PF_ISDN);
  728. }