/drivers/isdn/i4l/isdn_net.c

https://bitbucket.org/ndreys/linux-sunxi · C · 3228 lines · 2447 code · 231 blank · 550 comment · 494 complexity · e4af6b4412a18b08bb4a326450040524 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* $Id: isdn_net.c,v 1.1.2.2 2004/01/12 22:37:19 keil Exp $
  2. *
  3. * Linux ISDN subsystem, network interfaces and related functions (linklevel).
  4. *
  5. * Copyright 1994-1998 by Fritz Elfert (fritz@isdn4linux.de)
  6. * Copyright 1995,96 by Thinking Objects Software GmbH Wuerzburg
  7. * Copyright 1995,96 by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. *
  12. * Data Over Voice (DOV) support added - Guy Ellis 23-Mar-02
  13. * guy@traverse.com.au
  14. * Outgoing calls - looks for a 'V' in first char of dialed number
  15. * Incoming calls - checks first character of eaz as follows:
  16. * Numeric - accept DATA only - original functionality
  17. * 'V' - accept VOICE (DOV) only
  18. * 'B' - accept BOTH DATA and DOV types
  19. *
  20. * Jan 2001: fix CISCO HDLC Bjoern A. Zeeb <i4l@zabbadoz.net>
  21. * for info on the protocol, see
  22. * http://i4l.zabbadoz.net/i4l/cisco-hdlc.txt
  23. */
  24. #include <linux/isdn.h>
  25. #include <linux/slab.h>
  26. #include <net/arp.h>
  27. #include <net/dst.h>
  28. #include <net/pkt_sched.h>
  29. #include <linux/inetdevice.h>
  30. #include "isdn_common.h"
  31. #include "isdn_net.h"
  32. #ifdef CONFIG_ISDN_PPP
  33. #include "isdn_ppp.h"
  34. #endif
  35. #ifdef CONFIG_ISDN_X25
  36. #include <linux/concap.h>
  37. #include "isdn_concap.h"
  38. #endif
  39. /*
  40. * Outline of new tbusy handling:
  41. *
  42. * Old method, roughly spoken, consisted of setting tbusy when entering
  43. * isdn_net_start_xmit() and at several other locations and clearing
  44. * it from isdn_net_start_xmit() thread when sending was successful.
  45. *
  46. * With 2.3.x multithreaded network core, to prevent problems, tbusy should
  47. * only be set by the isdn_net_start_xmit() thread and only when a tx-busy
  48. * condition is detected. Other threads (in particular isdn_net_stat_callb())
  49. * are only allowed to clear tbusy.
  50. *
  51. * -HE
  52. */
  53. /*
  54. * About SOFTNET:
  55. * Most of the changes were pretty obvious and basically done by HE already.
  56. *
  57. * One problem of the isdn net device code is that is uses struct net_device
  58. * for masters and slaves. However, only master interface are registered to
  59. * the network layer, and therefore, it only makes sense to call netif_*
  60. * functions on them.
  61. *
  62. * --KG
  63. */
  64. /*
  65. * Find out if the netdevice has been ifup-ed yet.
  66. * For slaves, look at the corresponding master.
  67. */
  68. static __inline__ int isdn_net_device_started(isdn_net_dev *n)
  69. {
  70. isdn_net_local *lp = n->local;
  71. struct net_device *dev;
  72. if (lp->master)
  73. dev = lp->master;
  74. else
  75. dev = n->dev;
  76. return netif_running(dev);
  77. }
  78. /*
  79. * wake up the network -> net_device queue.
  80. * For slaves, wake the corresponding master interface.
  81. */
  82. static __inline__ void isdn_net_device_wake_queue(isdn_net_local *lp)
  83. {
  84. if (lp->master)
  85. netif_wake_queue(lp->master);
  86. else
  87. netif_wake_queue(lp->netdev->dev);
  88. }
  89. /*
  90. * stop the network -> net_device queue.
  91. * For slaves, stop the corresponding master interface.
  92. */
  93. static __inline__ void isdn_net_device_stop_queue(isdn_net_local *lp)
  94. {
  95. if (lp->master)
  96. netif_stop_queue(lp->master);
  97. else
  98. netif_stop_queue(lp->netdev->dev);
  99. }
  100. /*
  101. * find out if the net_device which this lp belongs to (lp can be
  102. * master or slave) is busy. It's busy iff all (master and slave)
  103. * queues are busy
  104. */
  105. static __inline__ int isdn_net_device_busy(isdn_net_local *lp)
  106. {
  107. isdn_net_local *nlp;
  108. isdn_net_dev *nd;
  109. unsigned long flags;
  110. if (!isdn_net_lp_busy(lp))
  111. return 0;
  112. if (lp->master)
  113. nd = ISDN_MASTER_PRIV(lp)->netdev;
  114. else
  115. nd = lp->netdev;
  116. spin_lock_irqsave(&nd->queue_lock, flags);
  117. nlp = lp->next;
  118. while (nlp != lp) {
  119. if (!isdn_net_lp_busy(nlp)) {
  120. spin_unlock_irqrestore(&nd->queue_lock, flags);
  121. return 0;
  122. }
  123. nlp = nlp->next;
  124. }
  125. spin_unlock_irqrestore(&nd->queue_lock, flags);
  126. return 1;
  127. }
  128. static __inline__ void isdn_net_inc_frame_cnt(isdn_net_local *lp)
  129. {
  130. atomic_inc(&lp->frame_cnt);
  131. if (isdn_net_device_busy(lp))
  132. isdn_net_device_stop_queue(lp);
  133. }
  134. static __inline__ void isdn_net_dec_frame_cnt(isdn_net_local *lp)
  135. {
  136. atomic_dec(&lp->frame_cnt);
  137. if (!(isdn_net_device_busy(lp))) {
  138. if (!skb_queue_empty(&lp->super_tx_queue)) {
  139. schedule_work(&lp->tqueue);
  140. } else {
  141. isdn_net_device_wake_queue(lp);
  142. }
  143. }
  144. }
  145. static __inline__ void isdn_net_zero_frame_cnt(isdn_net_local *lp)
  146. {
  147. atomic_set(&lp->frame_cnt, 0);
  148. }
  149. /* For 2.2.x we leave the transmitter busy timeout at 2 secs, just
  150. * to be safe.
  151. * For 2.3.x we push it up to 20 secs, because call establishment
  152. * (in particular callback) may take such a long time, and we
  153. * don't want confusing messages in the log. However, there is a slight
  154. * possibility that this large timeout will break other things like MPPP,
  155. * which might rely on the tx timeout. If so, we'll find out this way...
  156. */
  157. #define ISDN_NET_TX_TIMEOUT (20*HZ)
  158. /* Prototypes */
  159. static int isdn_net_force_dial_lp(isdn_net_local *);
  160. static netdev_tx_t isdn_net_start_xmit(struct sk_buff *,
  161. struct net_device *);
  162. static void isdn_net_ciscohdlck_connected(isdn_net_local *lp);
  163. static void isdn_net_ciscohdlck_disconnected(isdn_net_local *lp);
  164. char *isdn_net_revision = "$Revision: 1.1.2.2 $";
  165. /*
  166. * Code for raw-networking over ISDN
  167. */
  168. static void
  169. isdn_net_unreachable(struct net_device *dev, struct sk_buff *skb, char *reason)
  170. {
  171. if(skb) {
  172. u_short proto = ntohs(skb->protocol);
  173. printk(KERN_DEBUG "isdn_net: %s: %s, signalling dst_link_failure %s\n",
  174. dev->name,
  175. (reason != NULL) ? reason : "unknown",
  176. (proto != ETH_P_IP) ? "Protocol != ETH_P_IP" : "");
  177. dst_link_failure(skb);
  178. }
  179. else { /* dial not triggered by rawIP packet */
  180. printk(KERN_DEBUG "isdn_net: %s: %s\n",
  181. dev->name,
  182. (reason != NULL) ? reason : "reason unknown");
  183. }
  184. }
  185. static void
  186. isdn_net_reset(struct net_device *dev)
  187. {
  188. #ifdef CONFIG_ISDN_X25
  189. struct concap_device_ops * dops =
  190. ((isdn_net_local *) netdev_priv(dev))->dops;
  191. struct concap_proto * cprot =
  192. ((isdn_net_local *) netdev_priv(dev))->netdev->cprot;
  193. #endif
  194. #ifdef CONFIG_ISDN_X25
  195. if( cprot && cprot -> pops && dops )
  196. cprot -> pops -> restart ( cprot, dev, dops );
  197. #endif
  198. }
  199. /* Open/initialize the board. */
  200. static int
  201. isdn_net_open(struct net_device *dev)
  202. {
  203. int i;
  204. struct net_device *p;
  205. struct in_device *in_dev;
  206. /* moved here from isdn_net_reset, because only the master has an
  207. interface associated which is supposed to be started. BTW:
  208. we need to call netif_start_queue, not netif_wake_queue here */
  209. netif_start_queue(dev);
  210. isdn_net_reset(dev);
  211. /* Fill in the MAC-level header (not needed, but for compatibility... */
  212. for (i = 0; i < ETH_ALEN - sizeof(u32); i++)
  213. dev->dev_addr[i] = 0xfc;
  214. if ((in_dev = dev->ip_ptr) != NULL) {
  215. /*
  216. * Any address will do - we take the first
  217. */
  218. struct in_ifaddr *ifa = in_dev->ifa_list;
  219. if (ifa != NULL)
  220. memcpy(dev->dev_addr+2, &ifa->ifa_local, 4);
  221. }
  222. /* If this interface has slaves, start them also */
  223. p = MASTER_TO_SLAVE(dev);
  224. if (p) {
  225. while (p) {
  226. isdn_net_reset(p);
  227. p = MASTER_TO_SLAVE(p);
  228. }
  229. }
  230. isdn_lock_drivers();
  231. return 0;
  232. }
  233. /*
  234. * Assign an ISDN-channel to a net-interface
  235. */
  236. static void
  237. isdn_net_bind_channel(isdn_net_local * lp, int idx)
  238. {
  239. lp->flags |= ISDN_NET_CONNECTED;
  240. lp->isdn_device = dev->drvmap[idx];
  241. lp->isdn_channel = dev->chanmap[idx];
  242. dev->rx_netdev[idx] = lp->netdev;
  243. dev->st_netdev[idx] = lp->netdev;
  244. }
  245. /*
  246. * unbind a net-interface (resets interface after an error)
  247. */
  248. static void
  249. isdn_net_unbind_channel(isdn_net_local * lp)
  250. {
  251. skb_queue_purge(&lp->super_tx_queue);
  252. if (!lp->master) { /* reset only master device */
  253. /* Moral equivalent of dev_purge_queues():
  254. BEWARE! This chunk of code cannot be called from hardware
  255. interrupt handler. I hope it is true. --ANK
  256. */
  257. qdisc_reset_all_tx(lp->netdev->dev);
  258. }
  259. lp->dialstate = 0;
  260. dev->rx_netdev[isdn_dc2minor(lp->isdn_device, lp->isdn_channel)] = NULL;
  261. dev->st_netdev[isdn_dc2minor(lp->isdn_device, lp->isdn_channel)] = NULL;
  262. if (lp->isdn_device != -1 && lp->isdn_channel != -1)
  263. isdn_free_channel(lp->isdn_device, lp->isdn_channel,
  264. ISDN_USAGE_NET);
  265. lp->flags &= ~ISDN_NET_CONNECTED;
  266. lp->isdn_device = -1;
  267. lp->isdn_channel = -1;
  268. }
  269. /*
  270. * Perform auto-hangup and cps-calculation for net-interfaces.
  271. *
  272. * auto-hangup:
  273. * Increment idle-counter (this counter is reset on any incoming or
  274. * outgoing packet), if counter exceeds configured limit either do a
  275. * hangup immediately or - if configured - wait until just before the next
  276. * charge-info.
  277. *
  278. * cps-calculation (needed for dynamic channel-bundling):
  279. * Since this function is called every second, simply reset the
  280. * byte-counter of the interface after copying it to the cps-variable.
  281. */
  282. static unsigned long last_jiffies = -HZ;
  283. void
  284. isdn_net_autohup(void)
  285. {
  286. isdn_net_dev *p = dev->netdev;
  287. int anymore;
  288. anymore = 0;
  289. while (p) {
  290. isdn_net_local *l = p->local;
  291. if (jiffies == last_jiffies)
  292. l->cps = l->transcount;
  293. else
  294. l->cps = (l->transcount * HZ) / (jiffies - last_jiffies);
  295. l->transcount = 0;
  296. if (dev->net_verbose > 3)
  297. printk(KERN_DEBUG "%s: %d bogocps\n", p->dev->name, l->cps);
  298. if ((l->flags & ISDN_NET_CONNECTED) && (!l->dialstate)) {
  299. anymore = 1;
  300. l->huptimer++;
  301. /*
  302. * if there is some dialmode where timeout-hangup
  303. * should _not_ be done, check for that here
  304. */
  305. if ((l->onhtime) &&
  306. (l->huptimer > l->onhtime))
  307. {
  308. if (l->hupflags & ISDN_MANCHARGE &&
  309. l->hupflags & ISDN_CHARGEHUP) {
  310. while (time_after(jiffies, l->chargetime + l->chargeint))
  311. l->chargetime += l->chargeint;
  312. if (time_after(jiffies, l->chargetime + l->chargeint - 2 * HZ))
  313. if (l->outgoing || l->hupflags & ISDN_INHUP)
  314. isdn_net_hangup(p->dev);
  315. } else if (l->outgoing) {
  316. if (l->hupflags & ISDN_CHARGEHUP) {
  317. if (l->hupflags & ISDN_WAITCHARGE) {
  318. printk(KERN_DEBUG "isdn_net: Hupflags of %s are %X\n",
  319. p->dev->name, l->hupflags);
  320. isdn_net_hangup(p->dev);
  321. } else if (time_after(jiffies, l->chargetime + l->chargeint)) {
  322. printk(KERN_DEBUG
  323. "isdn_net: %s: chtime = %lu, chint = %d\n",
  324. p->dev->name, l->chargetime, l->chargeint);
  325. isdn_net_hangup(p->dev);
  326. }
  327. } else
  328. isdn_net_hangup(p->dev);
  329. } else if (l->hupflags & ISDN_INHUP)
  330. isdn_net_hangup(p->dev);
  331. }
  332. if(dev->global_flags & ISDN_GLOBAL_STOPPED || (ISDN_NET_DIALMODE(*l) == ISDN_NET_DM_OFF)) {
  333. isdn_net_hangup(p->dev);
  334. break;
  335. }
  336. }
  337. p = (isdn_net_dev *) p->next;
  338. }
  339. last_jiffies = jiffies;
  340. isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, anymore);
  341. }
  342. static void isdn_net_lp_disconnected(isdn_net_local *lp)
  343. {
  344. isdn_net_rm_from_bundle(lp);
  345. }
  346. /*
  347. * Handle status-messages from ISDN-interfacecard.
  348. * This function is called from within the main-status-dispatcher
  349. * isdn_status_callback, which itself is called from the low-level driver.
  350. * Return: 1 = Event handled, 0 = not for us or unknown Event.
  351. */
  352. int
  353. isdn_net_stat_callback(int idx, isdn_ctrl *c)
  354. {
  355. isdn_net_dev *p = dev->st_netdev[idx];
  356. int cmd = c->command;
  357. if (p) {
  358. isdn_net_local *lp = p->local;
  359. #ifdef CONFIG_ISDN_X25
  360. struct concap_proto *cprot = lp->netdev->cprot;
  361. struct concap_proto_ops *pops = cprot ? cprot->pops : NULL;
  362. #endif
  363. switch (cmd) {
  364. case ISDN_STAT_BSENT:
  365. /* A packet has successfully been sent out */
  366. if ((lp->flags & ISDN_NET_CONNECTED) &&
  367. (!lp->dialstate)) {
  368. isdn_net_dec_frame_cnt(lp);
  369. lp->stats.tx_packets++;
  370. lp->stats.tx_bytes += c->parm.length;
  371. }
  372. return 1;
  373. case ISDN_STAT_DCONN:
  374. /* D-Channel is up */
  375. switch (lp->dialstate) {
  376. case 4:
  377. case 7:
  378. case 8:
  379. lp->dialstate++;
  380. return 1;
  381. case 12:
  382. lp->dialstate = 5;
  383. return 1;
  384. }
  385. break;
  386. case ISDN_STAT_DHUP:
  387. /* Either D-Channel-hangup or error during dialout */
  388. #ifdef CONFIG_ISDN_X25
  389. /* If we are not connencted then dialing had
  390. failed. If there are generic encap protocol
  391. receiver routines signal the closure of
  392. the link*/
  393. if( !(lp->flags & ISDN_NET_CONNECTED)
  394. && pops && pops -> disconn_ind )
  395. pops -> disconn_ind(cprot);
  396. #endif /* CONFIG_ISDN_X25 */
  397. if ((!lp->dialstate) && (lp->flags & ISDN_NET_CONNECTED)) {
  398. if (lp->p_encap == ISDN_NET_ENCAP_CISCOHDLCK)
  399. isdn_net_ciscohdlck_disconnected(lp);
  400. #ifdef CONFIG_ISDN_PPP
  401. if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
  402. isdn_ppp_free(lp);
  403. #endif
  404. isdn_net_lp_disconnected(lp);
  405. isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
  406. printk(KERN_INFO "%s: remote hangup\n", p->dev->name);
  407. printk(KERN_INFO "%s: Chargesum is %d\n", p->dev->name,
  408. lp->charge);
  409. isdn_net_unbind_channel(lp);
  410. return 1;
  411. }
  412. break;
  413. #ifdef CONFIG_ISDN_X25
  414. case ISDN_STAT_BHUP:
  415. /* B-Channel-hangup */
  416. /* try if there are generic encap protocol
  417. receiver routines and signal the closure of
  418. the link */
  419. if( pops && pops -> disconn_ind ){
  420. pops -> disconn_ind(cprot);
  421. return 1;
  422. }
  423. break;
  424. #endif /* CONFIG_ISDN_X25 */
  425. case ISDN_STAT_BCONN:
  426. /* B-Channel is up */
  427. isdn_net_zero_frame_cnt(lp);
  428. switch (lp->dialstate) {
  429. case 5:
  430. case 6:
  431. case 7:
  432. case 8:
  433. case 9:
  434. case 10:
  435. case 12:
  436. if (lp->dialstate <= 6) {
  437. dev->usage[idx] |= ISDN_USAGE_OUTGOING;
  438. isdn_info_update();
  439. } else
  440. dev->rx_netdev[idx] = p;
  441. lp->dialstate = 0;
  442. isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 1);
  443. if (lp->p_encap == ISDN_NET_ENCAP_CISCOHDLCK)
  444. isdn_net_ciscohdlck_connected(lp);
  445. if (lp->p_encap != ISDN_NET_ENCAP_SYNCPPP) {
  446. if (lp->master) { /* is lp a slave? */
  447. isdn_net_dev *nd = ISDN_MASTER_PRIV(lp)->netdev;
  448. isdn_net_add_to_bundle(nd, lp);
  449. }
  450. }
  451. printk(KERN_INFO "isdn_net: %s connected\n", p->dev->name);
  452. /* If first Chargeinfo comes before B-Channel connect,
  453. * we correct the timestamp here.
  454. */
  455. lp->chargetime = jiffies;
  456. /* reset dial-timeout */
  457. lp->dialstarted = 0;
  458. lp->dialwait_timer = 0;
  459. #ifdef CONFIG_ISDN_PPP
  460. if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
  461. isdn_ppp_wakeup_daemon(lp);
  462. #endif
  463. #ifdef CONFIG_ISDN_X25
  464. /* try if there are generic concap receiver routines */
  465. if( pops )
  466. if( pops->connect_ind)
  467. pops->connect_ind(cprot);
  468. #endif /* CONFIG_ISDN_X25 */
  469. /* ppp needs to do negotiations first */
  470. if (lp->p_encap != ISDN_NET_ENCAP_SYNCPPP)
  471. isdn_net_device_wake_queue(lp);
  472. return 1;
  473. }
  474. break;
  475. case ISDN_STAT_NODCH:
  476. /* No D-Channel avail. */
  477. if (lp->dialstate == 4) {
  478. lp->dialstate--;
  479. return 1;
  480. }
  481. break;
  482. case ISDN_STAT_CINF:
  483. /* Charge-info from TelCo. Calculate interval between
  484. * charge-infos and set timestamp for last info for
  485. * usage by isdn_net_autohup()
  486. */
  487. lp->charge++;
  488. if (lp->hupflags & ISDN_HAVECHARGE) {
  489. lp->hupflags &= ~ISDN_WAITCHARGE;
  490. lp->chargeint = jiffies - lp->chargetime - (2 * HZ);
  491. }
  492. if (lp->hupflags & ISDN_WAITCHARGE)
  493. lp->hupflags |= ISDN_HAVECHARGE;
  494. lp->chargetime = jiffies;
  495. printk(KERN_DEBUG "isdn_net: Got CINF chargetime of %s now %lu\n",
  496. p->dev->name, lp->chargetime);
  497. return 1;
  498. }
  499. }
  500. return 0;
  501. }
  502. /*
  503. * Perform dialout for net-interfaces and timeout-handling for
  504. * D-Channel-up and B-Channel-up Messages.
  505. * This function is initially called from within isdn_net_start_xmit() or
  506. * or isdn_net_find_icall() after initializing the dialstate for an
  507. * interface. If further calls are needed, the function schedules itself
  508. * for a timer-callback via isdn_timer_function().
  509. * The dialstate is also affected by incoming status-messages from
  510. * the ISDN-Channel which are handled in isdn_net_stat_callback() above.
  511. */
  512. void
  513. isdn_net_dial(void)
  514. {
  515. isdn_net_dev *p = dev->netdev;
  516. int anymore = 0;
  517. int i;
  518. isdn_ctrl cmd;
  519. u_char *phone_number;
  520. while (p) {
  521. isdn_net_local *lp = p->local;
  522. #ifdef ISDN_DEBUG_NET_DIAL
  523. if (lp->dialstate)
  524. printk(KERN_DEBUG "%s: dialstate=%d\n", p->dev->name, lp->dialstate);
  525. #endif
  526. switch (lp->dialstate) {
  527. case 0:
  528. /* Nothing to do for this interface */
  529. break;
  530. case 1:
  531. /* Initiate dialout. Set phone-number-pointer to first number
  532. * of interface.
  533. */
  534. lp->dial = lp->phone[1];
  535. if (!lp->dial) {
  536. printk(KERN_WARNING "%s: phone number deleted?\n",
  537. p->dev->name);
  538. isdn_net_hangup(p->dev);
  539. break;
  540. }
  541. anymore = 1;
  542. if(lp->dialtimeout > 0)
  543. if(lp->dialstarted == 0 || time_after(jiffies, lp->dialstarted + lp->dialtimeout + lp->dialwait)) {
  544. lp->dialstarted = jiffies;
  545. lp->dialwait_timer = 0;
  546. }
  547. lp->dialstate++;
  548. /* Fall through */
  549. case 2:
  550. /* Prepare dialing. Clear EAZ, then set EAZ. */
  551. cmd.driver = lp->isdn_device;
  552. cmd.arg = lp->isdn_channel;
  553. cmd.command = ISDN_CMD_CLREAZ;
  554. isdn_command(&cmd);
  555. sprintf(cmd.parm.num, "%s", isdn_map_eaz2msn(lp->msn, cmd.driver));
  556. cmd.command = ISDN_CMD_SETEAZ;
  557. isdn_command(&cmd);
  558. lp->dialretry = 0;
  559. anymore = 1;
  560. lp->dialstate++;
  561. /* Fall through */
  562. case 3:
  563. /* Setup interface, dial current phone-number, switch to next number.
  564. * If list of phone-numbers is exhausted, increment
  565. * retry-counter.
  566. */
  567. if(dev->global_flags & ISDN_GLOBAL_STOPPED || (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF)) {
  568. char *s;
  569. if (dev->global_flags & ISDN_GLOBAL_STOPPED)
  570. s = "dial suppressed: isdn system stopped";
  571. else
  572. s = "dial suppressed: dialmode `off'";
  573. isdn_net_unreachable(p->dev, NULL, s);
  574. isdn_net_hangup(p->dev);
  575. break;
  576. }
  577. cmd.driver = lp->isdn_device;
  578. cmd.command = ISDN_CMD_SETL2;
  579. cmd.arg = lp->isdn_channel + (lp->l2_proto << 8);
  580. isdn_command(&cmd);
  581. cmd.driver = lp->isdn_device;
  582. cmd.command = ISDN_CMD_SETL3;
  583. cmd.arg = lp->isdn_channel + (lp->l3_proto << 8);
  584. isdn_command(&cmd);
  585. cmd.driver = lp->isdn_device;
  586. cmd.arg = lp->isdn_channel;
  587. if (!lp->dial) {
  588. printk(KERN_WARNING "%s: phone number deleted?\n",
  589. p->dev->name);
  590. isdn_net_hangup(p->dev);
  591. break;
  592. }
  593. if (!strncmp(lp->dial->num, "LEASED", strlen("LEASED"))) {
  594. lp->dialstate = 4;
  595. printk(KERN_INFO "%s: Open leased line ...\n", p->dev->name);
  596. } else {
  597. if(lp->dialtimeout > 0)
  598. if (time_after(jiffies, lp->dialstarted + lp->dialtimeout)) {
  599. lp->dialwait_timer = jiffies + lp->dialwait;
  600. lp->dialstarted = 0;
  601. isdn_net_unreachable(p->dev, NULL, "dial: timed out");
  602. isdn_net_hangup(p->dev);
  603. break;
  604. }
  605. cmd.driver = lp->isdn_device;
  606. cmd.command = ISDN_CMD_DIAL;
  607. cmd.parm.setup.si2 = 0;
  608. /* check for DOV */
  609. phone_number = lp->dial->num;
  610. if ((*phone_number == 'v') ||
  611. (*phone_number == 'V')) { /* DOV call */
  612. cmd.parm.setup.si1 = 1;
  613. } else { /* DATA call */
  614. cmd.parm.setup.si1 = 7;
  615. }
  616. strcpy(cmd.parm.setup.phone, phone_number);
  617. /*
  618. * Switch to next number or back to start if at end of list.
  619. */
  620. if (!(lp->dial = (isdn_net_phone *) lp->dial->next)) {
  621. lp->dial = lp->phone[1];
  622. lp->dialretry++;
  623. if (lp->dialretry > lp->dialmax) {
  624. if (lp->dialtimeout == 0) {
  625. lp->dialwait_timer = jiffies + lp->dialwait;
  626. lp->dialstarted = 0;
  627. isdn_net_unreachable(p->dev, NULL, "dial: tried all numbers dialmax times");
  628. }
  629. isdn_net_hangup(p->dev);
  630. break;
  631. }
  632. }
  633. sprintf(cmd.parm.setup.eazmsn, "%s",
  634. isdn_map_eaz2msn(lp->msn, cmd.driver));
  635. i = isdn_dc2minor(lp->isdn_device, lp->isdn_channel);
  636. if (i >= 0) {
  637. strcpy(dev->num[i], cmd.parm.setup.phone);
  638. dev->usage[i] |= ISDN_USAGE_OUTGOING;
  639. isdn_info_update();
  640. }
  641. printk(KERN_INFO "%s: dialing %d %s... %s\n", p->dev->name,
  642. lp->dialretry, cmd.parm.setup.phone,
  643. (cmd.parm.setup.si1 == 1) ? "DOV" : "");
  644. lp->dtimer = 0;
  645. #ifdef ISDN_DEBUG_NET_DIAL
  646. printk(KERN_DEBUG "dial: d=%d c=%d\n", lp->isdn_device,
  647. lp->isdn_channel);
  648. #endif
  649. isdn_command(&cmd);
  650. }
  651. lp->huptimer = 0;
  652. lp->outgoing = 1;
  653. if (lp->chargeint) {
  654. lp->hupflags |= ISDN_HAVECHARGE;
  655. lp->hupflags &= ~ISDN_WAITCHARGE;
  656. } else {
  657. lp->hupflags |= ISDN_WAITCHARGE;
  658. lp->hupflags &= ~ISDN_HAVECHARGE;
  659. }
  660. anymore = 1;
  661. lp->dialstate =
  662. (lp->cbdelay &&
  663. (lp->flags & ISDN_NET_CBOUT)) ? 12 : 4;
  664. break;
  665. case 4:
  666. /* Wait for D-Channel-connect.
  667. * If timeout, switch back to state 3.
  668. * Dialmax-handling moved to state 3.
  669. */
  670. if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
  671. lp->dialstate = 3;
  672. anymore = 1;
  673. break;
  674. case 5:
  675. /* Got D-Channel-Connect, send B-Channel-request */
  676. cmd.driver = lp->isdn_device;
  677. cmd.arg = lp->isdn_channel;
  678. cmd.command = ISDN_CMD_ACCEPTB;
  679. anymore = 1;
  680. lp->dtimer = 0;
  681. lp->dialstate++;
  682. isdn_command(&cmd);
  683. break;
  684. case 6:
  685. /* Wait for B- or D-Channel-connect. If timeout,
  686. * switch back to state 3.
  687. */
  688. #ifdef ISDN_DEBUG_NET_DIAL
  689. printk(KERN_DEBUG "dialtimer2: %d\n", lp->dtimer);
  690. #endif
  691. if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
  692. lp->dialstate = 3;
  693. anymore = 1;
  694. break;
  695. case 7:
  696. /* Got incoming Call, setup L2 and L3 protocols,
  697. * then wait for D-Channel-connect
  698. */
  699. #ifdef ISDN_DEBUG_NET_DIAL
  700. printk(KERN_DEBUG "dialtimer4: %d\n", lp->dtimer);
  701. #endif
  702. cmd.driver = lp->isdn_device;
  703. cmd.command = ISDN_CMD_SETL2;
  704. cmd.arg = lp->isdn_channel + (lp->l2_proto << 8);
  705. isdn_command(&cmd);
  706. cmd.driver = lp->isdn_device;
  707. cmd.command = ISDN_CMD_SETL3;
  708. cmd.arg = lp->isdn_channel + (lp->l3_proto << 8);
  709. isdn_command(&cmd);
  710. if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT15)
  711. isdn_net_hangup(p->dev);
  712. else {
  713. anymore = 1;
  714. lp->dialstate++;
  715. }
  716. break;
  717. case 9:
  718. /* Got incoming D-Channel-Connect, send B-Channel-request */
  719. cmd.driver = lp->isdn_device;
  720. cmd.arg = lp->isdn_channel;
  721. cmd.command = ISDN_CMD_ACCEPTB;
  722. isdn_command(&cmd);
  723. anymore = 1;
  724. lp->dtimer = 0;
  725. lp->dialstate++;
  726. break;
  727. case 8:
  728. case 10:
  729. /* Wait for B- or D-channel-connect */
  730. #ifdef ISDN_DEBUG_NET_DIAL
  731. printk(KERN_DEBUG "dialtimer4: %d\n", lp->dtimer);
  732. #endif
  733. if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
  734. isdn_net_hangup(p->dev);
  735. else
  736. anymore = 1;
  737. break;
  738. case 11:
  739. /* Callback Delay */
  740. if (lp->dtimer++ > lp->cbdelay)
  741. lp->dialstate = 1;
  742. anymore = 1;
  743. break;
  744. case 12:
  745. /* Remote does callback. Hangup after cbdelay, then wait for incoming
  746. * call (in state 4).
  747. */
  748. if (lp->dtimer++ > lp->cbdelay)
  749. {
  750. printk(KERN_INFO "%s: hangup waiting for callback ...\n", p->dev->name);
  751. lp->dtimer = 0;
  752. lp->dialstate = 4;
  753. cmd.driver = lp->isdn_device;
  754. cmd.command = ISDN_CMD_HANGUP;
  755. cmd.arg = lp->isdn_channel;
  756. isdn_command(&cmd);
  757. isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
  758. }
  759. anymore = 1;
  760. break;
  761. default:
  762. printk(KERN_WARNING "isdn_net: Illegal dialstate %d for device %s\n",
  763. lp->dialstate, p->dev->name);
  764. }
  765. p = (isdn_net_dev *) p->next;
  766. }
  767. isdn_timer_ctrl(ISDN_TIMER_NETDIAL, anymore);
  768. }
  769. /*
  770. * Perform hangup for a net-interface.
  771. */
  772. void
  773. isdn_net_hangup(struct net_device *d)
  774. {
  775. isdn_net_local *lp = netdev_priv(d);
  776. isdn_ctrl cmd;
  777. #ifdef CONFIG_ISDN_X25
  778. struct concap_proto *cprot = lp->netdev->cprot;
  779. struct concap_proto_ops *pops = cprot ? cprot->pops : NULL;
  780. #endif
  781. if (lp->flags & ISDN_NET_CONNECTED) {
  782. if (lp->slave != NULL) {
  783. isdn_net_local *slp = ISDN_SLAVE_PRIV(lp);
  784. if (slp->flags & ISDN_NET_CONNECTED) {
  785. printk(KERN_INFO
  786. "isdn_net: hang up slave %s before %s\n",
  787. lp->slave->name, d->name);
  788. isdn_net_hangup(lp->slave);
  789. }
  790. }
  791. printk(KERN_INFO "isdn_net: local hangup %s\n", d->name);
  792. #ifdef CONFIG_ISDN_PPP
  793. if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
  794. isdn_ppp_free(lp);
  795. #endif
  796. isdn_net_lp_disconnected(lp);
  797. #ifdef CONFIG_ISDN_X25
  798. /* try if there are generic encap protocol
  799. receiver routines and signal the closure of
  800. the link */
  801. if( pops && pops -> disconn_ind )
  802. pops -> disconn_ind(cprot);
  803. #endif /* CONFIG_ISDN_X25 */
  804. cmd.driver = lp->isdn_device;
  805. cmd.command = ISDN_CMD_HANGUP;
  806. cmd.arg = lp->isdn_channel;
  807. isdn_command(&cmd);
  808. printk(KERN_INFO "%s: Chargesum is %d\n", d->name, lp->charge);
  809. isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
  810. }
  811. isdn_net_unbind_channel(lp);
  812. }
  813. typedef struct {
  814. __be16 source;
  815. __be16 dest;
  816. } ip_ports;
  817. static void
  818. isdn_net_log_skb(struct sk_buff * skb, isdn_net_local * lp)
  819. {
  820. /* hopefully, this was set correctly */
  821. const u_char *p = skb_network_header(skb);
  822. unsigned short proto = ntohs(skb->protocol);
  823. int data_ofs;
  824. ip_ports *ipp;
  825. char addinfo[100];
  826. addinfo[0] = '\0';
  827. /* This check stolen from 2.1.72 dev_queue_xmit_nit() */
  828. if (p < skb->data || skb->network_header >= skb->tail) {
  829. /* fall back to old isdn_net_log_packet method() */
  830. char * buf = skb->data;
  831. printk(KERN_DEBUG "isdn_net: protocol %04x is buggy, dev %s\n", skb->protocol, lp->netdev->dev->name);
  832. p = buf;
  833. proto = ETH_P_IP;
  834. switch (lp->p_encap) {
  835. case ISDN_NET_ENCAP_IPTYP:
  836. proto = ntohs(*(__be16 *)&buf[0]);
  837. p = &buf[2];
  838. break;
  839. case ISDN_NET_ENCAP_ETHER:
  840. proto = ntohs(*(__be16 *)&buf[12]);
  841. p = &buf[14];
  842. break;
  843. case ISDN_NET_ENCAP_CISCOHDLC:
  844. proto = ntohs(*(__be16 *)&buf[2]);
  845. p = &buf[4];
  846. break;
  847. #ifdef CONFIG_ISDN_PPP
  848. case ISDN_NET_ENCAP_SYNCPPP:
  849. proto = ntohs(skb->protocol);
  850. p = &buf[IPPP_MAX_HEADER];
  851. break;
  852. #endif
  853. }
  854. }
  855. data_ofs = ((p[0] & 15) * 4);
  856. switch (proto) {
  857. case ETH_P_IP:
  858. switch (p[9]) {
  859. case 1:
  860. strcpy(addinfo, " ICMP");
  861. break;
  862. case 2:
  863. strcpy(addinfo, " IGMP");
  864. break;
  865. case 4:
  866. strcpy(addinfo, " IPIP");
  867. break;
  868. case 6:
  869. ipp = (ip_ports *) (&p[data_ofs]);
  870. sprintf(addinfo, " TCP, port: %d -> %d", ntohs(ipp->source),
  871. ntohs(ipp->dest));
  872. break;
  873. case 8:
  874. strcpy(addinfo, " EGP");
  875. break;
  876. case 12:
  877. strcpy(addinfo, " PUP");
  878. break;
  879. case 17:
  880. ipp = (ip_ports *) (&p[data_ofs]);
  881. sprintf(addinfo, " UDP, port: %d -> %d", ntohs(ipp->source),
  882. ntohs(ipp->dest));
  883. break;
  884. case 22:
  885. strcpy(addinfo, " IDP");
  886. break;
  887. }
  888. printk(KERN_INFO "OPEN: %pI4 -> %pI4%s\n",
  889. p + 12, p + 16, addinfo);
  890. break;
  891. case ETH_P_ARP:
  892. printk(KERN_INFO "OPEN: ARP %pI4 -> *.*.*.* ?%pI4\n",
  893. p + 14, p + 24);
  894. break;
  895. }
  896. }
  897. /*
  898. * this function is used to send supervisory data, i.e. data which was
  899. * not received from the network layer, but e.g. frames from ipppd, CCP
  900. * reset frames etc.
  901. */
  902. void isdn_net_write_super(isdn_net_local *lp, struct sk_buff *skb)
  903. {
  904. if (in_irq()) {
  905. // we can't grab the lock from irq context,
  906. // so we just queue the packet
  907. skb_queue_tail(&lp->super_tx_queue, skb);
  908. schedule_work(&lp->tqueue);
  909. return;
  910. }
  911. spin_lock_bh(&lp->xmit_lock);
  912. if (!isdn_net_lp_busy(lp)) {
  913. isdn_net_writebuf_skb(lp, skb);
  914. } else {
  915. skb_queue_tail(&lp->super_tx_queue, skb);
  916. }
  917. spin_unlock_bh(&lp->xmit_lock);
  918. }
  919. /*
  920. * called from tq_immediate
  921. */
  922. static void isdn_net_softint(struct work_struct *work)
  923. {
  924. isdn_net_local *lp = container_of(work, isdn_net_local, tqueue);
  925. struct sk_buff *skb;
  926. spin_lock_bh(&lp->xmit_lock);
  927. while (!isdn_net_lp_busy(lp)) {
  928. skb = skb_dequeue(&lp->super_tx_queue);
  929. if (!skb)
  930. break;
  931. isdn_net_writebuf_skb(lp, skb);
  932. }
  933. spin_unlock_bh(&lp->xmit_lock);
  934. }
  935. /*
  936. * all frames sent from the (net) LL to a HL driver should go via this function
  937. * it's serialized by the caller holding the lp->xmit_lock spinlock
  938. */
  939. void isdn_net_writebuf_skb(isdn_net_local *lp, struct sk_buff *skb)
  940. {
  941. int ret;
  942. int len = skb->len; /* save len */
  943. /* before obtaining the lock the caller should have checked that
  944. the lp isn't busy */
  945. if (isdn_net_lp_busy(lp)) {
  946. printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
  947. goto error;
  948. }
  949. if (!(lp->flags & ISDN_NET_CONNECTED)) {
  950. printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
  951. goto error;
  952. }
  953. ret = isdn_writebuf_skb_stub(lp->isdn_device, lp->isdn_channel, 1, skb);
  954. if (ret != len) {
  955. /* we should never get here */
  956. printk(KERN_WARNING "%s: HL driver queue full\n", lp->netdev->dev->name);
  957. goto error;
  958. }
  959. lp->transcount += len;
  960. isdn_net_inc_frame_cnt(lp);
  961. return;
  962. error:
  963. dev_kfree_skb(skb);
  964. lp->stats.tx_errors++;
  965. }
  966. /*
  967. * Helper function for isdn_net_start_xmit.
  968. * When called, the connection is already established.
  969. * Based on cps-calculation, check if device is overloaded.
  970. * If so, and if a slave exists, trigger dialing for it.
  971. * If any slave is online, deliver packets using a simple round robin
  972. * scheme.
  973. *
  974. * Return: 0 on success, !0 on failure.
  975. */
  976. static int
  977. isdn_net_xmit(struct net_device *ndev, struct sk_buff *skb)
  978. {
  979. isdn_net_dev *nd;
  980. isdn_net_local *slp;
  981. isdn_net_local *lp = netdev_priv(ndev);
  982. int retv = NETDEV_TX_OK;
  983. if (((isdn_net_local *) netdev_priv(ndev))->master) {
  984. printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
  985. dev_kfree_skb(skb);
  986. return NETDEV_TX_OK;
  987. }
  988. /* For the other encaps the header has already been built */
  989. #ifdef CONFIG_ISDN_PPP
  990. if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
  991. return isdn_ppp_xmit(skb, ndev);
  992. }
  993. #endif
  994. nd = ((isdn_net_local *) netdev_priv(ndev))->netdev;
  995. lp = isdn_net_get_locked_lp(nd);
  996. if (!lp) {
  997. printk(KERN_WARNING "%s: all channels busy - requeuing!\n", ndev->name);
  998. return NETDEV_TX_BUSY;
  999. }
  1000. /* we have our lp locked from now on */
  1001. /* Reset hangup-timeout */
  1002. lp->huptimer = 0; // FIXME?
  1003. isdn_net_writebuf_skb(lp, skb);
  1004. spin_unlock_bh(&lp->xmit_lock);
  1005. /* the following stuff is here for backwards compatibility.
  1006. * in future, start-up and hangup of slaves (based on current load)
  1007. * should move to userspace and get based on an overall cps
  1008. * calculation
  1009. */
  1010. if (lp->cps > lp->triggercps) {
  1011. if (lp->slave) {
  1012. if (!lp->sqfull) {
  1013. /* First time overload: set timestamp only */
  1014. lp->sqfull = 1;
  1015. lp->sqfull_stamp = jiffies;
  1016. } else {
  1017. /* subsequent overload: if slavedelay exceeded, start dialing */
  1018. if (time_after(jiffies, lp->sqfull_stamp + lp->slavedelay)) {
  1019. slp = ISDN_SLAVE_PRIV(lp);
  1020. if (!(slp->flags & ISDN_NET_CONNECTED)) {
  1021. isdn_net_force_dial_lp(ISDN_SLAVE_PRIV(lp));
  1022. }
  1023. }
  1024. }
  1025. }
  1026. } else {
  1027. if (lp->sqfull && time_after(jiffies, lp->sqfull_stamp + lp->slavedelay + (10 * HZ))) {
  1028. lp->sqfull = 0;
  1029. }
  1030. /* this is a hack to allow auto-hangup for slaves on moderate loads */
  1031. nd->queue = nd->local;
  1032. }
  1033. return retv;
  1034. }
  1035. static void
  1036. isdn_net_adjust_hdr(struct sk_buff *skb, struct net_device *dev)
  1037. {
  1038. isdn_net_local *lp = netdev_priv(dev);
  1039. if (!skb)
  1040. return;
  1041. if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
  1042. const int pullsize = skb_network_offset(skb) - ETH_HLEN;
  1043. if (pullsize > 0) {
  1044. printk(KERN_DEBUG "isdn_net: Pull junk %d\n", pullsize);
  1045. skb_pull(skb, pullsize);
  1046. }
  1047. }
  1048. }
  1049. static void isdn_net_tx_timeout(struct net_device * ndev)
  1050. {
  1051. isdn_net_local *lp = netdev_priv(ndev);
  1052. printk(KERN_WARNING "isdn_tx_timeout dev %s dialstate %d\n", ndev->name, lp->dialstate);
  1053. if (!lp->dialstate){
  1054. lp->stats.tx_errors++;
  1055. /*
  1056. * There is a certain probability that this currently
  1057. * works at all because if we always wake up the interface,
  1058. * then upper layer will try to send the next packet
  1059. * immediately. And then, the old clean_up logic in the
  1060. * driver will hopefully continue to work as it used to do.
  1061. *
  1062. * This is rather primitive right know, we better should
  1063. * clean internal queues here, in particular for multilink and
  1064. * ppp, and reset HL driver's channel, too. --HE
  1065. *
  1066. * actually, this may not matter at all, because ISDN hardware
  1067. * should not see transmitter hangs at all IMO
  1068. * changed KERN_DEBUG to KERN_WARNING to find out if this is
  1069. * ever called --KG
  1070. */
  1071. }
  1072. ndev->trans_start = jiffies;
  1073. netif_wake_queue(ndev);
  1074. }
  1075. /*
  1076. * Try sending a packet.
  1077. * If this interface isn't connected to a ISDN-Channel, find a free channel,
  1078. * and start dialing.
  1079. */
  1080. static netdev_tx_t
  1081. isdn_net_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1082. {
  1083. isdn_net_local *lp = netdev_priv(ndev);
  1084. #ifdef CONFIG_ISDN_X25
  1085. struct concap_proto * cprot = lp -> netdev -> cprot;
  1086. /* At this point hard_start_xmit() passes control to the encapsulation
  1087. protocol (if present).
  1088. For X.25 auto-dialing is completly bypassed because:
  1089. - It does not conform with the semantics of a reliable datalink
  1090. service as needed by X.25 PLP.
  1091. - I don't want that the interface starts dialing when the network layer
  1092. sends a message which requests to disconnect the lapb link (or if it
  1093. sends any other message not resulting in data transmission).
  1094. Instead, dialing will be initiated by the encapsulation protocol entity
  1095. when a dl_establish request is received from the upper layer.
  1096. */
  1097. if (cprot && cprot -> pops) {
  1098. int ret = cprot -> pops -> encap_and_xmit ( cprot , skb);
  1099. if (ret)
  1100. netif_stop_queue(ndev);
  1101. return ret;
  1102. } else
  1103. #endif
  1104. /* auto-dialing xmit function */
  1105. {
  1106. #ifdef ISDN_DEBUG_NET_DUMP
  1107. u_char *buf;
  1108. #endif
  1109. isdn_net_adjust_hdr(skb, ndev);
  1110. #ifdef ISDN_DEBUG_NET_DUMP
  1111. buf = skb->data;
  1112. isdn_dumppkt("S:", buf, skb->len, 40);
  1113. #endif
  1114. if (!(lp->flags & ISDN_NET_CONNECTED)) {
  1115. int chi;
  1116. /* only do autodial if allowed by config */
  1117. if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) {
  1118. isdn_net_unreachable(ndev, skb, "dial rejected: interface not in dialmode `auto'");
  1119. dev_kfree_skb(skb);
  1120. return NETDEV_TX_OK;
  1121. }
  1122. if (lp->phone[1]) {
  1123. ulong flags;
  1124. if(lp->dialwait_timer <= 0)
  1125. if(lp->dialstarted > 0 && lp->dialtimeout > 0 && time_before(jiffies, lp->dialstarted + lp->dialtimeout + lp->dialwait))
  1126. lp->dialwait_timer = lp->dialstarted + lp->dialtimeout + lp->dialwait;
  1127. if(lp->dialwait_timer > 0) {
  1128. if(time_before(jiffies, lp->dialwait_timer)) {
  1129. isdn_net_unreachable(ndev, skb, "dial rejected: retry-time not reached");
  1130. dev_kfree_skb(skb);
  1131. return NETDEV_TX_OK;
  1132. } else
  1133. lp->dialwait_timer = 0;
  1134. }
  1135. /* Grab a free ISDN-Channel */
  1136. spin_lock_irqsave(&dev->lock, flags);
  1137. if (((chi =
  1138. isdn_get_free_channel(
  1139. ISDN_USAGE_NET,
  1140. lp->l2_proto,
  1141. lp->l3_proto,
  1142. lp->pre_device,
  1143. lp->pre_channel,
  1144. lp->msn)
  1145. ) < 0) &&
  1146. ((chi =
  1147. isdn_get_free_channel(
  1148. ISDN_USAGE_NET,
  1149. lp->l2_proto,
  1150. lp->l3_proto,
  1151. lp->pre_device,
  1152. lp->pre_channel^1,
  1153. lp->msn)
  1154. ) < 0)) {
  1155. spin_unlock_irqrestore(&dev->lock, flags);
  1156. isdn_net_unreachable(ndev, skb,
  1157. "No channel");
  1158. dev_kfree_skb(skb);
  1159. return NETDEV_TX_OK;
  1160. }
  1161. /* Log packet, which triggered dialing */
  1162. if (dev->net_verbose)
  1163. isdn_net_log_skb(skb, lp);
  1164. lp->dialstate = 1;
  1165. /* Connect interface with channel */
  1166. isdn_net_bind_channel(lp, chi);
  1167. #ifdef CONFIG_ISDN_PPP
  1168. if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
  1169. /* no 'first_skb' handling for syncPPP */
  1170. if (isdn_ppp_bind(lp) < 0) {
  1171. dev_kfree_skb(skb);
  1172. isdn_net_unbind_channel(lp);
  1173. spin_unlock_irqrestore(&dev->lock, flags);
  1174. return NETDEV_TX_OK; /* STN (skb to nirvana) ;) */
  1175. }
  1176. #ifdef CONFIG_IPPP_FILTER
  1177. if (isdn_ppp_autodial_filter(skb, lp)) {
  1178. isdn_ppp_free(lp);
  1179. isdn_net_unbind_channel(lp);
  1180. spin_unlock_irqrestore(&dev->lock, flags);
  1181. isdn_net_unreachable(ndev, skb, "dial rejected: packet filtered");
  1182. dev_kfree_skb(skb);
  1183. return NETDEV_TX_OK;
  1184. }
  1185. #endif
  1186. spin_unlock_irqrestore(&dev->lock, flags);
  1187. isdn_net_dial(); /* Initiate dialing */
  1188. netif_stop_queue(ndev);
  1189. return NETDEV_TX_BUSY; /* let upper layer requeue skb packet */
  1190. }
  1191. #endif
  1192. /* Initiate dialing */
  1193. spin_unlock_irqrestore(&dev->lock, flags);
  1194. isdn_net_dial();
  1195. isdn_net_device_stop_queue(lp);
  1196. return NETDEV_TX_BUSY;
  1197. } else {
  1198. isdn_net_unreachable(ndev, skb,
  1199. "No phone number");
  1200. dev_kfree_skb(skb);
  1201. return NETDEV_TX_OK;
  1202. }
  1203. } else {
  1204. /* Device is connected to an ISDN channel */
  1205. ndev->trans_start = jiffies;
  1206. if (!lp->dialstate) {
  1207. /* ISDN connection is established, try sending */
  1208. int ret;
  1209. ret = (isdn_net_xmit(ndev, skb));
  1210. if(ret) netif_stop_queue(ndev);
  1211. return ret;
  1212. } else
  1213. netif_stop_queue(ndev);
  1214. }
  1215. }
  1216. return NETDEV_TX_BUSY;
  1217. }
  1218. /*
  1219. * Shutdown a net-interface.
  1220. */
  1221. static int
  1222. isdn_net_close(struct net_device *dev)
  1223. {
  1224. struct net_device *p;
  1225. #ifdef CONFIG_ISDN_X25
  1226. struct concap_proto * cprot =
  1227. ((isdn_net_local *) netdev_priv(dev))->netdev->cprot;
  1228. /* printk(KERN_DEBUG "isdn_net_close %s\n" , dev-> name ); */
  1229. #endif
  1230. #ifdef CONFIG_ISDN_X25
  1231. if( cprot && cprot -> pops ) cprot -> pops -> close( cprot );
  1232. #endif
  1233. netif_stop_queue(dev);
  1234. p = MASTER_TO_SLAVE(dev);
  1235. if (p) {
  1236. /* If this interface has slaves, stop them also */
  1237. while (p) {
  1238. #ifdef CONFIG_ISDN_X25
  1239. cprot = ((isdn_net_local *) netdev_priv(p))
  1240. -> netdev -> cprot;
  1241. if( cprot && cprot -> pops )
  1242. cprot -> pops -> close( cprot );
  1243. #endif
  1244. isdn_net_hangup(p);
  1245. p = MASTER_TO_SLAVE(p);
  1246. }
  1247. }
  1248. isdn_net_hangup(dev);
  1249. isdn_unlock_drivers();
  1250. return 0;
  1251. }
  1252. /*
  1253. * Get statistics
  1254. */
  1255. static struct net_device_stats *
  1256. isdn_net_get_stats(struct net_device *dev)
  1257. {
  1258. isdn_net_local *lp = netdev_priv(dev);
  1259. return &lp->stats;
  1260. }
  1261. /* This is simply a copy from std. eth.c EXCEPT we pull ETH_HLEN
  1262. * instead of dev->hard_header_len off. This is done because the
  1263. * lowlevel-driver has already pulled off its stuff when we get
  1264. * here and this routine only gets called with p_encap == ETHER.
  1265. * Determine the packet's protocol ID. The rule here is that we
  1266. * assume 802.3 if the type field is short enough to be a length.
  1267. * This is normal practice and works for any 'now in use' protocol.
  1268. */
  1269. static __be16
  1270. isdn_net_type_trans(struct sk_buff *skb, struct net_device *dev)
  1271. {
  1272. struct ethhdr *eth;
  1273. unsigned char *rawp;
  1274. skb_reset_mac_header(skb);
  1275. skb_pull(skb, ETH_HLEN);
  1276. eth = eth_hdr(skb);
  1277. if (*eth->h_dest & 1) {
  1278. if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
  1279. skb->pkt_type = PACKET_BROADCAST;
  1280. else
  1281. skb->pkt_type = PACKET_MULTICAST;
  1282. }
  1283. /*
  1284. * This ALLMULTI check should be redundant by 1.4
  1285. * so don't forget to remove it.
  1286. */
  1287. else if (dev->flags & (IFF_PROMISC /*| IFF_ALLMULTI*/)) {
  1288. if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
  1289. skb->pkt_type = PACKET_OTHERHOST;
  1290. }
  1291. if (ntohs(eth->h_proto) >= 1536)
  1292. return eth->h_proto;
  1293. rawp = skb->data;
  1294. /*
  1295. * This is a magic hack to spot IPX packets. Older Novell breaks
  1296. * the protocol design and runs IPX over 802.3 without an 802.2 LLC
  1297. * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
  1298. * won't work for fault tolerant netware but does for the rest.
  1299. */
  1300. if (*(unsigned short *) rawp == 0xFFFF)
  1301. return htons(ETH_P_802_3);
  1302. /*
  1303. * Real 802.2 LLC
  1304. */
  1305. return htons(ETH_P_802_2);
  1306. }
  1307. /*
  1308. * CISCO HDLC keepalive specific stuff
  1309. */
  1310. static struct sk_buff*
  1311. isdn_net_ciscohdlck_alloc_skb(isdn_net_local *lp, int len)
  1312. {
  1313. unsigned short hl = dev->drv[lp->isdn_device]->interface->hl_hdrlen;
  1314. struct sk_buff *skb;
  1315. skb = alloc_skb(hl + len, GFP_ATOMIC);
  1316. if (skb)
  1317. skb_reserve(skb, hl);
  1318. else
  1319. printk("isdn out of mem at %s:%d!\n", __FILE__, __LINE__);
  1320. return skb;
  1321. }
  1322. /* cisco hdlck device private ioctls */
  1323. static int
  1324. isdn_ciscohdlck_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1325. {
  1326. isdn_net_local *lp = netdev_priv(dev);
  1327. unsigned long len = 0;
  1328. unsigned long expires = 0;
  1329. int tmp = 0;
  1330. int period = lp->cisco_keepalive_period;
  1331. s8 debserint = lp->cisco_debserint;
  1332. int rc = 0;
  1333. if (lp->p_encap != ISDN_NET_ENCAP_CISCOHDLCK)
  1334. return -EINVAL;
  1335. switch (cmd) {
  1336. /* get/set keepalive period */
  1337. case SIOCGKEEPPERIOD:
  1338. len = (unsigned long)sizeof(lp->cisco_keepalive_period);
  1339. if (copy_to_user(ifr->ifr_data,
  1340. &lp->cisco_keepalive_period, len))
  1341. rc = -EFAULT;
  1342. break;
  1343. case SIOCSKEEPPERIOD:
  1344. tmp = lp->cisco_keepalive_period;
  1345. len = (unsigned long)sizeof(lp->cisco_keepalive_period);
  1346. if (copy_from_user(&period, ifr->ifr_data, len))
  1347. rc = -EFAULT;
  1348. if ((period > 0) && (period <= 32767))
  1349. lp->cisco_keepalive_period = period;
  1350. else
  1351. rc = -EINVAL;
  1352. if (!rc && (tmp != lp->cisco_keepalive_period)) {
  1353. expires = (unsigned long)(jiffies +
  1354. lp->cisco_keepalive_period * HZ);
  1355. mod_timer(&lp->cisco_timer, expires);
  1356. printk(KERN_INFO "%s: Keepalive period set "
  1357. "to %d seconds.\n",
  1358. dev->name, lp->cisco_keepalive_period);
  1359. }
  1360. break;
  1361. /* get/set debugging */
  1362. case SIOCGDEBSERINT:
  1363. len = (unsigned long)sizeof(lp->cisco_debserint);
  1364. if (copy_to_user(ifr->ifr_data,
  1365. &lp->cisco_debserint, len))
  1366. rc = -EFAULT;
  1367. break;
  1368. case SIOCSDEBSERINT:
  1369. len = (unsigned long)sizeof(lp->cisco_debserint);
  1370. if (copy_from_user(&debserint,
  1371. ifr->ifr_data, len))
  1372. rc = -EFAULT;
  1373. if ((debserint >= 0) && (debserint <= 64))
  1374. lp->cisco_debserint = debserint;
  1375. else
  1376. rc = -EINVAL;
  1377. break;
  1378. default:
  1379. rc = -EINVAL;
  1380. break;
  1381. }
  1382. return (rc);
  1383. }
  1384. static int isdn_net_ioctl(struct net_device *dev,
  1385. struct ifreq *ifr, int cmd)
  1386. {
  1387. isdn_net_local *lp = netdev_priv(dev);
  1388. switch (lp->p_encap) {
  1389. #ifdef CONFIG_ISDN_PPP
  1390. case ISDN_NET_ENCAP_SYNCPPP:
  1391. return isdn_ppp_dev_ioctl(dev, ifr, cmd);
  1392. #endif
  1393. case ISDN_NET_ENCAP_CISCOHDLCK:
  1394. return isdn_ciscohdlck_dev_ioctl(dev, ifr, cmd);
  1395. default:
  1396. return -EINVAL;
  1397. }
  1398. }
  1399. /* called via cisco_timer.function */
  1400. static void
  1401. isdn_net_ciscohdlck_slarp_send_keepalive(unsigned long data)
  1402. {
  1403. isdn_net_local *lp = (isdn_net_local *) data;
  1404. struct sk_buff *skb;
  1405. unsigned char *p;
  1406. unsigned long last_cisco_myseq = lp->cisco_myseq;
  1407. int myseq_diff = 0;
  1408. if (!(lp->flags & ISDN_NET_CONNECTED) || lp->dialstate) {
  1409. printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
  1410. return;
  1411. }
  1412. lp->cisco_myseq++;
  1413. myseq_diff = (lp->cisco_myseq - lp->cisco_mineseen);
  1414. if ((lp->cisco_line_state) && ((myseq_diff >= 3)||(myseq_diff <= -3))) {
  1415. /* line up -> down */
  1416. lp->cisco_line_state = 0;
  1417. printk (KERN_WARNING
  1418. "UPDOWN: Line protocol on Interface %s,"
  1419. " changed state to down\n", lp->netdev->dev->name);
  1420. /* should stop routing higher-level data across */
  1421. } else if ((!lp->cisco_line_state) &&
  1422. (myseq_diff >= 0) && (myseq_diff <= 2)) {
  1423. /* line down -> up */
  1424. lp->cisco_line_state = 1;
  1425. printk (KERN_WARNING
  1426. "UPDOWN: Line protocol on Interface %s,"
  1427. " changed state to up\n", lp->netdev->dev->name);
  1428. /* restart routing higher-level data across */
  1429. }
  1430. if (lp->cisco_debserint)
  1431. printk (KERN_DEBUG "%s: HDLC "
  1432. "myseq %lu, mineseen %lu%c, yourseen %lu, %s\n",
  1433. lp->netdev->dev->name, last_cisco_myseq, lp->cisco_mineseen,
  1434. ((last_cisco_myseq == lp->cisco_mineseen) ? '*' : 040),
  1435. lp->cisco_yourseq,
  1436. ((lp->cisco_line_state) ? "line up" : "line down"));
  1437. skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
  1438. if (!skb)
  1439. return;
  1440. p = skb_put(skb, 4 + 14);
  1441. /* cisco header */
  1442. *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
  1443. *(u8 *)(p + 1) = CISCO_CTRL;
  1444. *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
  1445. /* slarp keepalive */
  1446. *(__be32 *)(p + 4) = cpu_to_be32(CISCO_SLARP_KEEPALIVE);
  1447. *(__be32 *)(p + 8) = cpu_to_be32(lp->cisco_myseq);
  1448. *(__be32 *)(p + 12) = cpu_to_be32(lp->cisco_yourseq);
  1449. *(__be16 *)(p + 16) = cpu_to_be16(0xffff); // reliability, always 0xffff
  1450. p += 18;
  1451. isdn_net_write_super(lp, skb);
  1452. lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
  1453. add_timer(&lp->cisco_timer);
  1454. }
  1455. static void
  1456. isdn_net_ciscohdlck_slarp_send_request(isdn_net_local *lp)
  1457. {
  1458. struct sk_buff *skb;
  1459. unsigned char *p;
  1460. skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
  1461. if (!skb)
  1462. return;
  1463. p = skb_put(skb, 4 + 14);
  1464. /* cisco header */
  1465. *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
  1466. *(u8 *)(p + 1) = CISCO_CTRL;
  1467. *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
  1468. /* slarp request */
  1469. *(__be32 *)(p + 4) = cpu_to_be32(CISCO_SLARP_REQUEST);
  1470. *(__be32 *)(p + 8) = cpu_to_be32(0); // address
  1471. *(__be32 *)(p + 12) = cpu_to_be32(0); // netmask
  1472. *(__be16 *)(p + 16) = cpu_to_be16(0); // unused
  1473. p += 18;
  1474. isdn_net_write_super(lp, skb);
  1475. }
  1476. static void
  1477. isdn_net_ciscohdlck_connected(isdn_net_local *lp)
  1478. {
  1479. lp->cisco_myseq = 0;
  1480. lp->cisco_mineseen = 0;
  1481. lp->cisco_yourseq = 0;
  1482. lp->cisco_keepalive_period = ISDN_TIMER_KEEPINT;
  1483. lp->cisco_last_slarp_in = 0;
  1484. lp->cisco_line_state = 0;
  1485. lp->cisco_debserint = 0;
  1486. /* send slarp request because interface/seq.no.s reset */
  1487. isdn_net_ciscohdlck_slarp_send_request(lp);
  1488. init_timer(&lp->cisco_timer);
  1489. lp->cisco_timer.data = (unsigned long) lp;
  1490. lp->cisco_timer.function = isdn_net_ciscohdlck_slarp_send_keepalive;
  1491. lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
  1492. add_timer(&lp->cisco_timer);
  1493. }
  1494. static void
  1495. isdn_net_ciscohdlck_disconnected(isdn_net_local *lp)
  1496. {
  1497. del_timer(&lp->cisco_timer);
  1498. }
  1499. static void
  1500. isdn_net_ciscohdlck_slarp_send_reply(isdn_net_local *lp)
  1501. {
  1502. struct sk_buff *skb;
  1503. unsigned char *p;
  1504. struct in_device *in_dev = NULL;
  1505. __be32 addr = 0; /* local ipv4 address */
  1506. __be32 mask = 0; /* local netmask */
  1507. if ((in_dev = lp->netdev->dev->ip_ptr) != NULL) {
  1508. /* take primary(first) address of interface */
  1509. struct in_ifaddr *ifa = in_dev->ifa_list;
  1510. if (ifa != NULL) {
  1511. addr = ifa->ifa_local;
  1512. mask = ifa->ifa_mask;
  1513. }
  1514. }
  1515. skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
  1516. if (!skb)
  1517. return;
  1518. p = skb_put(skb, 4 + 14);
  1519. /* cisco header */
  1520. *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
  1521. *(u8 *)(p + 1) = CISCO_CTRL;
  1522. *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
  1523. /* slarp reply, send own ip/netmask; if values are nonsense remote
  1524. * should think we are unable to provide it with an address via SLARP */
  1525. *(__be32 *)(p + 4) = cpu_to_be32(CISCO_SLARP_REPLY);
  1526. *(__be32 *)(p + 8) = addr; // address
  1527. *(__be32 *)(p + 12) = mask; // netmask
  1528. *(__be16 *)(p + 16) = cpu_to_be16(0); // unused
  1529. p += 18;
  1530. isdn_net_write_super(lp, skb);
  1531. }
  1532. static void
  1533. isdn_net_ciscohdlck_slarp_in(isdn_net_local *lp, struct sk_buff *skb)
  1534. {
  1535. unsigned char *p;
  1536. int period;
  1537. u32 code;
  1538. u32 my_seq;
  1539. u32 your_seq;
  1540. __be32 local;
  1541. __be32 *addr, *mask;
  1542. if (skb->len < 14)
  1543. return;
  1544. p = skb->data;
  1545. code = be32_to_cpup((__be32 *)p);
  1546. p += 4;
  1547. switch (code) {
  1548. case CISCO_SLARP_REQUEST:
  1549. lp->cisco_yourseq = 0;
  1550. isdn_net_ciscohdlck_slarp_send_reply(lp);
  1551. break;
  1552. case CISCO_SLARP_REPLY:
  1553. addr = (__be32 *)p;
  1554. mask = (__be32 *)(p + 4);
  1555. if (*mask != cpu_to_be32(0xfffffffc))
  1556. goto slarp_reply_out;
  1557. if ((*addr & cpu_to_be32(3)) == cpu_to_be32(0) ||
  1558. (*addr & cpu_to_be32(3)) == cpu_to_be32(3))
  1559. goto slarp_reply_out;
  1560. local = *addr ^ cpu_to_be32(3);
  1561. printk(KERN_INFO "%s: got slarp reply: remote ip: %pI4, local ip: %pI4 mask: %pI4\n",
  1562. lp->netdev->dev->name, addr, &local, mask);
  1563. break;
  1564. slarp_reply_out:
  1565. printk(KERN_INFO "%s: got invalid slarp reply (%pI4/%pI4) - ignored\n",
  1566. lp->netdev->dev->name, addr, mask);
  1567. break;
  1568. case CISCO_SLARP_KEEPALIVE:
  1569. period = (int)((jiffies - lp->cisco_last_slarp_in
  1570. + HZ/2 - 1) / HZ);
  1571. if (lp->cisco_debserint &&
  1572. (period != lp->cisco_keepalive_period) &&
  1573. lp->cisco_last_slarp_in) {
  1574. printk(KERN_DEBUG "%s: Keepalive period mismatch - "
  1575. "is %d but should be %d.\n",
  1576. lp->netdev->dev->name, period,
  1577. lp->cisco_keepalive_period);
  1578. }
  1579. lp->cisco_last_slarp_in = jiffies;
  1580. my_seq = be32_to_cpup((__be32 *)(p + 0));
  1581. your_seq = be32_to_cpup((__be32 *)(p + 4));
  1582. p += 10;
  1583. lp->cisco_yourseq = my_seq;
  1584. lp->cisco_mineseen = your_seq;
  1585. break;
  1586. }
  1587. }
  1588. static void
  1589. isdn_net_ciscohdlck_receive(isdn_net_local *lp, struct sk_buff *skb)
  1590. {
  1591. unsigned char *p;
  1592. u8 addr;
  1593. u8 ctrl;
  1594. u16 type;
  1595. if (skb->len < 4)
  1596. goto out_free;
  1597. p = skb->data;
  1598. addr = *(u8 *)(p + 0);
  1599. ctrl = *(u8 *)(p + 1);
  1600. type = be16_to_cpup((__be16 *)(p + 2));
  1601. p += 4;
  1602. skb_pull(skb, 4);
  1603. if (addr != CISCO_ADDR_UNICAST && addr != CISCO_ADDR_BROADCAST) {
  1604. printk(KERN_WARNING "%s: Unknown Cisco addr 0x%02x\n",
  1605. lp->netdev->dev->name, addr);
  1606. goto out_free;
  1607. }
  1608. if (ctrl != CISCO_CTRL) {
  1609. printk(KERN_WARNING "%s: Unknown Cisco ctrl 0x%02x\n",
  1610. lp->netdev->dev->name, ctrl);
  1611. goto out_free;
  1612. }
  1613. switch (type) {
  1614. case CISCO_TYPE_SLARP:
  1615. isdn_net_ciscohdlck_slarp_in(lp, skb);
  1616. goto out_free;
  1617. case CISCO_TYPE_CDP:
  1618. if (lp->cisco_debserint)
  1619. printk(KERN_DEBUG "%s: Received CDP packet. use "
  1620. "\"no cdp enable\" on cisco.\n",
  1621. lp->netdev->dev->name);
  1622. goto out_free;
  1623. default:
  1624. /* no special cisco protocol */
  1625. skb->protocol = htons(type);
  1626. netif_rx(skb);
  1627. return;
  1628. }
  1629. out_free:
  1630. kfree_skb(skb);
  1631. }
  1632. /*
  1633. * Got a packet from ISDN-Channel.
  1634. */
  1635. static void
  1636. isdn_net_receive(struct net_device *ndev, struct sk_buff *skb)
  1637. {
  1638. isdn_net_local *lp = netdev_priv(ndev);
  1639. isdn_net_local *olp = lp; /* original 'lp' */
  1640. #ifdef CONFIG_ISDN_X25
  1641. struct concap_proto *cprot = lp -> netdev -> cprot;
  1642. #endif
  1643. lp->transcount += skb->len;
  1644. lp->stats.rx_packets++;
  1645. lp->stats.rx_bytes += skb->len;
  1646. if (lp->master) {
  1647. /* Bundling: If device is a slave-device, deliver to master, also
  1648. * handle master's statistics and hangup-timeout
  1649. */
  1650. ndev = lp->master;
  1651. lp = netdev_priv(ndev);
  1652. lp->stats.rx_packets++;
  1653. lp->stats.rx_bytes += skb->len;
  1654. }
  1655. skb->dev = ndev;
  1656. skb->pkt_type = P