36 #include <linux/errno.h>
38 #include <linux/hdlc.h>
39 #include <linux/if_arp.h>
42 #include <linux/kernel.h>
43 #include <linux/module.h>
45 #include <linux/poll.h>
46 #include <linux/rtnetlink.h>
48 #include <linux/slab.h>
60 #define NLPID_IPV6 0x8E
61 #define NLPID_SNAP 0x80
62 #define NLPID_PAD 0x00
63 #define NLPID_CCITT_ANSI_LMI 0x08
64 #define NLPID_CISCO_LMI 0x09
67 #define LMI_CCITT_ANSI_DLCI 0
68 #define LMI_CISCO_DLCI 1023
70 #define LMI_CALLREF 0x00
71 #define LMI_ANSI_LOCKSHIFT 0x95
72 #define LMI_ANSI_CISCO_REPTYPE 0x01
73 #define LMI_CCITT_REPTYPE 0x51
74 #define LMI_ANSI_CISCO_ALIVE 0x03
75 #define LMI_CCITT_ALIVE 0x53
76 #define LMI_ANSI_CISCO_PVCSTAT 0x07
77 #define LMI_CCITT_PVCSTAT 0x57
79 #define LMI_FULLREP 0x00
80 #define LMI_INTEGRITY 0x01
81 #define LMI_SINGLE 0x02
83 #define LMI_STATUS_ENQUIRY 0x75
84 #define LMI_STATUS 0x7D
86 #define LMI_REPT_LEN 1
87 #define LMI_INTEG_LEN 2
89 #define LMI_CCITT_CISCO_LENGTH 13
90 #define LMI_ANSI_LENGTH 14
94 #if defined(__LITTLE_ENDIAN_BITFIELD)
158 static inline u16 q922_to_dlci(
u8 *
hdr)
160 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
164 static inline void dlci_to_q922(
u8 *hdr,
u16 dlci)
166 hdr[0] = (dlci >> 2) & 0xFC;
167 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
182 if (pvc->
dlci == dlci)
184 if (pvc->
dlci > dlci)
199 if ((*pvc_p)->dlci == dlci)
201 if ((*pvc_p)->dlci > dlci)
203 pvc_p = &(*pvc_p)->
next;
221 static inline int pvc_is_used(
pvc_device *pvc)
227 static inline void pvc_carrier(
int on,
pvc_device *pvc)
231 if (!netif_carrier_ok(pvc->
main))
234 if (!netif_carrier_ok(pvc->
ether))
238 if (netif_carrier_ok(pvc->
main))
241 if (netif_carrier_ok(pvc->
ether))
247 static inline void delete_unused_pvcs(
hdlc_device *hdlc)
252 if (!pvc_is_used(*pvc_p)) {
261 pvc_p = &(*pvc_p)->
next;
275 static int fr_hard_header(
struct sk_buff **skb_p,
u16 dlci)
307 if (skb_headroom(skb) < head_len) {
336 dlci_to_q922(skb->
data, dlci);
353 pvc->
state.active = netif_carrier_ok(pvc->
frad);
355 pvc_carrier(pvc->
state.active, pvc);
356 state(hdlc)->dce_changed = 1;
370 pvc->
state.active = 0;
372 if (
state(hdlc)->settings.dce) {
373 state(hdlc)->dce_changed = 1;
374 pvc->
state.active = 0;
393 if (ifr->ifr_settings.size <
sizeof(info)) {
395 ifr->ifr_settings.size =
sizeof(
info);
401 if (
copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
402 &info,
sizeof(info)))
414 if (pvc->
state.active) {
419 if (skb_tailroom(skb) < pad)
422 dev->
stats.tx_dropped++;
431 if (!fr_hard_header(&skb, pvc->
dlci)) {
433 dev->
stats.tx_packets++;
435 dev->
stats.tx_compressed++;
442 dev->
stats.tx_dropped++;
447 static inline void fr_log_dlci_active(
pvc_device *pvc)
449 netdev_info(pvc->
frad,
"DLCI %d [%s%s%s]%s %s\n",
454 pvc->
state.new ?
" new" :
"",
455 !pvc->
state.exist ?
"deleted" :
456 pvc->
state.active ?
"active" :
"inactive");
461 static inline u8 fr_lmi_nextseq(
u8 x)
468 static void fr_lmi_send(
struct net_device *dev,
int fullrep)
473 int lmi =
state(hdlc)->settings.lmi;
474 int dce =
state(hdlc)->settings.dce;
476 int stat_len = (lmi ==
LMI_CISCO) ? 6 : 3;
480 if (dce && fullrep) {
481 len +=
state(hdlc)->dce_pvc_count * (2 + stat_len);
483 netdev_warn(dev,
"Too many PVCs while sending LMI full report\n");
488 skb = dev_alloc_skb(len);
490 netdev_warn(dev,
"Memory squeeze on fr_lmi_send()\n");
502 data = skb_tail_pointer(skb);
513 data[i++] =
state(hdlc)->txseq =
514 fr_lmi_nextseq(
state(hdlc)->txseq);
515 data[i++] =
state(hdlc)->rxseq;
517 if (dce && fullrep) {
521 data[i++] = stat_len;
524 if (
state(hdlc)->reliable && !pvc->
state.exist) {
526 fr_log_dlci_active(pvc);
533 pvc->
state.active = 1;
534 fr_log_dlci_active(pvc);
538 data[
i] = pvc->
dlci >> 8;
539 data[i + 1] = pvc->
dlci & 0xFF;
541 data[
i] = (pvc->
dlci >> 4) & 0x3F;
542 data[i + 1] = ((pvc->
dlci << 3) & 0x78) | 0x80;
548 else if (pvc->
state.active)
559 skb_reset_network_header(skb);
566 static void fr_set_link_state(
int reliable,
struct net_device *dev)
571 state(hdlc)->reliable = reliable;
573 netif_dormant_off(dev);
574 state(hdlc)->n391cnt = 0;
575 state(hdlc)->dce_changed = 1;
586 netif_dormant_on(dev);
591 if (!
state(hdlc)->settings.dce)
592 pvc->
state.bandwidth = 0;
599 static void fr_timer(
unsigned long arg)
603 int i,
cnt = 0, reliable;
606 if (
state(hdlc)->settings.dce) {
607 reliable =
state(hdlc)->request &&
609 state(hdlc)->settings.t392 *
HZ);
610 state(hdlc)->request = 0;
612 state(hdlc)->last_errors <<= 1;
614 if (
state(hdlc)->reliable)
615 netdev_info(dev,
"No LMI status reply received\n");
616 state(hdlc)->last_errors |= 1;
619 list =
state(hdlc)->last_errors;
620 for (i = 0; i <
state(hdlc)->settings.n393; i++, list >>= 1)
623 reliable = (cnt <
state(hdlc)->settings.n392);
626 if (
state(hdlc)->reliable != reliable) {
627 netdev_info(dev,
"Link %sreliable\n", reliable ?
"" :
"un");
628 fr_set_link_state(reliable, dev);
631 if (
state(hdlc)->settings.dce)
633 state(hdlc)->settings.t392 *
HZ;
635 if (
state(hdlc)->n391cnt)
636 state(hdlc)->n391cnt--;
638 fr_lmi_send(dev,
state(hdlc)->n391cnt == 0);
641 state(hdlc)->request = 1;
643 state(hdlc)->settings.t391 *
HZ;
646 state(hdlc)->timer.function = fr_timer;
657 int lmi =
state(hdlc)->settings.lmi;
658 int dce =
state(hdlc)->settings.dce;
659 int stat_len = (lmi ==
LMI_CISCO) ? 6 : 3, reptype,
error, no_ram, i;
663 netdev_info(dev,
"Short LMI frame\n");
669 netdev_info(dev,
"Received non-LMI frame with LMI DLCI\n");
674 netdev_info(dev,
"Invalid LMI Call reference (0x%02X)\n",
680 netdev_info(dev,
"Invalid LMI Message type (0x%02X)\n",
687 netdev_info(dev,
"Not ANSI locking shift in LMI message (0x%02X)\n",
697 netdev_info(dev,
"Not an LMI Report type IE (0x%02X)\n",
703 netdev_info(dev,
"Invalid LMI Report type IE length (%u)\n",
708 reptype = skb->
data[++
i];
710 netdev_info(dev,
"Unsupported LMI Report type (0x%02X)\n",
717 netdev_info(dev,
"Not an LMI Link integrity verification IE (0x%02X)\n",
723 netdev_info(dev,
"Invalid LMI Link integrity verification IE length (%u)\n",
730 rxseq = skb->
data[i++];
732 txseq =
state(hdlc)->txseq;
738 if (!
state(hdlc)->reliable)
741 if (rxseq == 0 || rxseq != txseq) {
742 state(hdlc)->n391cnt = 0;
749 state(hdlc)->fullrep_sent = 0;
750 pvc =
state(hdlc)->first_pvc;
752 if (pvc->
state.new) {
756 state(hdlc)->dce_changed = 1;
762 if (
state(hdlc)->dce_changed) {
764 state(hdlc)->fullrep_sent = 1;
765 state(hdlc)->dce_changed = 0;
768 state(hdlc)->request = 1;
775 state(hdlc)->request = 0;
783 pvc =
state(hdlc)->first_pvc;
786 pvc->
state.deleted = 1;
791 while (skb->
len >= i + 2 + stat_len) {
798 netdev_info(dev,
"Not an LMI PVC status IE (0x%02X)\n",
803 if (skb->
data[++i] != stat_len) {
804 netdev_info(dev,
"Invalid LMI PVC status IE length (%u)\n",
810 new = !! (skb->
data[i + 2] & 0x08);
811 active = !! (skb->
data[i + 2] & 0x02);
813 dlci = (skb->
data[
i] << 8) | skb->
data[i + 1];
814 bw = (skb->
data[i + 3] << 16) |
815 (skb->
data[i + 4] << 8) |
818 dlci = ((skb->
data[
i] & 0x3F) << 4) |
819 ((skb->
data[i + 1] & 0x78) >> 3);
823 pvc = add_pvc(dev, dlci);
825 if (!pvc && !no_ram) {
826 netdev_warn(dev,
"Memory squeeze on fr_lmi_recv()\n");
831 pvc->
state.exist = 1;
832 pvc->
state.deleted = 0;
833 if (active != pvc->
state.active ||
834 new != pvc->
state.new ||
835 bw != pvc->
state.bandwidth ||
837 pvc->
state.new =
new;
839 pvc->
state.bandwidth = bw;
840 pvc_carrier(active, pvc);
841 fr_log_dlci_active(pvc);
848 pvc =
state(hdlc)->first_pvc;
854 pvc->
state.exist = 0;
855 pvc->
state.bandwidth = 0;
856 fr_log_dlci_active(pvc);
862 state(hdlc)->n391cnt =
state(hdlc)->settings.n391;
868 static int fr_rx(
struct sk_buff *skb)
872 fr_hdr *fh = (fr_hdr*)skb->
data;
878 if (skb->
len <= 4 || fh->ea1 || data[2] !=
FR_UI)
881 dlci = q922_to_dlci(skb->
data);
888 if (fr_lmi_recv(frad, skb))
894 pvc = find_pvc(hdlc, dlci);
897 netdev_info(frad,
"No PVC for received frame's DLCI %d\n",
904 if (pvc->
state.fecn != fh->fecn) {
907 dlci, fh->fecn ?
"N" :
"FF");
909 pvc->
state.fecn ^= 1;
912 if (pvc->
state.becn != fh->becn) {
915 dlci, fh->becn ?
"N" :
"FF");
917 pvc->
state.becn ^= 1;
922 frad->
stats.rx_dropped++;
936 }
else if (skb->
len > 10 && data[3] ==
FR_PAD &&
942 switch ((((
u32)oui) << 16) | pid) {
957 netdev_info(frad,
"Unsupported protocol, OUI=%x PID=%x\n",
963 netdev_info(frad,
"Unsupported protocol, NLPID=%x length=%i\n",
970 dev->
stats.rx_packets++;
973 dev->
stats.rx_compressed++;
983 frad->
stats.rx_errors++;
997 state(hdlc)->reliable = 0;
998 state(hdlc)->dce_changed = 1;
999 state(hdlc)->request = 0;
1000 state(hdlc)->fullrep_sent = 0;
1001 state(hdlc)->last_errors = 0xFFFFFFFF;
1002 state(hdlc)->n391cnt = 0;
1008 state(hdlc)->timer.function = fr_timer;
1009 state(hdlc)->timer.data = (
unsigned long)dev;
1012 fr_set_link_state(1, dev);
1024 fr_set_link_state(0, dev);
1043 static void pvc_setup(
struct net_device *dev)
1053 .ndo_open = pvc_open,
1054 .ndo_stop = pvc_close,
1056 .ndo_start_xmit = pvc_xmit,
1057 .ndo_do_ioctl = pvc_ioctl,
1060 static int fr_add_pvc(
struct net_device *frad,
unsigned int dlci,
int type)
1067 if ((pvc = add_pvc(frad, dlci)) ==
NULL) {
1068 netdev_warn(frad,
"Memory squeeze on fr_add_pvc()\n");
1072 if (*get_dev_p(pvc, type))
1075 used = pvc_is_used(pvc);
1084 netdev_warn(frad,
"Memory squeeze on fr_pvc()\n");
1085 delete_unused_pvcs(hdlc);
1090 eth_hw_addr_random(dev);
1102 delete_unused_pvcs(hdlc);
1107 *get_dev_p(pvc, type) =
dev;
1109 state(hdlc)->dce_changed = 1;
1110 state(hdlc)->dce_pvc_count++;
1117 static int fr_del_pvc(
hdlc_device *hdlc,
unsigned int dlci,
int type)
1122 if ((pvc = find_pvc(hdlc, dlci)) ==
NULL)
1125 if ((dev = *get_dev_p(pvc, type)) ==
NULL)
1131 unregister_netdevice(dev);
1132 *get_dev_p(pvc, type) =
NULL;
1134 if (!pvc_is_used(pvc)) {
1135 state(hdlc)->dce_pvc_count--;
1136 state(hdlc)->dce_changed = 1;
1138 delete_unused_pvcs(hdlc);
1144 static void fr_destroy(
struct net_device *frad)
1149 state(hdlc)->dce_pvc_count = 0;
1150 state(hdlc)->dce_changed = 1;
1156 unregister_netdevice(pvc->
main);
1159 unregister_netdevice(pvc->
ether);
1171 .detach = fr_destroy,
1187 switch (ifr->ifr_settings.type) {
1189 if (dev_to_hdlc(dev)->proto != &proto)
1192 if (ifr->ifr_settings.size < size) {
1193 ifr->ifr_settings.size =
size;
1217 new_settings.
t391 < 1 ||
1218 new_settings.
t392 < 2 ||
1219 new_settings.
n391 < 1 ||
1220 new_settings.
n392 < 1 ||
1221 new_settings.
n393 < new_settings.
n392 ||
1222 new_settings.
n393 > 32 ||
1223 (new_settings.
dce != 0 &&
1224 new_settings.
dce != 1))
1231 if (dev_to_hdlc(dev)->proto != &proto) {
1237 state(hdlc)->dce_pvc_count = 0;
1239 memcpy(&
state(hdlc)->settings, &new_settings, size);
1247 if (dev_to_hdlc(dev)->proto != &proto)
1257 if (pvc.
dlci <= 0 || pvc.
dlci >= 1024)
1268 return fr_add_pvc(dev, pvc.
dlci, result);
1270 return fr_del_pvc(hdlc, pvc.
dlci, result);
1277 static int __init mod_init(
void)
1284 static void __exit mod_exit(
void)