File: /usr/src/linux/drivers/net/wan/hdlc.c

1     /*
2      * Generic HDLC support routines for Linux
3      *
4      * Copyright (C) 1999, 2000 Krzysztof Halasa <khc@pm.waw.pl>
5      *
6      * This program is free software; you can redistribute it and/or modify it
7      * under the terms of the GNU General Public License as published by
8      * the Free Software Foundation; either version 2 of the License, or
9      * (at your option) any later version.
10      *
11      * Current status:
12      *    - this is work in progress
13      *    - not heavily tested on SMP
14      *    - currently supported:
15      *	* raw IP-in-HDLC
16      *	* Cisco HDLC
17      *	* Frame Relay with ANSI or CCITT LMI (both user and network side)
18      *	* PPP (using syncppp.c)
19      *	* X.25
20      *
21      * Use sethdlc utility to set line parameters, protocol and PVCs
22      */
23     
24     #include <linux/config.h>
25     #include <linux/module.h>
26     #include <linux/kernel.h>
27     #include <linux/slab.h>
28     #include <linux/poll.h>
29     #include <linux/sched.h>
30     #include <linux/errno.h>
31     #include <linux/if_arp.h>
32     #include <linux/init.h>
33     #include <linux/skbuff.h>
34     #include <linux/pkt_sched.h>
35     #include <linux/inetdevice.h>
36     #include <linux/lapb.h>
37     #include <linux/rtnetlink.h>
38     #include <linux/hdlc.h>
39     
40     /* #define DEBUG_PKT */
41     /* #define DEBUG_HARD_HEADER */
42     /* #define DEBUG_FECN */
43     /* #define DEBUG_BECN */
44     
45     static const char* version = "HDLC support module revision 1.02 for Linux 2.4";
46     
47     
48     #define CISCO_MULTICAST		0x8F	/* Cisco multicast address */
49     #define CISCO_UNICAST		0x0F	/* Cisco unicast address */
50     #define CISCO_KEEPALIVE		0x8035	/* Cisco keepalive protocol */
51     #define CISCO_SYS_INFO		0x2000	/* Cisco interface/system info */
52     #define CISCO_ADDR_REQ		0	/* Cisco address request */
53     #define CISCO_ADDR_REPLY	1	/* Cisco address reply */
54     #define CISCO_KEEPALIVE_REQ	2	/* Cisco keepalive request */
55     
56     static int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
57     
58     /********************************************************
59      *
60      * Cisco HDLC support
61      *
62      *******************************************************/
63     
64     static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
65     			     u16 type, void *daddr, void *saddr,
66     			     unsigned int len)
67     {
68     	hdlc_header *data;
69     #ifdef DEBUG_HARD_HEADER
70     	printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
71     #endif
72     
73     	skb_push(skb, sizeof(hdlc_header));
74     	data = (hdlc_header*)skb->data;
75     	if (type == CISCO_KEEPALIVE)
76     		data->address = CISCO_MULTICAST;
77     	else
78     		data->address = CISCO_UNICAST;
79     	data->control = 0;
80     	data->protocol = htons(type);
81     
82     	return sizeof(hdlc_header);
83     }
84     
85     
86     
87     static void cisco_keepalive_send(hdlc_device *hdlc, u32 type,
88     				 u32 par1, u32 par2)
89     {
90     	struct sk_buff *skb;
91     	cisco_packet *data;
92     
93     	skb = dev_alloc_skb(sizeof(hdlc_header)+sizeof(cisco_packet));
94     	if (!skb) {
95     		printk(KERN_WARNING "%s: Memory squeeze on cisco_keepalive_send()\n",
96     			       hdlc_to_name(hdlc));
97     		return;
98     	}
99     	skb_reserve(skb, 4);
100     	cisco_hard_header(skb, hdlc_to_dev(hdlc), CISCO_KEEPALIVE,
101     			  NULL, NULL, 0);
102     	data = (cisco_packet*)skb->tail;
103     
104     	data->type = htonl(type);
105     	data->par1 = htonl(par1);
106     	data->par2 = htonl(par2);
107     	data->rel = 0xFFFF;
108     	data->time = htonl(jiffies * 1000 / HZ);
109     
110     	skb_put(skb, sizeof(cisco_packet));
111     	skb->priority = TC_PRIO_CONTROL;
112     	skb->dev = hdlc_to_dev(hdlc);
113     
114     	dev_queue_xmit(skb);
115     }
116     
117     
118     
119     static void cisco_netif(hdlc_device *hdlc, struct sk_buff *skb)
120     {
121     	hdlc_header *data = (hdlc_header*)skb->data;
122     	cisco_packet *cisco_data;
123     	struct in_device *in_dev;
124     	u32 addr, mask;
125     
126     	if (skb->len<sizeof(hdlc_header))
127     		goto rx_error;
128     
129     	if (data->address != CISCO_MULTICAST &&
130     	    data->address != CISCO_UNICAST)
131     		goto rx_error;
132     
133     	skb_pull(skb, sizeof(hdlc_header));
134     
135     	switch(ntohs(data->protocol)) {
136     	case ETH_P_IP:
137     	case ETH_P_IPX:
138     	case ETH_P_IPV6:
139     		skb->protocol = data->protocol;
140     		skb->dev = hdlc_to_dev(hdlc);
141     		netif_rx(skb);
142     		return;
143     
144     	case CISCO_SYS_INFO:
145     		/* Packet is not needed, drop it. */
146     		dev_kfree_skb_any(skb);
147     		return;
148     
149     	case CISCO_KEEPALIVE:
150     		if (skb->len != CISCO_PACKET_LEN &&
151     		    skb->len != CISCO_BIG_PACKET_LEN) {
152     			printk(KERN_INFO "%s: Invalid length of Cisco "
153     			       "control packet (%d bytes)\n",
154     			       hdlc_to_name(hdlc), skb->len);
155     			goto rx_error;
156     		}
157     
158     		cisco_data = (cisco_packet*)skb->data;
159     
160     		switch(ntohl (cisco_data->type)) {
161     		case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
162     			in_dev = hdlc_to_dev(hdlc)->ip_ptr;
163     			addr = 0;
164     			mask = ~0; /* is the mask correct? */
165     
166     			if (in_dev != NULL) {
167     				struct in_ifaddr **ifap = &in_dev->ifa_list;
168     
169     				while (*ifap != NULL) {
170     					if (strcmp(hdlc_to_name(hdlc),
171     						   (*ifap)->ifa_label) == 0) {
172     						addr = (*ifap)->ifa_local;
173     						mask = (*ifap)->ifa_mask;
174     						break;
175     					}
176     					ifap = &(*ifap)->ifa_next;
177     				}
178     
179     				cisco_keepalive_send(hdlc, CISCO_ADDR_REPLY,
180     						     addr, mask);
181     			}
182     			dev_kfree_skb_any(skb);
183     			return;
184     
185     		case CISCO_ADDR_REPLY:
186     			printk(KERN_INFO "%s: Unexpected Cisco IP address "
187     			       "reply\n", hdlc_to_name(hdlc));
188     			goto rx_error;
189     
190     		case CISCO_KEEPALIVE_REQ:
191     			hdlc->lmi.rxseq = ntohl(cisco_data->par1);
192     			if (ntohl(cisco_data->par2) == hdlc->lmi.txseq) {
193     				hdlc->lmi.last_poll = jiffies;
194     				if (!(hdlc->lmi.state & LINK_STATE_RELIABLE)) {
195     					u32 sec, min, hrs, days;
196     					sec = ntohl(cisco_data->time) / 1000;
197     					min = sec / 60; sec -= min * 60;
198     					hrs = min / 60; min -= hrs * 60;
199     					days = hrs / 24; hrs -= days * 24;
200     					printk(KERN_INFO "%s: Link up (peer "
201     					       "uptime %ud%uh%um%us)\n",
202     					       hdlc_to_name(hdlc), days, hrs,
203     					       min, sec);
204     				}
205     				hdlc->lmi.state |= LINK_STATE_RELIABLE;
206     			}
207     
208     			dev_kfree_skb_any(skb);
209     			return;
210     		} /* switch(keepalive type) */
211     	} /* switch(protocol) */
212     
213     	printk(KERN_INFO "%s: Unsupported protocol %x\n", hdlc_to_name(hdlc),
214     	       data->protocol);
215     	dev_kfree_skb_any(skb);
216     	return;
217     
218      rx_error:
219     	hdlc->stats.rx_errors++; /* Mark error */
220     	dev_kfree_skb_any(skb);
221     }
222     
223     
224     
225     static void cisco_timer(unsigned long arg)
226     {
227     	hdlc_device *hdlc = (hdlc_device*)arg;
228     
229     	if ((hdlc->lmi.state & LINK_STATE_RELIABLE) &&
230     	    (jiffies - hdlc->lmi.last_poll >= hdlc->lmi.T392 * HZ)) {
231     		hdlc->lmi.state &= ~LINK_STATE_RELIABLE;
232     		printk(KERN_INFO "%s: Link down\n", hdlc_to_name(hdlc));
233     	}
234     
235     	cisco_keepalive_send(hdlc, CISCO_KEEPALIVE_REQ, ++hdlc->lmi.txseq,
236     			     hdlc->lmi.rxseq);
237     	hdlc->timer.expires = jiffies + hdlc->lmi.T391*HZ;
238     
239     	hdlc->timer.function = cisco_timer;
240     	hdlc->timer.data = arg;
241     	add_timer(&hdlc->timer);
242     }
243     
244     
245     
246     /******************************************************************
247      *
248      *     generic Frame Relay routines
249      *
250      *****************************************************************/
251     
252     
253     static int fr_hard_header(struct sk_buff *skb, struct net_device *dev,
254     			  u16 type, void *daddr, void *saddr, unsigned int len)
255     {
256     	u16 head_len;
257     
258     	if (!daddr)
259     		daddr = dev->broadcast;
260     
261     #ifdef DEBUG_HARD_HEADER
262     	printk(KERN_DEBUG "%s: fr_hard_header called\n", dev->name);
263     #endif
264     
265     	switch(type) {
266     	case ETH_P_IP:
267     		head_len = 4;
268     		skb_push(skb, head_len);
269     		skb->data[3] = NLPID_IP;
270     		break;
271     
272     	case ETH_P_IPV6:
273     		head_len = 4;
274     		skb_push(skb, head_len);
275     		skb->data[3] = NLPID_IPV6;
276     		break;
277     
278     	case LMI_PROTO:
279     		head_len = 4;
280     		skb_push(skb, head_len);
281     		skb->data[3] = LMI_PROTO;
282     		break;
283     
284     	default:
285     		head_len = 10;
286     		skb_push(skb, head_len);
287     		skb->data[3] = FR_PAD;
288     		skb->data[4] = NLPID_SNAP;
289     		skb->data[5] = FR_PAD;
290     		skb->data[6] = FR_PAD;
291     		skb->data[7] = FR_PAD;
292     		skb->data[8] = type>>8;
293     		skb->data[9] = (u8)type;
294     	}
295     
296     	memcpy(skb->data, daddr, 2);
297     	skb->data[2] = FR_UI;
298     
299     	return head_len;
300     }
301     
302     
303     
304     static inline void fr_log_dlci_active(pvc_device *pvc)
305     {
306     	printk(KERN_INFO "%s: %sactive%s\n", pvc_to_name(pvc),
307     	       pvc->state & PVC_STATE_ACTIVE ? "" : "in",
308     	       pvc->state & PVC_STATE_NEW ? " new" : "");
309     }
310     
311     
312     
313     static inline u8 fr_lmi_nextseq(u8 x)
314     {
315     	x++;
316     	return x ? x : 1;
317     }
318     
319     
320     
321     static void fr_lmi_send(hdlc_device *hdlc, int fullrep)
322     {
323     	struct sk_buff *skb;
324     	pvc_device *pvc = hdlc->first_pvc;
325     	int len = mode_is(hdlc, MODE_FR_ANSI) ? LMI_ANSI_LENGTH : LMI_LENGTH;
326     	int stat_len = 3;
327     	u8 *data;
328     	int i = 0;
329     
330     	if (mode_is(hdlc, MODE_DCE) && fullrep) {
331     		len += hdlc->pvc_count * (2 + stat_len);
332     		if (len > HDLC_MAX_MTU) {
333     			printk(KERN_WARNING "%s: Too many PVCs while sending "
334     			       "LMI full report\n", hdlc_to_name(hdlc));
335     			return;
336     		}
337     	}
338     
339     	skb = dev_alloc_skb(len);
340     	if (!skb) {
341     		printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
342     			       hdlc_to_name(hdlc));
343     		return;
344     	}
345     	memset(skb->data, 0, len);
346     	skb_reserve(skb, 4);
347     	fr_hard_header(skb, hdlc_to_dev(hdlc), LMI_PROTO, NULL, NULL, 0);
348     	data = skb->tail;
349     	data[i++] = LMI_CALLREF;
350     	data[i++] = mode_is(hdlc, MODE_DCE) ? LMI_STATUS : LMI_STATUS_ENQUIRY;
351     	if (mode_is(hdlc, MODE_FR_ANSI))
352     		data[i++] = LMI_ANSI_LOCKSHIFT;
353     	data[i++] = mode_is(hdlc, MODE_FR_CCITT) ? LMI_CCITT_REPTYPE :
354     		LMI_REPTYPE;
355     	data[i++] = LMI_REPT_LEN;
356     	data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
357     
358     	data[i++] = mode_is(hdlc, MODE_FR_CCITT) ? LMI_CCITT_ALIVE : LMI_ALIVE;
359     	data[i++] = LMI_INTEG_LEN;
360     	data[i++] = hdlc->lmi.txseq = fr_lmi_nextseq(hdlc->lmi.txseq);
361     	data[i++] = hdlc->lmi.rxseq;
362     
363     	if (mode_is(hdlc, MODE_DCE) && fullrep) {
364     		while (pvc) {
365     			data[i++] = mode_is(hdlc, MODE_FR_CCITT) ?
366     				LMI_CCITT_PVCSTAT:LMI_PVCSTAT;
367     			data[i++] = stat_len;
368     
369     			if ((hdlc->lmi.state & LINK_STATE_RELIABLE) &&
370     			    (pvc->netdev.flags & IFF_UP) &&
371     			    !(pvc->state & (PVC_STATE_ACTIVE|PVC_STATE_NEW))) {
372     				pvc->state |= PVC_STATE_NEW;
373     				fr_log_dlci_active(pvc);
374     			}
375     
376     			dlci_to_status(hdlc, netdev_dlci(&pvc->netdev),
377     				       data+i, pvc->state);
378     			i += stat_len;
379     			pvc = pvc->next;
380     		}
381     	}
382     
383     	skb_put(skb, i);
384     	skb->priority = TC_PRIO_CONTROL;
385     	skb->dev = hdlc_to_dev(hdlc);
386     
387     	dev_queue_xmit(skb);
388     }
389     
390     
391     
392     static void fr_timer(unsigned long arg)
393     {
394     	hdlc_device *hdlc = (hdlc_device*)arg;
395     	int i, cnt = 0, reliable;
396     	u32 list;
397     
398     	if (mode_is(hdlc, MODE_DCE))
399     		reliable = (jiffies - hdlc->lmi.last_poll < hdlc->lmi.T392*HZ);
400     	else {
401     		hdlc->lmi.last_errors <<= 1; /* Shift the list */
402     		if (hdlc->lmi.state & LINK_STATE_REQUEST) {
403     			printk(KERN_INFO "%s: No LMI status reply received\n",
404     			       hdlc_to_name(hdlc));
405     			hdlc->lmi.last_errors |= 1;
406     		}
407     
408     		for (i = 0, list = hdlc->lmi.last_errors; i < hdlc->lmi.N393;
409     		     i++, list >>= 1)
410     			cnt += (list & 1);	/* errors count */
411     
412     		reliable = (cnt < hdlc->lmi.N392);
413     	}
414     
415     	if ((hdlc->lmi.state & LINK_STATE_RELIABLE) !=
416     	    (reliable ? LINK_STATE_RELIABLE : 0)) {
417     		pvc_device *pvc = hdlc->first_pvc;
418     
419     		while (pvc) {/* Deactivate all PVCs */
420     			pvc->state &= ~(PVC_STATE_NEW | PVC_STATE_ACTIVE);
421     			pvc = pvc->next;
422     		}
423     
424     		hdlc->lmi.state ^= LINK_STATE_RELIABLE;
425     		printk(KERN_INFO "%s: Link %sreliable\n", hdlc_to_name(hdlc),
426     		       reliable ? "" : "un");
427     
428     		if (reliable) {
429     			hdlc->lmi.N391cnt = 0; /* Request full status */
430     			hdlc->lmi.state |= LINK_STATE_CHANGED;
431     		}
432     	}
433     
434     	if (mode_is(hdlc, MODE_DCE))
435     		hdlc->timer.expires = jiffies + hdlc->lmi.T392*HZ;
436     	else {
437     		if (hdlc->lmi.N391cnt)
438     			hdlc->lmi.N391cnt--;
439     
440     		fr_lmi_send(hdlc, hdlc->lmi.N391cnt == 0);
441     
442     		hdlc->lmi.state |= LINK_STATE_REQUEST;
443     		hdlc->timer.expires = jiffies + hdlc->lmi.T391*HZ;
444     	}
445     
446     	hdlc->timer.function = fr_timer;
447     	hdlc->timer.data = arg;
448     	add_timer(&hdlc->timer);
449     }
450     
451     
452     
453     static int fr_lmi_recv(hdlc_device *hdlc, struct sk_buff *skb)
454     {
455     	int stat_len;
456     	pvc_device *pvc;
457     	int reptype = -1, error;
458     	u8 rxseq, txseq;
459     	int i;
460     
461     	if (skb->len < (mode_is(hdlc, MODE_FR_ANSI) ?
462     			LMI_ANSI_LENGTH : LMI_LENGTH)) {
463     		printk(KERN_INFO "%s: Short LMI frame\n", hdlc_to_name(hdlc));
464     		return 1;
465     	}
466     
467     	if (skb->data[5] != (!mode_is(hdlc, MODE_DCE) ?
468     			     LMI_STATUS : LMI_STATUS_ENQUIRY)) {
469     		printk(KERN_INFO "%s: LMI msgtype=%x, Not LMI status %s\n",
470     		       hdlc_to_name(hdlc), skb->data[2],
471     		       mode_is(hdlc, MODE_DCE) ? "enquiry" : "reply");
472     		return 1;
473     	}
474     
475     	i = mode_is(hdlc, MODE_FR_ANSI) ? 7 : 6;
476     
477     	if (skb->data[i] !=
478     	    (mode_is(hdlc, MODE_FR_CCITT) ? LMI_CCITT_REPTYPE : LMI_REPTYPE)) {
479     		printk(KERN_INFO "%s: Not a report type=%x\n",
480     		       hdlc_to_name(hdlc), skb->data[i]);
481     		return 1;
482     	}
483     	i++;
484     
485     	i++;				/* Skip length field */
486     
487     	reptype = skb->data[i++];
488     
489     	if (skb->data[i]!=
490     	    (mode_is(hdlc, MODE_FR_CCITT) ? LMI_CCITT_ALIVE : LMI_ALIVE)) {
491     		printk(KERN_INFO "%s: Unsupported status element=%x\n",
492     		       hdlc_to_name(hdlc), skb->data[i]);
493     		return 1;
494     	}
495     	i++;
496     
497     	i++;			/* Skip length field */
498     
499     	hdlc->lmi.rxseq = skb->data[i++]; /* TX sequence from peer */
500     	rxseq = skb->data[i++];	/* Should confirm our sequence */
501     
502     	txseq = hdlc->lmi.txseq;
503     
504     	if (mode_is(hdlc, MODE_DCE)) {
505     		if (reptype != LMI_FULLREP && reptype != LMI_INTEGRITY) {
506     			printk(KERN_INFO "%s: Unsupported report type=%x\n",
507     			       hdlc_to_name(hdlc), reptype);
508     			return 1;
509     		}
510     	}
511     
512     	error = 0;
513     	if (!(hdlc->lmi.state & LINK_STATE_RELIABLE))
514     		error = 1;
515     
516     	if (rxseq == 0 || rxseq != txseq) {
517     		hdlc->lmi.N391cnt = 0; /* Ask for full report next time */
518     		error = 1;
519     	}
520     
521     	if (mode_is(hdlc, MODE_DCE)) {
522     		if ((hdlc->lmi.state & LINK_STATE_FULLREP_SENT) && !error) {
523     /* Stop sending full report - the last one has been confirmed by DTE */
524     			hdlc->lmi.state &= ~LINK_STATE_FULLREP_SENT;
525     			pvc = hdlc->first_pvc;
526     			while (pvc) {
527     				if (pvc->state & PVC_STATE_NEW) {
528     					pvc->state &= ~PVC_STATE_NEW;
529     					pvc->state |= PVC_STATE_ACTIVE;
530     					fr_log_dlci_active(pvc);
531     
532     /* Tell DTE that new PVC is now active */
533     					hdlc->lmi.state |= LINK_STATE_CHANGED;
534     				}
535     				pvc = pvc->next;
536     			}
537     		}
538     
539     		if (hdlc->lmi.state & LINK_STATE_CHANGED) {
540     			reptype = LMI_FULLREP;
541     			hdlc->lmi.state |= LINK_STATE_FULLREP_SENT;
542     			hdlc->lmi.state &= ~LINK_STATE_CHANGED;
543     		}
544     
545     		fr_lmi_send(hdlc, reptype == LMI_FULLREP ? 1 : 0);
546     		return 0;
547     	}
548     
549     	/* DTE */
550     
551     	if (reptype != LMI_FULLREP || error)
552     		return 0;
553     
554     	stat_len = 3;
555     	pvc = hdlc->first_pvc;
556     
557     	while (pvc) {
558     		pvc->newstate = 0;
559     		pvc = pvc->next;
560     	}
561     
562     	while (skb->len >= i + 2 + stat_len) {
563     		u16 dlci;
564     		u8 state = 0;
565     
566     		if (skb->data[i] != (mode_is(hdlc, MODE_FR_CCITT) ?
567     				     LMI_CCITT_PVCSTAT : LMI_PVCSTAT)) {
568     			printk(KERN_WARNING "%s: Invalid PVCSTAT ID: %x\n",
569     			       hdlc_to_name(hdlc), skb->data[i]);
570     			return 1;
571     		}
572     		i++;
573     
574     		if (skb->data[i] != stat_len) {
575     			printk(KERN_WARNING "%s: Invalid PVCSTAT length: %x\n",
576     			       hdlc_to_name(hdlc), skb->data[i]);
577     			return 1;
578     		}
579     		i++;
580     
581     		dlci = status_to_dlci(hdlc, skb->data+i, &state);
582     		pvc = find_pvc(hdlc, dlci);
583     
584     		if (pvc)
585     			pvc->newstate = state;
586     		else if (state == PVC_STATE_NEW)
587     			printk(KERN_INFO "%s: new PVC available, DLCI=%u\n",
588     			       hdlc_to_name(hdlc), dlci);
589     
590     		i += stat_len;
591     	}
592     
593     	pvc = hdlc->first_pvc;
594     
595     	while (pvc) {
596     		if (pvc->newstate == PVC_STATE_NEW)
597     			pvc->newstate = PVC_STATE_ACTIVE;
598     
599     		pvc->newstate |= (pvc->state &
600     				  ~(PVC_STATE_NEW|PVC_STATE_ACTIVE));
601     		if (pvc->state != pvc->newstate) {
602     			pvc->state = pvc->newstate;
603     			fr_log_dlci_active(pvc);
604     		}
605     		pvc = pvc->next;
606     	}
607     
608     	/* Next full report after N391 polls */
609     	hdlc->lmi.N391cnt = hdlc->lmi.N391;
610     
611     	return 0;
612     }
613     
614     
615     
616     static void fr_netif(hdlc_device *hdlc, struct sk_buff *skb)
617     {
618     	fr_hdr *fh = (fr_hdr*)skb->data;
619     	u8 *data = skb->data;
620     	u16 dlci;
621     	pvc_device *pvc;
622     
623     	if (skb->len<4 || fh->ea1 || data[2] != FR_UI)
624     		goto rx_error;
625     
626     	dlci = q922_to_dlci(skb->data);
627     
628     	if (dlci == LMI_DLCI) {
629     		if (data[3] == LMI_PROTO) {
630     			if (fr_lmi_recv(hdlc, skb))
631     				goto rx_error;
632     			else {
633     				/* No request pending */
634     				hdlc->lmi.state &= ~LINK_STATE_REQUEST;
635     				hdlc->lmi.last_poll = jiffies;
636     				dev_kfree_skb_any(skb);
637     				return;
638     			}
639     		}
640     
641     		printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
642     		       hdlc_to_name(hdlc));
643     		goto rx_error;
644     	}
645     
646     	pvc = find_pvc(hdlc, dlci);
647     	if (!pvc) {
648     #ifdef DEBUG_PKT
649     		printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
650     		       hdlc_to_name(hdlc), dlci);
651     #endif
652     		goto rx_error;
653     	}
654     
655     	if ((pvc->netdev.flags & IFF_UP) == 0) {
656     #ifdef DEBUG_PKT
657     		printk(KERN_INFO "%s: PVC for received frame's DLCI %d is down\n",
658     		       hdlc_to_name(hdlc), dlci);
659     #endif
660     		goto rx_error;
661     	}
662     
663     	pvc->stats.rx_packets++; /* PVC traffic */
664     	pvc->stats.rx_bytes += skb->len;
665     
666     	if ((pvc->state & PVC_STATE_FECN) != (fh->fecn ? PVC_STATE_FECN : 0)) {
667     #ifdef DEBUG_FECN
668     		printk(KERN_DEBUG "%s: FECN O%s\n", pvc_to_name(pvc),
669     		       fh->fecn ? "N" : "FF");
670     #endif
671     		pvc->state ^= PVC_STATE_FECN;
672     	}
673     
674     	if ((pvc->state & PVC_STATE_BECN) != (fh->becn ? PVC_STATE_BECN : 0)) {
675     #ifdef DEBUG_FECN
676     		printk(KERN_DEBUG "%s: BECN O%s\n", pvc_to_name(pvc),
677     		       fh->becn ? "N" : "FF");
678     #endif
679     		pvc->state ^= PVC_STATE_BECN;
680     	}
681     
682     	if (pvc->state & PVC_STATE_BECN)
683     		pvc->stats.rx_compressed++;
684     
685     	if (data[3] == NLPID_IP) {
686     		skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
687     		skb->protocol = htons(ETH_P_IP);
688     		skb->dev = &pvc->netdev;
689     		netif_rx(skb);
690     		return;
691     	}
692     
693     
694     	if (data[3] == NLPID_IPV6) {
695     		skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
696     		skb->protocol = htons(ETH_P_IPV6);
697     		skb->dev = &pvc->netdev;
698     		netif_rx(skb);
699     		return;
700     	}
701     
702     	if (data[3] == FR_PAD && data[4] == NLPID_SNAP && data[5] == FR_PAD &&
703     	    data[6] == FR_PAD && data[7] == FR_PAD &&
704     	    ((data[8]<<8) | data[9]) == ETH_P_ARP) {
705     		skb_pull(skb, 10);
706     		skb->protocol = htons(ETH_P_ARP);
707     		skb->dev = &pvc->netdev;
708     		netif_rx(skb);
709     		return;
710     	}
711     
712     	printk(KERN_INFO "%s: Unusupported protocol %x\n",
713     	       hdlc_to_name(hdlc), data[3]);
714     	dev_kfree_skb_any(skb);
715     	return;
716     
717      rx_error:
718     	hdlc->stats.rx_errors++; /* Mark error */
719     	dev_kfree_skb_any(skb);
720     }
721     
722     
723     
724     static void fr_cisco_open(hdlc_device *hdlc)
725     {
726     	hdlc->lmi.state = LINK_STATE_CHANGED;
727     	hdlc->lmi.txseq = hdlc->lmi.rxseq = 0;
728     	hdlc->lmi.last_errors = 0xFFFFFFFF;
729     	hdlc->lmi.N391cnt = 0;
730     
731     	init_timer(&hdlc->timer);
732     	hdlc->timer.expires = jiffies + HZ; /* First poll after 1 second */
733     	hdlc->timer.function = mode_is(hdlc, MODE_FR) ? fr_timer : cisco_timer;
734     	hdlc->timer.data = (unsigned long)hdlc;
735     	add_timer(&hdlc->timer);
736     }
737     
738     
739     
740     static void fr_cisco_close(hdlc_device *hdlc)
741     {
742     	pvc_device *pvc = hdlc->first_pvc;
743     
744     	del_timer_sync(&hdlc->timer);
745     
746     	while(pvc) {		/* NULL in Cisco mode */
747     		dev_close(&pvc->netdev); /* Shutdown all PVCs for this FRAD */
748     		pvc = pvc->next;
749     	}
750     }
751     
752     
753     
754     /******************************************************************
755      *
756      *     generic HDLC routines
757      *
758      *****************************************************************/
759     
760     
761     
762     static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
763     {
764     	if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
765     		return -EINVAL;
766     	dev->mtu = new_mtu;
767     	return 0;
768     }
769     
770     
771     
772     /********************************************************
773      *
774      * PVC device routines
775      *
776      *******************************************************/
777     
778     static int pvc_open(struct net_device *dev)
779     {
780     	pvc_device *pvc = dev_to_pvc(dev);
781     	int result = 0;
782     
783     	if ((hdlc_to_dev(pvc->master)->flags & IFF_UP) == 0)
784     		return -EIO;  /* Master must be UP in order to activate PVC */
785     
786     	memset(&(pvc->stats), 0, sizeof(struct net_device_stats));
787     	pvc->state = 0;
788     
789     	if (!mode_is(pvc->master, MODE_SOFT) && pvc->master->open_pvc)
790     		result = pvc->master->open_pvc(pvc);
791     	if (result)
792     		return result;
793     
794     	pvc->master->lmi.state |= LINK_STATE_CHANGED;
795     	return 0;
796     }
797     
798     
799     
800     static int pvc_close(struct net_device *dev)
801     {
802     	pvc_device *pvc = dev_to_pvc(dev);
803     	pvc->state = 0;
804     
805     	if (!mode_is(pvc->master, MODE_SOFT) && pvc->master->close_pvc)
806     		pvc->master->close_pvc(pvc);
807     
808     	pvc->master->lmi.state |= LINK_STATE_CHANGED;
809     	return 0;
810     }
811     
812     
813     
814     static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
815     {
816     	pvc_device *pvc = dev_to_pvc(dev);
817     
818     	if (pvc->state & PVC_STATE_ACTIVE) {
819     		skb->dev = hdlc_to_dev(pvc->master);
820     		pvc->stats.tx_bytes += skb->len;
821     		pvc->stats.tx_packets++;
822     		if (pvc->state & PVC_STATE_FECN)
823     			pvc->stats.tx_compressed++; /* TX Congestion counter */
824     		dev_queue_xmit(skb);
825     	} else {
826     		pvc->stats.tx_dropped++;
827     		dev_kfree_skb(skb);
828     	}
829     
830     	return 0;
831     }
832     
833     
834     
835     static struct net_device_stats *pvc_get_stats(struct net_device *dev)
836     {
837     	pvc_device *pvc = dev_to_pvc(dev);
838     	return &pvc->stats;
839     }
840     
841     
842     
843     static int pvc_change_mtu(struct net_device *dev, int new_mtu)
844     {
845     	if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
846     		return -EINVAL;
847     	dev->mtu = new_mtu;
848     	return 0;
849     }
850     
851     
852     
853     static void destroy_pvc_list(hdlc_device *hdlc)
854     {
855     	pvc_device *pvc = hdlc->first_pvc;
856     	while(pvc) {
857     		pvc_device *next = pvc->next;
858     		unregister_netdevice(&pvc->netdev);
859     		kfree(pvc);
860     		pvc = next;
861     	}
862     
863     	hdlc->first_pvc = NULL;	/* All PVCs destroyed */
864     	hdlc->pvc_count = 0;
865     	hdlc->lmi.state |= LINK_STATE_CHANGED;
866     }
867     
868     
869     
870     /********************************************************
871      *
872      * X.25 protocol support routines
873      *
874      *******************************************************/
875     
876     #ifdef CONFIG_HDLC_X25
877     /* These functions are callbacks called by LAPB layer */
878     
879     void x25_connect_disconnect(void *token, int reason, int code)
880     {
881     	hdlc_device *hdlc = token;
882     	struct sk_buff *skb;
883     	unsigned char *ptr;
884     
885     	if ((skb = dev_alloc_skb(1)) == NULL) {
886     		printk(KERN_ERR "%s: out of memory\n", hdlc_to_name(hdlc));
887     		return;
888     	}
889     
890     	ptr = skb_put(skb, 1);
891     	*ptr = code;
892     
893     	skb->dev = hdlc_to_dev(hdlc);
894     	skb->protocol = htons(ETH_P_X25);
895     	skb->mac.raw = skb->data;
896     	skb->pkt_type = PACKET_HOST;
897     
898     	netif_rx(skb);
899     }
900     
901     void x25_connected(void *token, int reason)
902     {
903     	x25_connect_disconnect(token, reason, 1);
904     }
905     
906     void x25_disconnected(void *token, int reason)
907     {
908     	x25_connect_disconnect(token, reason, 2);
909     }
910     
911     
912     int x25_data_indication(void *token, struct sk_buff *skb)
913     {
914     	hdlc_device *hdlc = token;
915     	unsigned char *ptr;
916     
917     	ptr = skb_push(skb, 1);
918     	*ptr = 0;
919     
920     	skb->dev = hdlc_to_dev(hdlc);
921     	skb->protocol = htons(ETH_P_X25);
922     	skb->mac.raw = skb->data;
923     	skb->pkt_type = PACKET_HOST;
924     
925     	return netif_rx(skb);
926     }
927     
928     
929     void x25_data_transmit(void *token, struct sk_buff *skb)
930     {
931     	hdlc_device *hdlc = token;
932     	hdlc->xmit(hdlc, skb);	/* Ignore return value :-( */
933     }
934     #endif /* CONFIG_HDLC_X25 */
935     
936     
937     /********************************************************
938      *
939      * HDLC device routines
940      *
941      *******************************************************/
942     
943     static int hdlc_open(struct net_device *dev)
944     {
945     	hdlc_device *hdlc = dev_to_hdlc(dev);
946     	int result;
947     
948     	if (hdlc->mode == MODE_NONE)
949     		return -ENOSYS;
950     
951     	memset(&(hdlc->stats), 0, sizeof(struct net_device_stats));
952     
953     	if (mode_is(hdlc, MODE_FR | MODE_SOFT) ||
954     	    mode_is(hdlc, MODE_CISCO | MODE_SOFT))
955     		fr_cisco_open(hdlc);
956     #ifdef CONFIG_HDLC_PPP
957     	else if (mode_is(hdlc, MODE_PPP | MODE_SOFT)) {
958     		sppp_attach(&hdlc->pppdev);
959     		/* sppp_attach nukes them. We don't need syncppp's ioctl */
960     		dev->do_ioctl = hdlc_ioctl;
961     		hdlc->pppdev.sppp.pp_flags &= ~PP_CISCO;
962     		dev->type = ARPHRD_PPP;
963     		result = sppp_open(dev);
964     		if (result) {
965     			sppp_detach(dev);
966     			return result;
967     		}
968     	}
969     #endif
970     #ifdef CONFIG_HDLC_X25
971     	else if (mode_is(hdlc, MODE_X25)) {
972     		struct lapb_register_struct cb;
973     
974     		cb.connect_confirmation = x25_connected;
975     		cb.connect_indication = x25_connected;
976     		cb.disconnect_confirmation = x25_disconnected;
977     		cb.disconnect_indication = x25_disconnected;
978     		cb.data_indication = x25_data_indication;
979     		cb.data_transmit = x25_data_transmit;
980     
981     		result = lapb_register(hdlc, &cb);
982     		if (result != LAPB_OK)
983     			return result;
984     	}
985     #endif
986     	result = hdlc->open(hdlc);
987     	if (result) {
988     		if (mode_is(hdlc, MODE_FR | MODE_SOFT) ||
989     		    mode_is(hdlc, MODE_CISCO | MODE_SOFT))
990     			fr_cisco_close(hdlc);
991     #ifdef CONFIG_HDLC_PPP
992     		else if (mode_is(hdlc, MODE_PPP | MODE_SOFT)) {
993     			sppp_close(dev);
994     			sppp_detach(dev);
995     			dev->rebuild_header = NULL;
996     			dev->change_mtu = hdlc_change_mtu;
997     			dev->mtu = HDLC_MAX_MTU;
998     			dev->hard_header_len = 16;
999     		}
1000     #endif
1001     #ifdef CONFIG_HDLC_X25
1002     		else if (mode_is(hdlc, MODE_X25))
1003     			lapb_unregister(hdlc);
1004     #endif
1005     	}
1006     
1007     	return result;
1008     }
1009     
1010     
1011     
1012     static int hdlc_close(struct net_device *dev)
1013     {
1014     	hdlc_device *hdlc = dev_to_hdlc(dev);
1015     
1016     	hdlc->close(hdlc);
1017     
1018     	if (mode_is(hdlc, MODE_FR | MODE_SOFT) ||
1019     	    mode_is(hdlc, MODE_CISCO | MODE_SOFT))
1020     		fr_cisco_close(hdlc);
1021     #ifdef CONFIG_HDLC_PPP
1022     	else if (mode_is(hdlc, MODE_PPP | MODE_SOFT)) {
1023     		sppp_close(dev);
1024     		sppp_detach(dev);
1025     		dev->rebuild_header = NULL;
1026     		dev->change_mtu = hdlc_change_mtu;
1027     		dev->mtu = HDLC_MAX_MTU;
1028     		dev->hard_header_len = 16;
1029     	}
1030     #endif
1031     #ifdef CONFIG_HDLC_X25
1032     	else if (mode_is(hdlc, MODE_X25))
1033     		lapb_unregister(hdlc);
1034     #endif
1035     	return 0;
1036     }
1037     
1038     
1039     
1040     static int hdlc_xmit(struct sk_buff *skb, struct net_device *dev)
1041     {
1042     	hdlc_device *hdlc = dev_to_hdlc(dev);
1043     
1044     #ifdef CONFIG_HDLC_X25
1045     	if (mode_is(hdlc, MODE_X25 | MODE_SOFT)) {
1046     		int result;
1047     
1048     
1049     		/* X.25 to LAPB */
1050     		switch (skb->data[0]) {
1051     		case 0:		/* Data to be transmitted */
1052     			skb_pull(skb, 1);
1053     			if ((result = lapb_data_request(hdlc, skb)) != LAPB_OK)
1054     				dev_kfree_skb(skb);
1055     			return 0;
1056     
1057     		case 1:
1058     			if ((result = lapb_connect_request(hdlc))!= LAPB_OK) {
1059     				if (result == LAPB_CONNECTED) {
1060     				/* Send connect confirm. msg to level 3 */
1061     					x25_connected(hdlc, 0);
1062     				} else {
1063     					printk(KERN_ERR "%s: LAPB connect "
1064     					       "request failed, error code = "
1065     					       "%i\n", hdlc_to_name(hdlc),
1066     					       result);
1067     				}
1068     			}
1069     			break;
1070     
1071     		case 2:
1072     			if ((result=lapb_disconnect_request(hdlc))!=LAPB_OK) {
1073     				if (result == LAPB_NOTCONNECTED) {
1074     				/* Send disconnect confirm. msg to level 3 */
1075     					x25_disconnected(hdlc, 0);
1076     				} else {
1077     					printk(KERN_ERR "%s: LAPB disconnect "
1078     					       "request failed, error code = "
1079     					       "%i\n", hdlc_to_name(hdlc),
1080     					       result);
1081     				}
1082     			}
1083     			break;
1084     
1085     		default:
1086     			/* to be defined */
1087     			break;
1088     		}
1089     
1090     		dev_kfree_skb(skb);
1091     		return 0;
1092     	} /* MODE_X25 */
1093     #endif /* CONFIG_HDLC_X25 */
1094     
1095     	return hdlc->xmit(hdlc, skb);
1096     }
1097     
1098     
1099     
1100     void hdlc_netif_rx(hdlc_device *hdlc, struct sk_buff *skb)
1101     {
1102     /* skb contains raw HDLC frame, in both hard- and software modes */
1103     	skb->mac.raw = skb->data;
1104     
1105     	switch(hdlc->mode & MODE_MASK) {
1106     	case MODE_HDLC:
1107     		skb->protocol = htons(ETH_P_IP);
1108     		skb->dev = hdlc_to_dev(hdlc);
1109     		netif_rx(skb);
1110     		return;
1111     
1112     	case MODE_FR:
1113     		fr_netif(hdlc, skb);
1114     		return;
1115     
1116     	case MODE_CISCO:
1117     		cisco_netif(hdlc, skb);
1118     		return;
1119     
1120     #ifdef CONFIG_HDLC_PPP
1121     	case MODE_PPP:
1122     #if 0
1123     		sppp_input(hdlc_to_dev(hdlc), skb);
1124     #else
1125     		skb->protocol = htons(ETH_P_WAN_PPP);
1126     		skb->dev = hdlc_to_dev(hdlc);
1127     		netif_rx(skb);
1128     #endif
1129     		return;
1130     #endif
1131     #ifdef CONFIG_HDLC_X25
1132     	case MODE_X25:
1133     		skb->dev = hdlc_to_dev(hdlc);
1134     		if (lapb_data_received(hdlc, skb) == LAPB_OK)
1135     			return;
1136     		break;
1137     #endif
1138     	}
1139     
1140     	hdlc->stats.rx_errors++;
1141     	dev_kfree_skb_any(skb);
1142     }
1143     
1144     
1145     
1146     static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
1147     {
1148     	return &dev_to_hdlc(dev)->stats;
1149     }
1150     
1151     
1152     
1153     static int hdlc_set_mode(hdlc_device *hdlc, int mode)
1154     {
1155     	int result = -1;	/* Default to soft modes */
1156     	struct net_device *dev = hdlc_to_dev(hdlc);
1157     
1158     	if(!capable(CAP_NET_ADMIN))
1159     		return -EPERM;
1160     
1161     	if(dev->flags & IFF_UP)
1162     		return -EBUSY;
1163     
1164     	dev->addr_len = 0;
1165     	dev->hard_header = NULL;
1166     	hdlc->mode = MODE_NONE;
1167     
1168     	if (!(mode & MODE_SOFT))
1169     		switch(mode & MODE_MASK) {
1170     		case MODE_HDLC:
1171     			result = hdlc->set_mode ?
1172     				hdlc->set_mode(hdlc, MODE_HDLC) : 0;
1173     			break;
1174     
1175     		case MODE_CISCO: /* By card */
1176     #ifdef CONFIG_HDLC_PPP
1177     		case MODE_PPP:
1178     #endif
1179     #ifdef CONFIG_HDLC_X25
1180     		case MODE_X25:
1181     #endif
1182     		case MODE_FR:
1183     			result = hdlc->set_mode ?
1184     				hdlc->set_mode(hdlc, mode) : -ENOSYS;
1185     			break;
1186     
1187     		default:
1188     			return -EINVAL;
1189     		}
1190     
1191     	if (result) {
1192     		mode |= MODE_SOFT; /* Try "host software" protocol */
1193     
1194     		switch(mode & MODE_MASK) {
1195     		case MODE_CISCO:
1196     			dev->hard_header = cisco_hard_header;
1197     			break;
1198     
1199     #ifdef CONFIG_HDLC_PPP
1200     		case MODE_PPP:
1201     			break;
1202     #endif
1203     #ifdef CONFIG_HDLC_X25
1204     		case MODE_X25:
1205     			break;
1206     #endif
1207     
1208     		case MODE_FR:
1209     			dev->hard_header = fr_hard_header;
1210     			dev->addr_len = 2;
1211     			*(u16*)dev->dev_addr = htons(LMI_DLCI);
1212     			dlci_to_q922(dev->broadcast, LMI_DLCI);
1213     			break;
1214     
1215     		default:
1216     			return -EINVAL;
1217     		}
1218     
1219     		result = hdlc->set_mode ?
1220     			hdlc->set_mode(hdlc, MODE_HDLC) : 0;
1221     	}
1222     
1223     	if (result)
1224     		return result;
1225     
1226     	hdlc->mode = mode;
1227     	switch(mode & MODE_MASK) {
1228     #ifdef CONFIG_HDLC_PPP
1229     	case MODE_PPP:   dev->type = ARPHRD_PPP;   break;
1230     #endif
1231     #ifdef CONFIG_HDLC_X25
1232     	case MODE_X25:   dev->type = ARPHRD_X25;   break;
1233     #endif
1234     	case MODE_FR:    dev->type = ARPHRD_FRAD;  break;
1235     	case MODE_CISCO: dev->type = ARPHRD_CISCO; break;
1236     	default:         dev->type = ARPHRD_RAWHDLC;
1237     	}
1238     
1239     	memset(&(hdlc->stats), 0, sizeof(struct net_device_stats));
1240     	destroy_pvc_list(hdlc);
1241     	return 0;
1242     }
1243     
1244     
1245     
1246     static int hdlc_fr_pvc(hdlc_device *hdlc, int dlci)
1247     {
1248     	pvc_device **pvc_p = &hdlc->first_pvc;
1249     	pvc_device *pvc;
1250     	int result, create = 1;	/* Create or delete PVC */
1251     
1252     	if(!capable(CAP_NET_ADMIN))
1253     		return -EPERM;
1254     
1255     	if(dlci<0) {
1256     		dlci = -dlci;
1257     		create = 0;
1258     	}
1259     
1260     	if(dlci <= 0 || dlci >= 1024)
1261     		return -EINVAL;	/* Only 10 bits for DLCI, DLCI=0 is reserved */
1262     
1263     	if(!mode_is(hdlc, MODE_FR))
1264     		return -EINVAL;	/* Only meaningfull on FR */
1265     
1266     	while(*pvc_p) {
1267     		if (netdev_dlci(&(*pvc_p)->netdev) == dlci)
1268     			break;
1269     		pvc_p = &(*pvc_p)->next;
1270     	}
1271     
1272     	if (create) {		/* Create PVC */
1273     		if (*pvc_p != NULL)
1274     			return -EEXIST;
1275     
1276     		pvc = *pvc_p = kmalloc(sizeof(pvc_device), GFP_KERNEL);
1277     		if (!pvc) {
1278     			printk(KERN_WARNING "%s: Memory squeeze on "
1279     			       "hdlc_fr_pvc()\n", hdlc_to_name(hdlc));
1280     			return -ENOBUFS;
1281     		}
1282     		memset(pvc, 0, sizeof(pvc_device));
1283     
1284     		pvc->netdev.hard_start_xmit = pvc_xmit;
1285     		pvc->netdev.get_stats = pvc_get_stats;
1286     		pvc->netdev.open = pvc_open;
1287     		pvc->netdev.stop = pvc_close;
1288     		pvc->netdev.change_mtu = pvc_change_mtu;
1289     		pvc->netdev.mtu = HDLC_MAX_MTU;
1290     
1291     		pvc->netdev.type = ARPHRD_DLCI;
1292     		pvc->netdev.hard_header_len = 16;
1293     		pvc->netdev.hard_header = fr_hard_header;
1294     		pvc->netdev.tx_queue_len = 0;
1295     		pvc->netdev.flags = IFF_POINTOPOINT;
1296     
1297     		pvc->master = hdlc;
1298     		*(u16*)pvc->netdev.dev_addr = htons(dlci);
1299     		dlci_to_q922(pvc->netdev.broadcast, dlci);
1300     		pvc->netdev.addr_len = 2;
1301     		pvc->netdev.irq = hdlc_to_dev(hdlc)->irq;
1302     
1303     		result = dev_alloc_name(&pvc->netdev, "pvc%d");
1304     		if (result < 0) {
1305     			kfree(pvc);
1306     			*pvc_p = NULL;
1307     			return result;
1308     		}
1309     
1310     		if (register_netdevice(&pvc->netdev) != 0) {
1311     			kfree(pvc);
1312     			*pvc_p = NULL;
1313     			return -EIO;
1314     		}
1315     
1316     		if (!mode_is(hdlc, MODE_SOFT) && hdlc->create_pvc) {
1317     			result = hdlc->create_pvc(pvc);
1318     			if (result) {
1319     				unregister_netdevice(&pvc->netdev);
1320     				kfree(pvc);
1321     				*pvc_p = NULL;
1322     				return result;
1323     			}
1324     		}
1325     
1326     		hdlc->lmi.state |= LINK_STATE_CHANGED;
1327     		hdlc->pvc_count++;
1328     		return 0;
1329     	}
1330     
1331     	if (*pvc_p == NULL)		/* Delete PVC */
1332     		return -ENOENT;
1333     
1334     	pvc = *pvc_p;
1335     
1336     	if (pvc->netdev.flags & IFF_UP)
1337     		return -EBUSY;		/* PVC in use */
1338     
1339     	if (!mode_is(hdlc, MODE_SOFT) && hdlc->destroy_pvc)
1340     		hdlc->destroy_pvc(pvc);
1341     
1342     	hdlc->lmi.state |= LINK_STATE_CHANGED;
1343     	hdlc->pvc_count--;
1344     	*pvc_p = pvc->next;
1345     	unregister_netdevice(&pvc->netdev);
1346     	kfree(pvc);
1347     	return 0;
1348     }
1349     
1350     
1351     
1352     static int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1353     {
1354     	hdlc_device *hdlc = dev_to_hdlc(dev);
1355     
1356     	switch(cmd) {
1357     	case HDLCGMODE:
1358     		ifr->ifr_ifru.ifru_ivalue = hdlc->mode;
1359     		return 0;
1360     
1361     	case HDLCSMODE:
1362     		return hdlc_set_mode(hdlc, ifr->ifr_ifru.ifru_ivalue);
1363     
1364     	case HDLCPVC:
1365     		return hdlc_fr_pvc(hdlc, ifr->ifr_ifru.ifru_ivalue);
1366     
1367     	default:
1368     		if (hdlc->ioctl != NULL)
1369     			return hdlc->ioctl(hdlc, ifr, cmd);
1370     	}
1371     
1372     	return -EINVAL;
1373     }
1374     
1375     
1376     
1377     static int hdlc_init(struct net_device *dev)
1378     {
1379     	hdlc_device *hdlc = dev_to_hdlc(dev);
1380     
1381     	memset(&(hdlc->stats), 0, sizeof(struct net_device_stats));
1382     
1383     	dev->get_stats = hdlc_get_stats;
1384     	dev->open = hdlc_open;
1385     	dev->stop = hdlc_close;
1386     	dev->hard_start_xmit = hdlc_xmit;
1387     	dev->do_ioctl = hdlc_ioctl;
1388     	dev->change_mtu = hdlc_change_mtu;
1389     	dev->mtu = HDLC_MAX_MTU;
1390     
1391     	dev->type = ARPHRD_RAWHDLC;
1392     	dev->hard_header_len = 16;
1393     
1394     	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1395     
1396     	return 0;
1397     }
1398     
1399     
1400     
1401     int register_hdlc_device(hdlc_device *hdlc)
1402     {
1403     	int result;
1404     	struct net_device *dev = hdlc_to_dev(hdlc);
1405     
1406     	dev->init = hdlc_init;
1407     	dev->priv = &hdlc->syncppp_ptr;
1408     	hdlc->syncppp_ptr = &hdlc->pppdev;
1409     	hdlc->pppdev.dev = dev;
1410     	hdlc->mode = MODE_NONE;
1411     	hdlc->lmi.T391 = 10;	/* polling verification timer */
1412     	hdlc->lmi.T392 = 15;	/* link integrity verification polling timer */
1413     	hdlc->lmi.N391 = 6;	/* full status polling counter */
1414     	hdlc->lmi.N392 = 3;	/* error threshold */
1415     	hdlc->lmi.N393 = 4;	/* monitored events count */
1416     
1417     	result = dev_alloc_name(dev, "hdlc%d");
1418     	if (result<0)
1419     		return result;
1420     
1421     	result = register_netdev(dev);
1422     	if (result != 0)
1423     		return -EIO;
1424     
1425     	MOD_INC_USE_COUNT;
1426     	return 0;
1427     }
1428     
1429     
1430     
1431     void unregister_hdlc_device(hdlc_device *hdlc)
1432     {
1433     	destroy_pvc_list(hdlc);
1434     	unregister_netdev(hdlc_to_dev(hdlc));
1435     	MOD_DEC_USE_COUNT;
1436     }
1437     
1438     MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1439     MODULE_DESCRIPTION("HDLC support module");
1440     MODULE_LICENSE("GPL");
1441     
1442     EXPORT_SYMBOL(hdlc_netif_rx);
1443     EXPORT_SYMBOL(register_hdlc_device);
1444     EXPORT_SYMBOL(unregister_hdlc_device);
1445     
1446     static int __init hdlc_module_init(void)
1447     {
1448     	printk(KERN_INFO "%s\n", version);
1449     	return 0;
1450     }
1451     
1452     
1453     module_init(hdlc_module_init);
1454