socket.c 18 KB

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