0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #include <linux/errno.h>
0034 #include <linux/etherdevice.h>
0035 #include <linux/hdlc.h>
0036 #include <linux/if_arp.h>
0037 #include <linux/inetdevice.h>
0038 #include <linux/init.h>
0039 #include <linux/kernel.h>
0040 #include <linux/module.h>
0041 #include <linux/pkt_sched.h>
0042 #include <linux/poll.h>
0043 #include <linux/rtnetlink.h>
0044 #include <linux/skbuff.h>
0045 #include <linux/slab.h>
0046
0047 #undef DEBUG_PKT
0048 #undef DEBUG_ECN
0049 #undef DEBUG_LINK
0050 #undef DEBUG_PROTO
0051 #undef DEBUG_PVC
0052
0053 #define FR_UI 0x03
0054 #define FR_PAD 0x00
0055
0056 #define NLPID_IP 0xCC
0057 #define NLPID_IPV6 0x8E
0058 #define NLPID_SNAP 0x80
0059 #define NLPID_PAD 0x00
0060 #define NLPID_CCITT_ANSI_LMI 0x08
0061 #define NLPID_CISCO_LMI 0x09
0062
0063 #define LMI_CCITT_ANSI_DLCI 0
0064 #define LMI_CISCO_DLCI 1023
0065
0066 #define LMI_CALLREF 0x00
0067 #define LMI_ANSI_LOCKSHIFT 0x95
0068 #define LMI_ANSI_CISCO_REPTYPE 0x01
0069 #define LMI_CCITT_REPTYPE 0x51
0070 #define LMI_ANSI_CISCO_ALIVE 0x03
0071 #define LMI_CCITT_ALIVE 0x53
0072 #define LMI_ANSI_CISCO_PVCSTAT 0x07
0073 #define LMI_CCITT_PVCSTAT 0x57
0074
0075 #define LMI_FULLREP 0x00
0076 #define LMI_INTEGRITY 0x01
0077 #define LMI_SINGLE 0x02
0078
0079 #define LMI_STATUS_ENQUIRY 0x75
0080 #define LMI_STATUS 0x7D
0081
0082 #define LMI_REPT_LEN 1
0083 #define LMI_INTEG_LEN 2
0084
0085 #define LMI_CCITT_CISCO_LENGTH 13
0086 #define LMI_ANSI_LENGTH 14
0087
0088 struct fr_hdr {
0089 #if defined(__LITTLE_ENDIAN_BITFIELD)
0090 unsigned ea1: 1;
0091 unsigned cr: 1;
0092 unsigned dlcih: 6;
0093
0094 unsigned ea2: 1;
0095 unsigned de: 1;
0096 unsigned becn: 1;
0097 unsigned fecn: 1;
0098 unsigned dlcil: 4;
0099 #else
0100 unsigned dlcih: 6;
0101 unsigned cr: 1;
0102 unsigned ea1: 1;
0103
0104 unsigned dlcil: 4;
0105 unsigned fecn: 1;
0106 unsigned becn: 1;
0107 unsigned de: 1;
0108 unsigned ea2: 1;
0109 #endif
0110 } __packed;
0111
0112 struct pvc_device {
0113 struct net_device *frad;
0114 struct net_device *main;
0115 struct net_device *ether;
0116 struct pvc_device *next;
0117 int dlci;
0118 int open_count;
0119
0120 struct {
0121 unsigned int new: 1;
0122 unsigned int active: 1;
0123 unsigned int exist: 1;
0124 unsigned int deleted: 1;
0125 unsigned int fecn: 1;
0126 unsigned int becn: 1;
0127 unsigned int bandwidth;
0128 } state;
0129 };
0130
0131 struct frad_state {
0132 fr_proto settings;
0133 struct pvc_device *first_pvc;
0134 int dce_pvc_count;
0135
0136 struct timer_list timer;
0137 struct net_device *dev;
0138 unsigned long last_poll;
0139 int reliable;
0140 int dce_changed;
0141 int request;
0142 int fullrep_sent;
0143 u32 last_errors;
0144 u8 n391cnt;
0145 u8 txseq;
0146 u8 rxseq;
0147 };
0148
0149 static int fr_ioctl(struct net_device *dev, struct if_settings *ifs);
0150
0151 static inline u16 q922_to_dlci(u8 *hdr)
0152 {
0153 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
0154 }
0155
0156 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
0157 {
0158 hdr[0] = (dlci >> 2) & 0xFC;
0159 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
0160 }
0161
0162 static inline struct frad_state *state(hdlc_device *hdlc)
0163 {
0164 return (struct frad_state *)(hdlc->state);
0165 }
0166
0167 static inline struct pvc_device *find_pvc(hdlc_device *hdlc, u16 dlci)
0168 {
0169 struct pvc_device *pvc = state(hdlc)->first_pvc;
0170
0171 while (pvc) {
0172 if (pvc->dlci == dlci)
0173 return pvc;
0174 if (pvc->dlci > dlci)
0175 return NULL;
0176 pvc = pvc->next;
0177 }
0178
0179 return NULL;
0180 }
0181
0182 static struct pvc_device *add_pvc(struct net_device *dev, u16 dlci)
0183 {
0184 hdlc_device *hdlc = dev_to_hdlc(dev);
0185 struct pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
0186
0187 while (*pvc_p) {
0188 if ((*pvc_p)->dlci == dlci)
0189 return *pvc_p;
0190 if ((*pvc_p)->dlci > dlci)
0191 break;
0192 pvc_p = &(*pvc_p)->next;
0193 }
0194
0195 pvc = kzalloc(sizeof(*pvc), GFP_ATOMIC);
0196 #ifdef DEBUG_PVC
0197 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
0198 #endif
0199 if (!pvc)
0200 return NULL;
0201
0202 pvc->dlci = dlci;
0203 pvc->frad = dev;
0204 pvc->next = *pvc_p;
0205 *pvc_p = pvc;
0206 return pvc;
0207 }
0208
0209 static inline int pvc_is_used(struct pvc_device *pvc)
0210 {
0211 return pvc->main || pvc->ether;
0212 }
0213
0214 static inline void pvc_carrier(int on, struct pvc_device *pvc)
0215 {
0216 if (on) {
0217 if (pvc->main)
0218 if (!netif_carrier_ok(pvc->main))
0219 netif_carrier_on(pvc->main);
0220 if (pvc->ether)
0221 if (!netif_carrier_ok(pvc->ether))
0222 netif_carrier_on(pvc->ether);
0223 } else {
0224 if (pvc->main)
0225 if (netif_carrier_ok(pvc->main))
0226 netif_carrier_off(pvc->main);
0227 if (pvc->ether)
0228 if (netif_carrier_ok(pvc->ether))
0229 netif_carrier_off(pvc->ether);
0230 }
0231 }
0232
0233 static inline void delete_unused_pvcs(hdlc_device *hdlc)
0234 {
0235 struct pvc_device **pvc_p = &state(hdlc)->first_pvc;
0236
0237 while (*pvc_p) {
0238 if (!pvc_is_used(*pvc_p)) {
0239 struct pvc_device *pvc = *pvc_p;
0240 #ifdef DEBUG_PVC
0241 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
0242 #endif
0243 *pvc_p = pvc->next;
0244 kfree(pvc);
0245 continue;
0246 }
0247 pvc_p = &(*pvc_p)->next;
0248 }
0249 }
0250
0251 static inline struct net_device **get_dev_p(struct pvc_device *pvc,
0252 int type)
0253 {
0254 if (type == ARPHRD_ETHER)
0255 return &pvc->ether;
0256 else
0257 return &pvc->main;
0258 }
0259
0260 static int fr_hard_header(struct sk_buff *skb, u16 dlci)
0261 {
0262 if (!skb->dev) {
0263 switch (dlci) {
0264 case LMI_CCITT_ANSI_DLCI:
0265 skb_push(skb, 4);
0266 skb->data[3] = NLPID_CCITT_ANSI_LMI;
0267 break;
0268
0269 case LMI_CISCO_DLCI:
0270 skb_push(skb, 4);
0271 skb->data[3] = NLPID_CISCO_LMI;
0272 break;
0273
0274 default:
0275 return -EINVAL;
0276 }
0277
0278 } else if (skb->dev->type == ARPHRD_DLCI) {
0279 switch (skb->protocol) {
0280 case htons(ETH_P_IP):
0281 skb_push(skb, 4);
0282 skb->data[3] = NLPID_IP;
0283 break;
0284
0285 case htons(ETH_P_IPV6):
0286 skb_push(skb, 4);
0287 skb->data[3] = NLPID_IPV6;
0288 break;
0289
0290 default:
0291 skb_push(skb, 10);
0292 skb->data[3] = FR_PAD;
0293 skb->data[4] = NLPID_SNAP;
0294
0295 skb->data[5] = 0x00;
0296 skb->data[6] = 0x00;
0297 skb->data[7] = 0x00;
0298
0299 *(__be16 *)(skb->data + 8) = skb->protocol;
0300 }
0301
0302 } else if (skb->dev->type == ARPHRD_ETHER) {
0303 skb_push(skb, 10);
0304 skb->data[3] = FR_PAD;
0305 skb->data[4] = NLPID_SNAP;
0306
0307 skb->data[5] = 0x00;
0308 skb->data[6] = 0x80;
0309 skb->data[7] = 0xC2;
0310
0311 skb->data[8] = 0x00;
0312 skb->data[9] = 0x07;
0313
0314 } else {
0315 return -EINVAL;
0316 }
0317
0318 dlci_to_q922(skb->data, dlci);
0319 skb->data[2] = FR_UI;
0320 return 0;
0321 }
0322
0323 static int pvc_open(struct net_device *dev)
0324 {
0325 struct pvc_device *pvc = dev->ml_priv;
0326
0327 if ((pvc->frad->flags & IFF_UP) == 0)
0328 return -EIO;
0329
0330 if (pvc->open_count++ == 0) {
0331 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
0332
0333 if (state(hdlc)->settings.lmi == LMI_NONE)
0334 pvc->state.active = netif_carrier_ok(pvc->frad);
0335
0336 pvc_carrier(pvc->state.active, pvc);
0337 state(hdlc)->dce_changed = 1;
0338 }
0339 return 0;
0340 }
0341
0342 static int pvc_close(struct net_device *dev)
0343 {
0344 struct pvc_device *pvc = dev->ml_priv;
0345
0346 if (--pvc->open_count == 0) {
0347 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
0348
0349 if (state(hdlc)->settings.lmi == LMI_NONE)
0350 pvc->state.active = 0;
0351
0352 if (state(hdlc)->settings.dce) {
0353 state(hdlc)->dce_changed = 1;
0354 pvc->state.active = 0;
0355 }
0356 }
0357 return 0;
0358 }
0359
0360 static int pvc_ioctl(struct net_device *dev, struct if_settings *ifs)
0361 {
0362 struct pvc_device *pvc = dev->ml_priv;
0363 fr_proto_pvc_info info;
0364
0365 if (ifs->type == IF_GET_PROTO) {
0366 if (dev->type == ARPHRD_ETHER)
0367 ifs->type = IF_PROTO_FR_ETH_PVC;
0368 else
0369 ifs->type = IF_PROTO_FR_PVC;
0370
0371 if (ifs->size < sizeof(info)) {
0372
0373 ifs->size = sizeof(info);
0374 return -ENOBUFS;
0375 }
0376
0377 info.dlci = pvc->dlci;
0378 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
0379 if (copy_to_user(ifs->ifs_ifsu.fr_pvc_info,
0380 &info, sizeof(info)))
0381 return -EFAULT;
0382 return 0;
0383 }
0384
0385 return -EINVAL;
0386 }
0387
0388 static netdev_tx_t pvc_xmit(struct sk_buff *skb, struct net_device *dev)
0389 {
0390 struct pvc_device *pvc = dev->ml_priv;
0391
0392 if (!pvc->state.active)
0393 goto drop;
0394
0395 if (dev->type == ARPHRD_ETHER) {
0396 int pad = ETH_ZLEN - skb->len;
0397
0398 if (pad > 0) {
0399 if (__skb_pad(skb, pad, false))
0400 goto drop;
0401 skb_put(skb, pad);
0402 }
0403 }
0404
0405
0406
0407
0408
0409 if (skb_headroom(skb) < 10) {
0410 struct sk_buff *skb2 = skb_realloc_headroom(skb, 10);
0411
0412 if (!skb2)
0413 goto drop;
0414 dev_kfree_skb(skb);
0415 skb = skb2;
0416 }
0417
0418 skb->dev = dev;
0419 if (fr_hard_header(skb, pvc->dlci))
0420 goto drop;
0421
0422 dev->stats.tx_bytes += skb->len;
0423 dev->stats.tx_packets++;
0424 if (pvc->state.fecn)
0425 dev->stats.tx_compressed++;
0426 skb->dev = pvc->frad;
0427 skb->protocol = htons(ETH_P_HDLC);
0428 skb_reset_network_header(skb);
0429 dev_queue_xmit(skb);
0430 return NETDEV_TX_OK;
0431
0432 drop:
0433 dev->stats.tx_dropped++;
0434 kfree_skb(skb);
0435 return NETDEV_TX_OK;
0436 }
0437
0438 static inline void fr_log_dlci_active(struct pvc_device *pvc)
0439 {
0440 netdev_info(pvc->frad, "DLCI %d [%s%s%s]%s %s\n",
0441 pvc->dlci,
0442 pvc->main ? pvc->main->name : "",
0443 pvc->main && pvc->ether ? " " : "",
0444 pvc->ether ? pvc->ether->name : "",
0445 pvc->state.new ? " new" : "",
0446 !pvc->state.exist ? "deleted" :
0447 pvc->state.active ? "active" : "inactive");
0448 }
0449
0450 static inline u8 fr_lmi_nextseq(u8 x)
0451 {
0452 x++;
0453 return x ? x : 1;
0454 }
0455
0456 static void fr_lmi_send(struct net_device *dev, int fullrep)
0457 {
0458 hdlc_device *hdlc = dev_to_hdlc(dev);
0459 struct sk_buff *skb;
0460 struct pvc_device *pvc = state(hdlc)->first_pvc;
0461 int lmi = state(hdlc)->settings.lmi;
0462 int dce = state(hdlc)->settings.dce;
0463 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
0464 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
0465 u8 *data;
0466 int i = 0;
0467
0468 if (dce && fullrep) {
0469 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
0470 if (len > HDLC_MAX_MRU) {
0471 netdev_warn(dev, "Too many PVCs while sending LMI full report\n");
0472 return;
0473 }
0474 }
0475
0476 skb = dev_alloc_skb(len);
0477 if (!skb)
0478 return;
0479
0480 memset(skb->data, 0, len);
0481 skb_reserve(skb, 4);
0482 if (lmi == LMI_CISCO)
0483 fr_hard_header(skb, LMI_CISCO_DLCI);
0484 else
0485 fr_hard_header(skb, LMI_CCITT_ANSI_DLCI);
0486
0487 data = skb_tail_pointer(skb);
0488 data[i++] = LMI_CALLREF;
0489 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
0490 if (lmi == LMI_ANSI)
0491 data[i++] = LMI_ANSI_LOCKSHIFT;
0492 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
0493 LMI_ANSI_CISCO_REPTYPE;
0494 data[i++] = LMI_REPT_LEN;
0495 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
0496 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
0497 data[i++] = LMI_INTEG_LEN;
0498 data[i++] = state(hdlc)->txseq =
0499 fr_lmi_nextseq(state(hdlc)->txseq);
0500 data[i++] = state(hdlc)->rxseq;
0501
0502 if (dce && fullrep) {
0503 while (pvc) {
0504 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
0505 LMI_ANSI_CISCO_PVCSTAT;
0506 data[i++] = stat_len;
0507
0508
0509 if (state(hdlc)->reliable && !pvc->state.exist) {
0510 pvc->state.exist = pvc->state.new = 1;
0511 fr_log_dlci_active(pvc);
0512 }
0513
0514
0515 if (pvc->open_count && !pvc->state.active &&
0516 pvc->state.exist && !pvc->state.new) {
0517 pvc_carrier(1, pvc);
0518 pvc->state.active = 1;
0519 fr_log_dlci_active(pvc);
0520 }
0521
0522 if (lmi == LMI_CISCO) {
0523 data[i] = pvc->dlci >> 8;
0524 data[i + 1] = pvc->dlci & 0xFF;
0525 } else {
0526 data[i] = (pvc->dlci >> 4) & 0x3F;
0527 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
0528 data[i + 2] = 0x80;
0529 }
0530
0531 if (pvc->state.new)
0532 data[i + 2] |= 0x08;
0533 else if (pvc->state.active)
0534 data[i + 2] |= 0x02;
0535
0536 i += stat_len;
0537 pvc = pvc->next;
0538 }
0539 }
0540
0541 skb_put(skb, i);
0542 skb->priority = TC_PRIO_CONTROL;
0543 skb->dev = dev;
0544 skb->protocol = htons(ETH_P_HDLC);
0545 skb_reset_network_header(skb);
0546
0547 dev_queue_xmit(skb);
0548 }
0549
0550 static void fr_set_link_state(int reliable, struct net_device *dev)
0551 {
0552 hdlc_device *hdlc = dev_to_hdlc(dev);
0553 struct pvc_device *pvc = state(hdlc)->first_pvc;
0554
0555 state(hdlc)->reliable = reliable;
0556 if (reliable) {
0557 netif_dormant_off(dev);
0558 state(hdlc)->n391cnt = 0;
0559 state(hdlc)->dce_changed = 1;
0560
0561 if (state(hdlc)->settings.lmi == LMI_NONE) {
0562 while (pvc) {
0563 pvc_carrier(1, pvc);
0564 pvc->state.exist = pvc->state.active = 1;
0565 pvc->state.new = 0;
0566 pvc = pvc->next;
0567 }
0568 }
0569 } else {
0570 netif_dormant_on(dev);
0571 while (pvc) {
0572 pvc_carrier(0, pvc);
0573 pvc->state.exist = pvc->state.active = 0;
0574 pvc->state.new = 0;
0575 if (!state(hdlc)->settings.dce)
0576 pvc->state.bandwidth = 0;
0577 pvc = pvc->next;
0578 }
0579 }
0580 }
0581
0582 static void fr_timer(struct timer_list *t)
0583 {
0584 struct frad_state *st = from_timer(st, t, timer);
0585 struct net_device *dev = st->dev;
0586 hdlc_device *hdlc = dev_to_hdlc(dev);
0587 int i, cnt = 0, reliable;
0588 u32 list;
0589
0590 if (state(hdlc)->settings.dce) {
0591 reliable = state(hdlc)->request &&
0592 time_before(jiffies, state(hdlc)->last_poll +
0593 state(hdlc)->settings.t392 * HZ);
0594 state(hdlc)->request = 0;
0595 } else {
0596 state(hdlc)->last_errors <<= 1;
0597 if (state(hdlc)->request) {
0598 if (state(hdlc)->reliable)
0599 netdev_info(dev, "No LMI status reply received\n");
0600 state(hdlc)->last_errors |= 1;
0601 }
0602
0603 list = state(hdlc)->last_errors;
0604 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
0605 cnt += (list & 1);
0606
0607 reliable = (cnt < state(hdlc)->settings.n392);
0608 }
0609
0610 if (state(hdlc)->reliable != reliable) {
0611 netdev_info(dev, "Link %sreliable\n", reliable ? "" : "un");
0612 fr_set_link_state(reliable, dev);
0613 }
0614
0615 if (state(hdlc)->settings.dce) {
0616 state(hdlc)->timer.expires = jiffies +
0617 state(hdlc)->settings.t392 * HZ;
0618 } else {
0619 if (state(hdlc)->n391cnt)
0620 state(hdlc)->n391cnt--;
0621
0622 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
0623
0624 state(hdlc)->last_poll = jiffies;
0625 state(hdlc)->request = 1;
0626 state(hdlc)->timer.expires = jiffies +
0627 state(hdlc)->settings.t391 * HZ;
0628 }
0629
0630 add_timer(&state(hdlc)->timer);
0631 }
0632
0633 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
0634 {
0635 hdlc_device *hdlc = dev_to_hdlc(dev);
0636 struct pvc_device *pvc;
0637 u8 rxseq, txseq;
0638 int lmi = state(hdlc)->settings.lmi;
0639 int dce = state(hdlc)->settings.dce;
0640 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
0641
0642 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
0643 LMI_CCITT_CISCO_LENGTH)) {
0644 netdev_info(dev, "Short LMI frame\n");
0645 return 1;
0646 }
0647
0648 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
0649 NLPID_CCITT_ANSI_LMI)) {
0650 netdev_info(dev, "Received non-LMI frame with LMI DLCI\n");
0651 return 1;
0652 }
0653
0654 if (skb->data[4] != LMI_CALLREF) {
0655 netdev_info(dev, "Invalid LMI Call reference (0x%02X)\n",
0656 skb->data[4]);
0657 return 1;
0658 }
0659
0660 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
0661 netdev_info(dev, "Invalid LMI Message type (0x%02X)\n",
0662 skb->data[5]);
0663 return 1;
0664 }
0665
0666 if (lmi == LMI_ANSI) {
0667 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
0668 netdev_info(dev, "Not ANSI locking shift in LMI message (0x%02X)\n",
0669 skb->data[6]);
0670 return 1;
0671 }
0672 i = 7;
0673 } else {
0674 i = 6;
0675 }
0676
0677 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
0678 LMI_ANSI_CISCO_REPTYPE)) {
0679 netdev_info(dev, "Not an LMI Report type IE (0x%02X)\n",
0680 skb->data[i]);
0681 return 1;
0682 }
0683
0684 if (skb->data[++i] != LMI_REPT_LEN) {
0685 netdev_info(dev, "Invalid LMI Report type IE length (%u)\n",
0686 skb->data[i]);
0687 return 1;
0688 }
0689
0690 reptype = skb->data[++i];
0691 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
0692 netdev_info(dev, "Unsupported LMI Report type (0x%02X)\n",
0693 reptype);
0694 return 1;
0695 }
0696
0697 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
0698 LMI_ANSI_CISCO_ALIVE)) {
0699 netdev_info(dev, "Not an LMI Link integrity verification IE (0x%02X)\n",
0700 skb->data[i]);
0701 return 1;
0702 }
0703
0704 if (skb->data[++i] != LMI_INTEG_LEN) {
0705 netdev_info(dev, "Invalid LMI Link integrity verification IE length (%u)\n",
0706 skb->data[i]);
0707 return 1;
0708 }
0709 i++;
0710
0711 state(hdlc)->rxseq = skb->data[i++];
0712 rxseq = skb->data[i++];
0713
0714 txseq = state(hdlc)->txseq;
0715
0716 if (dce)
0717 state(hdlc)->last_poll = jiffies;
0718
0719 error = 0;
0720 if (!state(hdlc)->reliable)
0721 error = 1;
0722
0723 if (rxseq == 0 || rxseq != txseq) {
0724 state(hdlc)->n391cnt = 0;
0725 error = 1;
0726 }
0727
0728 if (dce) {
0729 if (state(hdlc)->fullrep_sent && !error) {
0730
0731 state(hdlc)->fullrep_sent = 0;
0732 pvc = state(hdlc)->first_pvc;
0733 while (pvc) {
0734 if (pvc->state.new) {
0735 pvc->state.new = 0;
0736
0737
0738 state(hdlc)->dce_changed = 1;
0739 }
0740 pvc = pvc->next;
0741 }
0742 }
0743
0744 if (state(hdlc)->dce_changed) {
0745 reptype = LMI_FULLREP;
0746 state(hdlc)->fullrep_sent = 1;
0747 state(hdlc)->dce_changed = 0;
0748 }
0749
0750 state(hdlc)->request = 1;
0751 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
0752 return 0;
0753 }
0754
0755
0756
0757 state(hdlc)->request = 0;
0758
0759 if (error)
0760 return 0;
0761
0762 if (reptype != LMI_FULLREP)
0763 return 0;
0764
0765 pvc = state(hdlc)->first_pvc;
0766
0767 while (pvc) {
0768 pvc->state.deleted = 1;
0769 pvc = pvc->next;
0770 }
0771
0772 no_ram = 0;
0773 while (skb->len >= i + 2 + stat_len) {
0774 u16 dlci;
0775 u32 bw;
0776 unsigned int active, new;
0777
0778 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
0779 LMI_ANSI_CISCO_PVCSTAT)) {
0780 netdev_info(dev, "Not an LMI PVC status IE (0x%02X)\n",
0781 skb->data[i]);
0782 return 1;
0783 }
0784
0785 if (skb->data[++i] != stat_len) {
0786 netdev_info(dev, "Invalid LMI PVC status IE length (%u)\n",
0787 skb->data[i]);
0788 return 1;
0789 }
0790 i++;
0791
0792 new = !!(skb->data[i + 2] & 0x08);
0793 active = !!(skb->data[i + 2] & 0x02);
0794 if (lmi == LMI_CISCO) {
0795 dlci = (skb->data[i] << 8) | skb->data[i + 1];
0796 bw = (skb->data[i + 3] << 16) |
0797 (skb->data[i + 4] << 8) |
0798 (skb->data[i + 5]);
0799 } else {
0800 dlci = ((skb->data[i] & 0x3F) << 4) |
0801 ((skb->data[i + 1] & 0x78) >> 3);
0802 bw = 0;
0803 }
0804
0805 pvc = add_pvc(dev, dlci);
0806
0807 if (!pvc && !no_ram) {
0808 netdev_warn(dev, "Memory squeeze on fr_lmi_recv()\n");
0809 no_ram = 1;
0810 }
0811
0812 if (pvc) {
0813 pvc->state.exist = 1;
0814 pvc->state.deleted = 0;
0815 if (active != pvc->state.active ||
0816 new != pvc->state.new ||
0817 bw != pvc->state.bandwidth ||
0818 !pvc->state.exist) {
0819 pvc->state.new = new;
0820 pvc->state.active = active;
0821 pvc->state.bandwidth = bw;
0822 pvc_carrier(active, pvc);
0823 fr_log_dlci_active(pvc);
0824 }
0825 }
0826
0827 i += stat_len;
0828 }
0829
0830 pvc = state(hdlc)->first_pvc;
0831
0832 while (pvc) {
0833 if (pvc->state.deleted && pvc->state.exist) {
0834 pvc_carrier(0, pvc);
0835 pvc->state.active = pvc->state.new = 0;
0836 pvc->state.exist = 0;
0837 pvc->state.bandwidth = 0;
0838 fr_log_dlci_active(pvc);
0839 }
0840 pvc = pvc->next;
0841 }
0842
0843
0844 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
0845
0846 return 0;
0847 }
0848
0849 static int fr_snap_parse(struct sk_buff *skb, struct pvc_device *pvc)
0850 {
0851
0852 if (skb->data[0] == 0x00 &&
0853 skb->data[1] == 0x00 &&
0854 skb->data[2] == 0x00) {
0855 if (!pvc->main)
0856 return -1;
0857 skb->dev = pvc->main;
0858 skb->protocol = *(__be16 *)(skb->data + 3);
0859 skb_pull(skb, 5);
0860 skb_reset_mac_header(skb);
0861 return 0;
0862
0863
0864 } else if (skb->data[0] == 0x00 &&
0865 skb->data[1] == 0x80 &&
0866 skb->data[2] == 0xC2) {
0867
0868 if (skb->data[3] == 0x00 &&
0869 skb->data[4] == 0x07) {
0870 if (!pvc->ether)
0871 return -1;
0872 skb_pull(skb, 5);
0873 if (skb->len < ETH_HLEN)
0874 return -1;
0875 skb->protocol = eth_type_trans(skb, pvc->ether);
0876 return 0;
0877
0878
0879 } else {
0880 return -1;
0881 }
0882
0883
0884 } else {
0885 return -1;
0886 }
0887 }
0888
0889 static int fr_rx(struct sk_buff *skb)
0890 {
0891 struct net_device *frad = skb->dev;
0892 hdlc_device *hdlc = dev_to_hdlc(frad);
0893 struct fr_hdr *fh = (struct fr_hdr *)skb->data;
0894 u8 *data = skb->data;
0895 u16 dlci;
0896 struct pvc_device *pvc;
0897 struct net_device *dev;
0898
0899 if (skb->len < 4 || fh->ea1 || !fh->ea2 || data[2] != FR_UI)
0900 goto rx_error;
0901
0902 dlci = q922_to_dlci(skb->data);
0903
0904 if ((dlci == LMI_CCITT_ANSI_DLCI &&
0905 (state(hdlc)->settings.lmi == LMI_ANSI ||
0906 state(hdlc)->settings.lmi == LMI_CCITT)) ||
0907 (dlci == LMI_CISCO_DLCI &&
0908 state(hdlc)->settings.lmi == LMI_CISCO)) {
0909 if (fr_lmi_recv(frad, skb))
0910 goto rx_error;
0911 dev_kfree_skb_any(skb);
0912 return NET_RX_SUCCESS;
0913 }
0914
0915 pvc = find_pvc(hdlc, dlci);
0916 if (!pvc) {
0917 #ifdef DEBUG_PKT
0918 netdev_info(frad, "No PVC for received frame's DLCI %d\n",
0919 dlci);
0920 #endif
0921 goto rx_drop;
0922 }
0923
0924 if (pvc->state.fecn != fh->fecn) {
0925 #ifdef DEBUG_ECN
0926 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
0927 dlci, fh->fecn ? "N" : "FF");
0928 #endif
0929 pvc->state.fecn ^= 1;
0930 }
0931
0932 if (pvc->state.becn != fh->becn) {
0933 #ifdef DEBUG_ECN
0934 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
0935 dlci, fh->becn ? "N" : "FF");
0936 #endif
0937 pvc->state.becn ^= 1;
0938 }
0939
0940 skb = skb_share_check(skb, GFP_ATOMIC);
0941 if (!skb) {
0942 frad->stats.rx_dropped++;
0943 return NET_RX_DROP;
0944 }
0945
0946 if (data[3] == NLPID_IP) {
0947 if (!pvc->main)
0948 goto rx_drop;
0949 skb_pull(skb, 4);
0950 skb->dev = pvc->main;
0951 skb->protocol = htons(ETH_P_IP);
0952 skb_reset_mac_header(skb);
0953
0954 } else if (data[3] == NLPID_IPV6) {
0955 if (!pvc->main)
0956 goto rx_drop;
0957 skb_pull(skb, 4);
0958 skb->dev = pvc->main;
0959 skb->protocol = htons(ETH_P_IPV6);
0960 skb_reset_mac_header(skb);
0961
0962 } else if (data[3] == FR_PAD) {
0963 if (skb->len < 5)
0964 goto rx_error;
0965 if (data[4] == NLPID_SNAP) {
0966 skb_pull(skb, 5);
0967 if (skb->len < 5)
0968 goto rx_error;
0969 if (fr_snap_parse(skb, pvc))
0970 goto rx_drop;
0971 } else {
0972 goto rx_drop;
0973 }
0974
0975 } else {
0976 netdev_info(frad, "Unsupported protocol, NLPID=%x length=%i\n",
0977 data[3], skb->len);
0978 goto rx_drop;
0979 }
0980
0981 dev = skb->dev;
0982 dev->stats.rx_packets++;
0983 dev->stats.rx_bytes += skb->len;
0984 if (pvc->state.becn)
0985 dev->stats.rx_compressed++;
0986 netif_rx(skb);
0987 return NET_RX_SUCCESS;
0988
0989 rx_error:
0990 frad->stats.rx_errors++;
0991 rx_drop:
0992 dev_kfree_skb_any(skb);
0993 return NET_RX_DROP;
0994 }
0995
0996 static void fr_start(struct net_device *dev)
0997 {
0998 hdlc_device *hdlc = dev_to_hdlc(dev);
0999 #ifdef DEBUG_LINK
1000 printk(KERN_DEBUG "fr_start\n");
1001 #endif
1002 if (state(hdlc)->settings.lmi != LMI_NONE) {
1003 state(hdlc)->reliable = 0;
1004 state(hdlc)->dce_changed = 1;
1005 state(hdlc)->request = 0;
1006 state(hdlc)->fullrep_sent = 0;
1007 state(hdlc)->last_errors = 0xFFFFFFFF;
1008 state(hdlc)->n391cnt = 0;
1009 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1010
1011 state(hdlc)->dev = dev;
1012 timer_setup(&state(hdlc)->timer, fr_timer, 0);
1013
1014 state(hdlc)->timer.expires = jiffies + HZ;
1015 add_timer(&state(hdlc)->timer);
1016 } else {
1017 fr_set_link_state(1, dev);
1018 }
1019 }
1020
1021 static void fr_stop(struct net_device *dev)
1022 {
1023 hdlc_device *hdlc = dev_to_hdlc(dev);
1024 #ifdef DEBUG_LINK
1025 printk(KERN_DEBUG "fr_stop\n");
1026 #endif
1027 if (state(hdlc)->settings.lmi != LMI_NONE)
1028 del_timer_sync(&state(hdlc)->timer);
1029 fr_set_link_state(0, dev);
1030 }
1031
1032 static void fr_close(struct net_device *dev)
1033 {
1034 hdlc_device *hdlc = dev_to_hdlc(dev);
1035 struct pvc_device *pvc = state(hdlc)->first_pvc;
1036
1037 while (pvc) {
1038 if (pvc->main)
1039 dev_close(pvc->main);
1040 if (pvc->ether)
1041 dev_close(pvc->ether);
1042 pvc = pvc->next;
1043 }
1044 }
1045
1046 static void pvc_setup(struct net_device *dev)
1047 {
1048 dev->type = ARPHRD_DLCI;
1049 dev->flags = IFF_POINTOPOINT;
1050 dev->hard_header_len = 0;
1051 dev->addr_len = 2;
1052 netif_keep_dst(dev);
1053 }
1054
1055 static const struct net_device_ops pvc_ops = {
1056 .ndo_open = pvc_open,
1057 .ndo_stop = pvc_close,
1058 .ndo_start_xmit = pvc_xmit,
1059 .ndo_siocwandev = pvc_ioctl,
1060 };
1061
1062 static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1063 {
1064 hdlc_device *hdlc = dev_to_hdlc(frad);
1065 struct pvc_device *pvc;
1066 struct net_device *dev;
1067 int used;
1068
1069 pvc = add_pvc(frad, dlci);
1070 if (!pvc) {
1071 netdev_warn(frad, "Memory squeeze on fr_add_pvc()\n");
1072 return -ENOBUFS;
1073 }
1074
1075 if (*get_dev_p(pvc, type))
1076 return -EEXIST;
1077
1078 used = pvc_is_used(pvc);
1079
1080 if (type == ARPHRD_ETHER)
1081 dev = alloc_netdev(0, "pvceth%d", NET_NAME_UNKNOWN,
1082 ether_setup);
1083 else
1084 dev = alloc_netdev(0, "pvc%d", NET_NAME_UNKNOWN, pvc_setup);
1085
1086 if (!dev) {
1087 netdev_warn(frad, "Memory squeeze on fr_pvc()\n");
1088 delete_unused_pvcs(hdlc);
1089 return -ENOBUFS;
1090 }
1091
1092 if (type == ARPHRD_ETHER) {
1093 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1094 eth_hw_addr_random(dev);
1095 } else {
1096 __be16 addr = htons(dlci);
1097
1098 dev_addr_set(dev, (u8 *)&addr);
1099 dlci_to_q922(dev->broadcast, dlci);
1100 }
1101 dev->netdev_ops = &pvc_ops;
1102 dev->mtu = HDLC_MAX_MTU;
1103 dev->min_mtu = 68;
1104 dev->max_mtu = HDLC_MAX_MTU;
1105 dev->needed_headroom = 10;
1106 dev->priv_flags |= IFF_NO_QUEUE;
1107 dev->ml_priv = pvc;
1108
1109 if (register_netdevice(dev) != 0) {
1110 free_netdev(dev);
1111 delete_unused_pvcs(hdlc);
1112 return -EIO;
1113 }
1114
1115 dev->needs_free_netdev = true;
1116 *get_dev_p(pvc, type) = dev;
1117 if (!used) {
1118 state(hdlc)->dce_changed = 1;
1119 state(hdlc)->dce_pvc_count++;
1120 }
1121 return 0;
1122 }
1123
1124 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1125 {
1126 struct pvc_device *pvc;
1127 struct net_device *dev;
1128
1129 pvc = find_pvc(hdlc, dlci);
1130 if (!pvc)
1131 return -ENOENT;
1132
1133 dev = *get_dev_p(pvc, type);
1134 if (!dev)
1135 return -ENOENT;
1136
1137 if (dev->flags & IFF_UP)
1138 return -EBUSY;
1139
1140 unregister_netdevice(dev);
1141 *get_dev_p(pvc, type) = NULL;
1142
1143 if (!pvc_is_used(pvc)) {
1144 state(hdlc)->dce_pvc_count--;
1145 state(hdlc)->dce_changed = 1;
1146 }
1147 delete_unused_pvcs(hdlc);
1148 return 0;
1149 }
1150
1151 static void fr_destroy(struct net_device *frad)
1152 {
1153 hdlc_device *hdlc = dev_to_hdlc(frad);
1154 struct pvc_device *pvc = state(hdlc)->first_pvc;
1155
1156 state(hdlc)->first_pvc = NULL;
1157 state(hdlc)->dce_pvc_count = 0;
1158 state(hdlc)->dce_changed = 1;
1159
1160 while (pvc) {
1161 struct pvc_device *next = pvc->next;
1162
1163 if (pvc->main)
1164 unregister_netdevice(pvc->main);
1165
1166 if (pvc->ether)
1167 unregister_netdevice(pvc->ether);
1168
1169 kfree(pvc);
1170 pvc = next;
1171 }
1172 }
1173
1174 static struct hdlc_proto proto = {
1175 .close = fr_close,
1176 .start = fr_start,
1177 .stop = fr_stop,
1178 .detach = fr_destroy,
1179 .ioctl = fr_ioctl,
1180 .netif_rx = fr_rx,
1181 .module = THIS_MODULE,
1182 };
1183
1184 static int fr_ioctl(struct net_device *dev, struct if_settings *ifs)
1185 {
1186 fr_proto __user *fr_s = ifs->ifs_ifsu.fr;
1187 const size_t size = sizeof(fr_proto);
1188 fr_proto new_settings;
1189 hdlc_device *hdlc = dev_to_hdlc(dev);
1190 fr_proto_pvc pvc;
1191 int result;
1192
1193 switch (ifs->type) {
1194 case IF_GET_PROTO:
1195 if (dev_to_hdlc(dev)->proto != &proto)
1196 return -EINVAL;
1197 ifs->type = IF_PROTO_FR;
1198 if (ifs->size < size) {
1199 ifs->size = size;
1200 return -ENOBUFS;
1201 }
1202 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1203 return -EFAULT;
1204 return 0;
1205
1206 case IF_PROTO_FR:
1207 if (!capable(CAP_NET_ADMIN))
1208 return -EPERM;
1209
1210 if (dev->flags & IFF_UP)
1211 return -EBUSY;
1212
1213 if (copy_from_user(&new_settings, fr_s, size))
1214 return -EFAULT;
1215
1216 if (new_settings.lmi == LMI_DEFAULT)
1217 new_settings.lmi = LMI_ANSI;
1218
1219 if ((new_settings.lmi != LMI_NONE &&
1220 new_settings.lmi != LMI_ANSI &&
1221 new_settings.lmi != LMI_CCITT &&
1222 new_settings.lmi != LMI_CISCO) ||
1223 new_settings.t391 < 1 ||
1224 new_settings.t392 < 2 ||
1225 new_settings.n391 < 1 ||
1226 new_settings.n392 < 1 ||
1227 new_settings.n393 < new_settings.n392 ||
1228 new_settings.n393 > 32 ||
1229 (new_settings.dce != 0 &&
1230 new_settings.dce != 1))
1231 return -EINVAL;
1232
1233 result = hdlc->attach(dev, ENCODING_NRZ,
1234 PARITY_CRC16_PR1_CCITT);
1235 if (result)
1236 return result;
1237
1238 if (dev_to_hdlc(dev)->proto != &proto) {
1239 result = attach_hdlc_protocol(dev, &proto,
1240 sizeof(struct frad_state));
1241 if (result)
1242 return result;
1243 state(hdlc)->first_pvc = NULL;
1244 state(hdlc)->dce_pvc_count = 0;
1245 }
1246 memcpy(&state(hdlc)->settings, &new_settings, size);
1247 dev->type = ARPHRD_FRAD;
1248 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1249 return 0;
1250
1251 case IF_PROTO_FR_ADD_PVC:
1252 case IF_PROTO_FR_DEL_PVC:
1253 case IF_PROTO_FR_ADD_ETH_PVC:
1254 case IF_PROTO_FR_DEL_ETH_PVC:
1255 if (dev_to_hdlc(dev)->proto != &proto)
1256 return -EINVAL;
1257
1258 if (!capable(CAP_NET_ADMIN))
1259 return -EPERM;
1260
1261 if (copy_from_user(&pvc, ifs->ifs_ifsu.fr_pvc,
1262 sizeof(fr_proto_pvc)))
1263 return -EFAULT;
1264
1265 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1266 return -EINVAL;
1267
1268 if (ifs->type == IF_PROTO_FR_ADD_ETH_PVC ||
1269 ifs->type == IF_PROTO_FR_DEL_ETH_PVC)
1270 result = ARPHRD_ETHER;
1271 else
1272 result = ARPHRD_DLCI;
1273
1274 if (ifs->type == IF_PROTO_FR_ADD_PVC ||
1275 ifs->type == IF_PROTO_FR_ADD_ETH_PVC)
1276 return fr_add_pvc(dev, pvc.dlci, result);
1277 else
1278 return fr_del_pvc(hdlc, pvc.dlci, result);
1279 }
1280
1281 return -EINVAL;
1282 }
1283
1284 static int __init hdlc_fr_init(void)
1285 {
1286 register_hdlc_protocol(&proto);
1287 return 0;
1288 }
1289
1290 static void __exit hdlc_fr_exit(void)
1291 {
1292 unregister_hdlc_protocol(&proto);
1293 }
1294
1295 module_init(hdlc_fr_init);
1296 module_exit(hdlc_fr_exit);
1297
1298 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1299 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1300 MODULE_LICENSE("GPL v2");