2 * Generic HDLC support routines for Linux
5 * Copyright (C) 1999 - 2003 Krzysztof Halasa <khc@pm.waw.pl>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License
9 * as published by the Free Software Foundation.
16 (exist,new) -> 0,0 when "PVC create" or if "link unreliable"
17 0,x -> 1,1 if "link reliable" when sending FULL STATUS
18 1,1 -> 1,0 if received FULL STATUS ACK
20 (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create"
21 -> 1 when "PVC up" and (exist,new) = 1,0
24 (exist,new,active) = FULL STATUS if "link reliable"
25 = 0, 0, 0 if "link unreliable"
27 active = open and "link reliable"
28 exist = new = not used
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/errno.h>
37 #include <linux/if_arp.h>
38 #include <linux/init.h>
39 #include <linux/skbuff.h>
40 #include <linux/pkt_sched.h>
41 #include <linux/random.h>
42 #include <linux/inetdevice.h>
43 #include <linux/lapb.h>
44 #include <linux/rtnetlink.h>
45 #include <linux/etherdevice.h>
46 #include <linux/hdlc.h>
52 #define MAXLEN_LMISTAT 20 /* max size of status enquiry frame */
54 #define PVC_STATE_NEW 0x01
55 #define PVC_STATE_ACTIVE 0x02
56 #define PVC_STATE_FECN 0x08 /* FECN condition */
57 #define PVC_STATE_BECN 0x10 /* BECN condition */
64 #define NLPID_IPV6 0x8E
65 #define NLPID_SNAP 0x80
66 #define NLPID_PAD 0x00
67 #define NLPID_Q933 0x08
70 #define LMI_DLCI 0 /* LMI DLCI */
71 #define LMI_PROTO 0x08
72 #define LMI_CALLREF 0x00 /* Call Reference */
73 #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI lockshift */
74 #define LMI_REPTYPE 1 /* report type */
75 #define LMI_CCITT_REPTYPE 0x51
76 #define LMI_ALIVE 3 /* keep alive */
77 #define LMI_CCITT_ALIVE 0x53
78 #define LMI_PVCSTAT 7 /* pvc status */
79 #define LMI_CCITT_PVCSTAT 0x57
80 #define LMI_FULLREP 0 /* full report */
81 #define LMI_INTEGRITY 1 /* link integrity report */
82 #define LMI_SINGLE 2 /* single pvc report */
83 #define LMI_STATUS_ENQUIRY 0x75
84 #define LMI_STATUS 0x7D /* reply */
86 #define LMI_REPT_LEN 1 /* report type element length */
87 #define LMI_INTEG_LEN 2 /* link integrity element length */
89 #define LMI_LENGTH 13 /* standard LMI frame length */
90 #define LMI_ANSI_LENGTH 14
94 #if defined(__LITTLE_ENDIAN_BITFIELD)
115 }__attribute__ ((packed)) fr_hdr;
118 static inline u16 q922_to_dlci(u8 *hdr)
120 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
125 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
127 hdr[0] = (dlci >> 2) & 0xFC;
128 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
133 static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
135 pvc_device *pvc = hdlc->state.fr.first_pvc;
138 if (pvc->dlci == dlci)
140 if (pvc->dlci > dlci)
141 return NULL; /* the listed is sorted */
149 static inline pvc_device* add_pvc(struct net_device *dev, u16 dlci)
151 hdlc_device *hdlc = dev_to_hdlc(dev);
152 pvc_device *pvc, **pvc_p = &hdlc->state.fr.first_pvc;
155 if ((*pvc_p)->dlci == dlci)
157 if ((*pvc_p)->dlci > dlci)
158 break; /* the list is sorted */
159 pvc_p = &(*pvc_p)->next;
162 pvc = kmalloc(sizeof(pvc_device), GFP_ATOMIC);
166 memset(pvc, 0, sizeof(pvc_device));
169 pvc->next = *pvc_p; /* Put it in the chain */
175 static inline int pvc_is_used(pvc_device *pvc)
177 return pvc->main != NULL || pvc->ether != NULL;
181 static inline void pvc_carrier(int on, pvc_device *pvc)
185 if (!netif_carrier_ok(pvc->main))
186 netif_carrier_on(pvc->main);
188 if (!netif_carrier_ok(pvc->ether))
189 netif_carrier_on(pvc->ether);
192 if (netif_carrier_ok(pvc->main))
193 netif_carrier_off(pvc->main);
195 if (netif_carrier_ok(pvc->ether))
196 netif_carrier_off(pvc->ether);
201 static inline void delete_unused_pvcs(hdlc_device *hdlc)
203 pvc_device **pvc_p = &hdlc->state.fr.first_pvc;
206 if (!pvc_is_used(*pvc_p)) {
207 pvc_device *pvc = *pvc_p;
212 pvc_p = &(*pvc_p)->next;
217 static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
219 if (type == ARPHRD_ETHER)
226 static inline u16 status_to_dlci(u8 *status, int *active, int *new)
228 *new = (status[2] & 0x08) ? 1 : 0;
229 *active = (status[2] & 0x02) ? 1 : 0;
231 return ((status[0] & 0x3F) << 4) | ((status[1] & 0x78) >> 3);
235 static inline void dlci_to_status(u16 dlci, u8 *status, int active, int new)
237 status[0] = (dlci >> 4) & 0x3F;
238 status[1] = ((dlci << 3) & 0x78) | 0x80;
249 static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
252 struct sk_buff *skb = *skb_p;
254 switch (skb->protocol) {
255 case __constant_ntohs(ETH_P_IP):
257 skb_push(skb, head_len);
258 skb->data[3] = NLPID_IP;
261 case __constant_ntohs(ETH_P_IPV6):
263 skb_push(skb, head_len);
264 skb->data[3] = NLPID_IPV6;
267 case __constant_ntohs(LMI_PROTO):
269 skb_push(skb, head_len);
270 skb->data[3] = LMI_PROTO;
273 case __constant_ntohs(ETH_P_802_3):
275 if (skb_headroom(skb) < head_len) {
276 struct sk_buff *skb2 = skb_realloc_headroom(skb,
283 skb_push(skb, head_len);
284 skb->data[3] = FR_PAD;
285 skb->data[4] = NLPID_SNAP;
286 skb->data[5] = FR_PAD;
290 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
295 skb_push(skb, head_len);
296 skb->data[3] = FR_PAD;
297 skb->data[4] = NLPID_SNAP;
298 skb->data[5] = FR_PAD;
299 skb->data[6] = FR_PAD;
300 skb->data[7] = FR_PAD;
301 *(u16*)(skb->data + 8) = skb->protocol;
304 dlci_to_q922(skb->data, dlci);
305 skb->data[2] = FR_UI;
311 static int pvc_open(struct net_device *dev)
313 pvc_device *pvc = dev_to_pvc(dev);
315 if ((pvc->master->flags & IFF_UP) == 0)
316 return -EIO; /* Master must be UP in order to activate PVC */
318 if (pvc->open_count++ == 0) {
319 hdlc_device *hdlc = dev_to_hdlc(pvc->master);
320 if (hdlc->state.fr.settings.lmi == LMI_NONE)
321 pvc->state.active = hdlc->carrier;
323 pvc_carrier(pvc->state.active, pvc);
324 hdlc->state.fr.dce_changed = 1;
331 static int pvc_close(struct net_device *dev)
333 pvc_device *pvc = dev_to_pvc(dev);
335 if (--pvc->open_count == 0) {
336 hdlc_device *hdlc = dev_to_hdlc(pvc->master);
337 if (hdlc->state.fr.settings.lmi == LMI_NONE)
338 pvc->state.active = 0;
340 if (hdlc->state.fr.settings.dce) {
341 hdlc->state.fr.dce_changed = 1;
342 pvc->state.active = 0;
350 int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
352 pvc_device *pvc = dev_to_pvc(dev);
353 fr_proto_pvc_info info;
355 if (ifr->ifr_settings.type == IF_GET_PROTO) {
356 if (dev->type == ARPHRD_ETHER)
357 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
359 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
361 if (ifr->ifr_settings.size < sizeof(info)) {
362 /* data size wanted */
363 ifr->ifr_settings.size = sizeof(info);
367 info.dlci = pvc->dlci;
368 memcpy(info.master, pvc->master->name, IFNAMSIZ);
369 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
370 &info, sizeof(info)))
379 static inline struct net_device_stats *pvc_get_stats(struct net_device *dev)
381 return netdev_priv(dev);
386 static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
388 pvc_device *pvc = dev_to_pvc(dev);
389 struct net_device_stats *stats = pvc_get_stats(dev);
391 if (pvc->state.active) {
392 if (dev->type == ARPHRD_ETHER) {
393 int pad = ETH_ZLEN - skb->len;
394 if (pad > 0) { /* Pad the frame with zeros */
396 if (skb_tailroom(skb) < pad)
397 if (pskb_expand_head(skb, 0, pad,
404 memset(skb->data + len, 0, pad);
406 skb->protocol = __constant_htons(ETH_P_802_3);
408 if (!fr_hard_header(&skb, pvc->dlci)) {
409 stats->tx_bytes += skb->len;
411 if (pvc->state.fecn) /* TX Congestion counter */
412 stats->tx_compressed++;
413 skb->dev = pvc->master;
426 static int pvc_change_mtu(struct net_device *dev, int new_mtu)
428 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
436 static inline void fr_log_dlci_active(pvc_device *pvc)
438 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
441 pvc->main ? pvc->main->name : "",
442 pvc->main && pvc->ether ? " " : "",
443 pvc->ether ? pvc->ether->name : "",
444 pvc->state.new ? " new" : "",
445 !pvc->state.exist ? "deleted" :
446 pvc->state.active ? "active" : "inactive");
451 static inline u8 fr_lmi_nextseq(u8 x)
459 static void fr_lmi_send(struct net_device *dev, int fullrep)
461 hdlc_device *hdlc = dev_to_hdlc(dev);
463 pvc_device *pvc = hdlc->state.fr.first_pvc;
464 int len = (hdlc->state.fr.settings.lmi == LMI_ANSI) ? LMI_ANSI_LENGTH
470 if (hdlc->state.fr.settings.dce && fullrep) {
471 len += hdlc->state.fr.dce_pvc_count * (2 + stat_len);
472 if (len > HDLC_MAX_MRU) {
473 printk(KERN_WARNING "%s: Too many PVCs while sending "
474 "LMI full report\n", dev->name);
479 skb = dev_alloc_skb(len);
481 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
485 memset(skb->data, 0, len);
487 skb->protocol = __constant_htons(LMI_PROTO);
488 fr_hard_header(&skb, LMI_DLCI);
490 data[i++] = LMI_CALLREF;
491 data[i++] = hdlc->state.fr.settings.dce
492 ? LMI_STATUS : LMI_STATUS_ENQUIRY;
493 if (hdlc->state.fr.settings.lmi == LMI_ANSI)
494 data[i++] = LMI_ANSI_LOCKSHIFT;
495 data[i++] = (hdlc->state.fr.settings.lmi == LMI_CCITT)
496 ? LMI_CCITT_REPTYPE : LMI_REPTYPE;
497 data[i++] = LMI_REPT_LEN;
498 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
500 data[i++] = (hdlc->state.fr.settings.lmi == LMI_CCITT)
501 ? LMI_CCITT_ALIVE : LMI_ALIVE;
502 data[i++] = LMI_INTEG_LEN;
503 data[i++] = hdlc->state.fr.txseq =fr_lmi_nextseq(hdlc->state.fr.txseq);
504 data[i++] = hdlc->state.fr.rxseq;
506 if (hdlc->state.fr.settings.dce && fullrep) {
508 data[i++] = (hdlc->state.fr.settings.lmi == LMI_CCITT)
509 ? LMI_CCITT_PVCSTAT : LMI_PVCSTAT;
510 data[i++] = stat_len;
512 /* LMI start/restart */
513 if (hdlc->state.fr.reliable && !pvc->state.exist) {
514 pvc->state.exist = pvc->state.new = 1;
515 fr_log_dlci_active(pvc);
518 /* ifconfig PVC up */
519 if (pvc->open_count && !pvc->state.active &&
520 pvc->state.exist && !pvc->state.new) {
522 pvc->state.active = 1;
523 fr_log_dlci_active(pvc);
526 dlci_to_status(pvc->dlci, data + i,
527 pvc->state.active, pvc->state.new);
534 skb->priority = TC_PRIO_CONTROL;
536 skb->nh.raw = skb->data;
543 static void fr_set_link_state(int reliable, struct net_device *dev)
545 hdlc_device *hdlc = dev_to_hdlc(dev);
546 pvc_device *pvc = hdlc->state.fr.first_pvc;
548 hdlc->state.fr.reliable = reliable;
550 if (!netif_carrier_ok(dev))
551 netif_carrier_on(dev);
553 hdlc->state.fr.n391cnt = 0; /* Request full status */
554 hdlc->state.fr.dce_changed = 1;
556 if (hdlc->state.fr.settings.lmi == LMI_NONE) {
557 while (pvc) { /* Activate all PVCs */
559 pvc->state.exist = pvc->state.active = 1;
565 if (netif_carrier_ok(dev))
566 netif_carrier_off(dev);
568 while (pvc) { /* Deactivate all PVCs */
570 pvc->state.exist = pvc->state.active = 0;
579 static void fr_timer(unsigned long arg)
581 struct net_device *dev = (struct net_device *)arg;
582 hdlc_device *hdlc = dev_to_hdlc(dev);
583 int i, cnt = 0, reliable;
586 if (hdlc->state.fr.settings.dce)
587 reliable = (jiffies - hdlc->state.fr.last_poll <
588 hdlc->state.fr.settings.t392 * HZ);
590 hdlc->state.fr.last_errors <<= 1; /* Shift the list */
591 if (hdlc->state.fr.request) {
592 if (hdlc->state.fr.reliable)
593 printk(KERN_INFO "%s: No LMI status reply "
594 "received\n", dev->name);
595 hdlc->state.fr.last_errors |= 1;
598 list = hdlc->state.fr.last_errors;
599 for (i = 0; i < hdlc->state.fr.settings.n393; i++, list >>= 1)
600 cnt += (list & 1); /* errors count */
602 reliable = (cnt < hdlc->state.fr.settings.n392);
605 if (hdlc->state.fr.reliable != reliable) {
606 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
607 reliable ? "" : "un");
608 fr_set_link_state(reliable, dev);
611 if (hdlc->state.fr.settings.dce)
612 hdlc->state.fr.timer.expires = jiffies +
613 hdlc->state.fr.settings.t392 * HZ;
615 if (hdlc->state.fr.n391cnt)
616 hdlc->state.fr.n391cnt--;
618 fr_lmi_send(dev, hdlc->state.fr.n391cnt == 0);
620 hdlc->state.fr.request = 1;
621 hdlc->state.fr.timer.expires = jiffies +
622 hdlc->state.fr.settings.t391 * HZ;
625 hdlc->state.fr.timer.function = fr_timer;
626 hdlc->state.fr.timer.data = arg;
627 add_timer(&hdlc->state.fr.timer);
632 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
634 hdlc_device *hdlc = dev_to_hdlc(dev);
637 int reptype = -1, error, no_ram;
641 if (skb->len < ((hdlc->state.fr.settings.lmi == LMI_ANSI)
642 ? LMI_ANSI_LENGTH : LMI_LENGTH)) {
643 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
647 if (skb->data[5] != (!hdlc->state.fr.settings.dce ?
648 LMI_STATUS : LMI_STATUS_ENQUIRY)) {
649 printk(KERN_INFO "%s: LMI msgtype=%x, Not LMI status %s\n",
650 dev->name, skb->data[2],
651 hdlc->state.fr.settings.dce ? "enquiry" : "reply");
655 i = (hdlc->state.fr.settings.lmi == LMI_ANSI) ? 7 : 6;
658 ((hdlc->state.fr.settings.lmi == LMI_CCITT)
659 ? LMI_CCITT_REPTYPE : LMI_REPTYPE)) {
660 printk(KERN_INFO "%s: Not a report type=%x\n",
661 dev->name, skb->data[i]);
666 i++; /* Skip length field */
668 reptype = skb->data[i++];
671 ((hdlc->state.fr.settings.lmi == LMI_CCITT)
672 ? LMI_CCITT_ALIVE : LMI_ALIVE)) {
673 printk(KERN_INFO "%s: Unsupported status element=%x\n",
674 dev->name, skb->data[i]);
679 i++; /* Skip length field */
681 hdlc->state.fr.rxseq = skb->data[i++]; /* TX sequence from peer */
682 rxseq = skb->data[i++]; /* Should confirm our sequence */
684 txseq = hdlc->state.fr.txseq;
686 if (hdlc->state.fr.settings.dce) {
687 if (reptype != LMI_FULLREP && reptype != LMI_INTEGRITY) {
688 printk(KERN_INFO "%s: Unsupported report type=%x\n",
695 if (!hdlc->state.fr.reliable)
698 if (rxseq == 0 || rxseq != txseq) {
699 hdlc->state.fr.n391cnt = 0; /* Ask for full report next time */
703 if (hdlc->state.fr.settings.dce) {
704 if (hdlc->state.fr.fullrep_sent && !error) {
705 /* Stop sending full report - the last one has been confirmed by DTE */
706 hdlc->state.fr.fullrep_sent = 0;
707 pvc = hdlc->state.fr.first_pvc;
709 if (pvc->state.new) {
712 /* Tell DTE that new PVC is now active */
713 hdlc->state.fr.dce_changed = 1;
719 if (hdlc->state.fr.dce_changed) {
720 reptype = LMI_FULLREP;
721 hdlc->state.fr.fullrep_sent = 1;
722 hdlc->state.fr.dce_changed = 0;
725 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
731 if (reptype != LMI_FULLREP || error)
735 pvc = hdlc->state.fr.first_pvc;
738 pvc->state.deleted = 1;
743 while (skb->len >= i + 2 + stat_len) {
745 unsigned int active, new;
747 if (skb->data[i] != ((hdlc->state.fr.settings.lmi == LMI_CCITT)
748 ? LMI_CCITT_PVCSTAT : LMI_PVCSTAT)) {
749 printk(KERN_WARNING "%s: Invalid PVCSTAT ID: %x\n",
750 dev->name, skb->data[i]);
755 if (skb->data[i] != stat_len) {
756 printk(KERN_WARNING "%s: Invalid PVCSTAT length: %x\n",
757 dev->name, skb->data[i]);
762 dlci = status_to_dlci(skb->data + i, &active, &new);
764 pvc = add_pvc(dev, dlci);
766 if (!pvc && !no_ram) {
768 "%s: Memory squeeze on fr_lmi_recv()\n",
774 pvc->state.exist = 1;
775 pvc->state.deleted = 0;
776 if (active != pvc->state.active ||
777 new != pvc->state.new ||
779 pvc->state.new = new;
780 pvc->state.active = active;
781 pvc_carrier(active, pvc);
782 fr_log_dlci_active(pvc);
789 pvc = hdlc->state.fr.first_pvc;
792 if (pvc->state.deleted && pvc->state.exist) {
794 pvc->state.active = pvc->state.new = 0;
795 pvc->state.exist = 0;
796 fr_log_dlci_active(pvc);
801 /* Next full report after N391 polls */
802 hdlc->state.fr.n391cnt = hdlc->state.fr.settings.n391;
809 static int fr_rx(struct sk_buff *skb)
811 struct net_device *ndev = skb->dev;
812 hdlc_device *hdlc = dev_to_hdlc(ndev);
813 fr_hdr *fh = (fr_hdr*)skb->data;
814 u8 *data = skb->data;
817 struct net_device *dev = NULL;
819 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
822 dlci = q922_to_dlci(skb->data);
824 if (dlci == LMI_DLCI) {
825 if (hdlc->state.fr.settings.lmi == LMI_NONE)
826 goto rx_error; /* LMI packet with no LMI? */
828 if (data[3] == LMI_PROTO) {
829 if (fr_lmi_recv(ndev, skb))
832 /* No request pending */
833 hdlc->state.fr.request = 0;
834 hdlc->state.fr.last_poll = jiffies;
835 dev_kfree_skb_any(skb);
836 return NET_RX_SUCCESS;
840 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
845 pvc = find_pvc(hdlc, dlci);
848 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
851 dev_kfree_skb_any(skb);
855 if (pvc->state.fecn != fh->fecn) {
857 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", ndev->name,
858 dlci, fh->fecn ? "N" : "FF");
860 pvc->state.fecn ^= 1;
863 if (pvc->state.becn != fh->becn) {
865 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", ndev->name,
866 dlci, fh->becn ? "N" : "FF");
868 pvc->state.becn ^= 1;
872 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
873 hdlc->stats.rx_dropped++;
877 if (data[3] == NLPID_IP) {
878 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
880 skb->protocol = htons(ETH_P_IP);
882 } else if (data[3] == NLPID_IPV6) {
883 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
885 skb->protocol = htons(ETH_P_IPV6);
887 } else if (skb->len > 10 && data[3] == FR_PAD &&
888 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
889 u16 oui = ntohs(*(u16*)(data + 6));
890 u16 pid = ntohs(*(u16*)(data + 8));
893 switch ((((u32)oui) << 16) | pid) {
894 case ETH_P_ARP: /* routed frame with SNAP */
896 case ETH_P_IP: /* a long variant */
899 skb->protocol = htons(pid);
902 case 0x80C20007: /* bridged Ethernet frame */
903 if ((dev = pvc->ether) != NULL)
904 skb->protocol = eth_type_trans(skb, dev);
908 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
909 "PID=%x\n", ndev->name, oui, pid);
910 dev_kfree_skb_any(skb);
914 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
915 "length = %i\n", ndev->name, data[3], skb->len);
916 dev_kfree_skb_any(skb);
921 struct net_device_stats *stats = pvc_get_stats(dev);
922 stats->rx_packets++; /* PVC traffic */
923 stats->rx_bytes += skb->len;
925 stats->rx_compressed++;
928 return NET_RX_SUCCESS;
930 dev_kfree_skb_any(skb);
935 hdlc->stats.rx_errors++; /* Mark error */
936 dev_kfree_skb_any(skb);
942 static void fr_start(struct net_device *dev)
944 hdlc_device *hdlc = dev_to_hdlc(dev);
946 printk(KERN_DEBUG "fr_start\n");
948 if (hdlc->state.fr.settings.lmi != LMI_NONE) {
949 if (netif_carrier_ok(dev))
950 netif_carrier_off(dev);
951 hdlc->state.fr.last_poll = 0;
952 hdlc->state.fr.reliable = 0;
953 hdlc->state.fr.dce_changed = 1;
954 hdlc->state.fr.request = 0;
955 hdlc->state.fr.fullrep_sent = 0;
956 hdlc->state.fr.last_errors = 0xFFFFFFFF;
957 hdlc->state.fr.n391cnt = 0;
958 hdlc->state.fr.txseq = hdlc->state.fr.rxseq = 0;
960 init_timer(&hdlc->state.fr.timer);
961 /* First poll after 1 s */
962 hdlc->state.fr.timer.expires = jiffies + HZ;
963 hdlc->state.fr.timer.function = fr_timer;
964 hdlc->state.fr.timer.data = (unsigned long)dev;
965 add_timer(&hdlc->state.fr.timer);
967 fr_set_link_state(1, dev);
972 static void fr_stop(struct net_device *dev)
974 hdlc_device *hdlc = dev_to_hdlc(dev);
976 printk(KERN_DEBUG "fr_stop\n");
978 if (hdlc->state.fr.settings.lmi != LMI_NONE)
979 del_timer_sync(&hdlc->state.fr.timer);
980 fr_set_link_state(0, dev);
985 static void fr_close(struct net_device *dev)
987 hdlc_device *hdlc = dev_to_hdlc(dev);
988 pvc_device *pvc = hdlc->state.fr.first_pvc;
990 while (pvc) { /* Shutdown all PVCs for this FRAD */
992 dev_close(pvc->main);
994 dev_close(pvc->ether);
999 static void dlci_setup(struct net_device *dev)
1001 dev->type = ARPHRD_DLCI;
1002 dev->flags = IFF_POINTOPOINT;
1003 dev->hard_header_len = 10;
1007 static int fr_add_pvc(struct net_device *master, unsigned int dlci, int type)
1009 hdlc_device *hdlc = dev_to_hdlc(master);
1010 pvc_device *pvc = NULL;
1011 struct net_device *dev;
1013 char * prefix = "pvc%d";
1015 if (type == ARPHRD_ETHER)
1016 prefix = "pvceth%d";
1018 if ((pvc = add_pvc(master, dlci)) == NULL) {
1019 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
1024 if (*get_dev_p(pvc, type))
1027 used = pvc_is_used(pvc);
1029 if (type == ARPHRD_ETHER)
1030 dev = alloc_netdev(sizeof(struct net_device_stats),
1031 "pvceth%d", ether_setup);
1033 dev = alloc_netdev(sizeof(struct net_device_stats),
1034 "pvc%d", dlci_setup);
1037 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
1039 delete_unused_pvcs(hdlc);
1043 if (type == ARPHRD_ETHER) {
1044 memcpy(dev->dev_addr, "\x00\x01", 2);
1045 get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
1047 *(u16*)dev->dev_addr = htons(dlci);
1048 dlci_to_q922(dev->broadcast, dlci);
1050 dev->hard_start_xmit = pvc_xmit;
1051 dev->get_stats = pvc_get_stats;
1052 dev->open = pvc_open;
1053 dev->stop = pvc_close;
1054 dev->do_ioctl = pvc_ioctl;
1055 dev->change_mtu = pvc_change_mtu;
1056 dev->mtu = HDLC_MAX_MTU;
1057 dev->tx_queue_len = 0;
1060 result = dev_alloc_name(dev, dev->name);
1063 delete_unused_pvcs(hdlc);
1067 if (register_netdevice(dev) != 0) {
1069 delete_unused_pvcs(hdlc);
1073 dev->destructor = free_netdev;
1074 *get_dev_p(pvc, type) = dev;
1076 hdlc->state.fr.dce_changed = 1;
1077 hdlc->state.fr.dce_pvc_count++;
1084 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1087 struct net_device *dev;
1089 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1092 if ((dev = *get_dev_p(pvc, type)) == NULL)
1095 if (dev->flags & IFF_UP)
1096 return -EBUSY; /* PVC in use */
1098 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1099 *get_dev_p(pvc, type) = NULL;
1101 if (!pvc_is_used(pvc)) {
1102 hdlc->state.fr.dce_pvc_count--;
1103 hdlc->state.fr.dce_changed = 1;
1105 delete_unused_pvcs(hdlc);
1111 static void fr_destroy(hdlc_device *hdlc)
1115 pvc = hdlc->state.fr.first_pvc;
1116 hdlc->state.fr.first_pvc = NULL; /* All PVCs destroyed */
1117 hdlc->state.fr.dce_pvc_count = 0;
1118 hdlc->state.fr.dce_changed = 1;
1121 pvc_device *next = pvc->next;
1122 /* destructors will free_netdev() main and ether */
1124 unregister_netdevice(pvc->main);
1127 unregister_netdevice(pvc->ether);
1136 int hdlc_fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1138 fr_proto *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1139 const size_t size = sizeof(fr_proto);
1140 fr_proto new_settings;
1141 hdlc_device *hdlc = dev_to_hdlc(dev);
1145 switch (ifr->ifr_settings.type) {
1147 ifr->ifr_settings.type = IF_PROTO_FR;
1148 if (ifr->ifr_settings.size < size) {
1149 ifr->ifr_settings.size = size; /* data size wanted */
1152 if (copy_to_user(fr_s, &hdlc->state.fr.settings, size))
1157 if(!capable(CAP_NET_ADMIN))
1160 if(dev->flags & IFF_UP)
1163 if (copy_from_user(&new_settings, fr_s, size))
1166 if (new_settings.lmi == LMI_DEFAULT)
1167 new_settings.lmi = LMI_ANSI;
1169 if ((new_settings.lmi != LMI_NONE &&
1170 new_settings.lmi != LMI_ANSI &&
1171 new_settings.lmi != LMI_CCITT) ||
1172 new_settings.t391 < 1 ||
1173 new_settings.t392 < 2 ||
1174 new_settings.n391 < 1 ||
1175 new_settings.n392 < 1 ||
1176 new_settings.n393 < new_settings.n392 ||
1177 new_settings.n393 > 32 ||
1178 (new_settings.dce != 0 &&
1179 new_settings.dce != 1))
1182 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1186 if (hdlc->proto.id != IF_PROTO_FR) {
1187 hdlc_proto_detach(hdlc);
1188 hdlc->state.fr.first_pvc = NULL;
1189 hdlc->state.fr.dce_pvc_count = 0;
1191 memcpy(&hdlc->state.fr.settings, &new_settings, size);
1192 memset(&hdlc->proto, 0, sizeof(hdlc->proto));
1194 hdlc->proto.close = fr_close;
1195 hdlc->proto.start = fr_start;
1196 hdlc->proto.stop = fr_stop;
1197 hdlc->proto.detach = fr_destroy;
1198 hdlc->proto.netif_rx = fr_rx;
1199 hdlc->proto.id = IF_PROTO_FR;
1200 dev->hard_start_xmit = hdlc->xmit;
1201 dev->hard_header = NULL;
1202 dev->type = ARPHRD_FRAD;
1203 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1207 case IF_PROTO_FR_ADD_PVC:
1208 case IF_PROTO_FR_DEL_PVC:
1209 case IF_PROTO_FR_ADD_ETH_PVC:
1210 case IF_PROTO_FR_DEL_ETH_PVC:
1211 if(!capable(CAP_NET_ADMIN))
1214 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1215 sizeof(fr_proto_pvc)))
1218 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1219 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1221 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1222 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1223 result = ARPHRD_ETHER; /* bridged Ethernet device */
1225 result = ARPHRD_DLCI;
1227 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1228 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1229 return fr_add_pvc(dev, pvc.dlci, result);
1231 return fr_del_pvc(hdlc, pvc.dlci, result);