⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hdlc_fr.c

📁 linux-2.4.29操作系统的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
{	int stat_len;	pvc_device *pvc;	int reptype = -1, error, no_ram;	u8 rxseq, txseq;	int i;	if (skb->len < ((hdlc->state.fr.settings.lmi == LMI_ANSI)			? LMI_ANSI_LENGTH : LMI_LENGTH)) {		printk(KERN_INFO "%s: Short LMI frame\n", hdlc_to_name(hdlc));		return 1;	}	if (skb->data[5] != (!hdlc->state.fr.settings.dce ?			     LMI_STATUS : LMI_STATUS_ENQUIRY)) {		printk(KERN_INFO "%s: LMI msgtype=%x, Not LMI status %s\n",		       hdlc_to_name(hdlc), skb->data[2],		       hdlc->state.fr.settings.dce ? "enquiry" : "reply");		return 1;	}	i = (hdlc->state.fr.settings.lmi == LMI_ANSI) ? 7 : 6;	if (skb->data[i] !=	    ((hdlc->state.fr.settings.lmi == LMI_CCITT)	     ? LMI_CCITT_REPTYPE : LMI_REPTYPE)) {		printk(KERN_INFO "%s: Not a report type=%x\n",		       hdlc_to_name(hdlc), skb->data[i]);		return 1;	}	i++;	i++;				/* Skip length field */	reptype = skb->data[i++];	if (skb->data[i]!=	    ((hdlc->state.fr.settings.lmi == LMI_CCITT)	     ? LMI_CCITT_ALIVE : LMI_ALIVE)) {		printk(KERN_INFO "%s: Unsupported status element=%x\n",		       hdlc_to_name(hdlc), skb->data[i]);		return 1;	}	i++;	i++;			/* Skip length field */	hdlc->state.fr.rxseq = skb->data[i++]; /* TX sequence from peer */	rxseq = skb->data[i++];	/* Should confirm our sequence */	txseq = hdlc->state.fr.txseq;	if (hdlc->state.fr.settings.dce) {		if (reptype != LMI_FULLREP && reptype != LMI_INTEGRITY) {			printk(KERN_INFO "%s: Unsupported report type=%x\n",			       hdlc_to_name(hdlc), reptype);			return 1;		}	}	error = 0;	if (!hdlc->state.fr.reliable)		error = 1;	if (rxseq == 0 || rxseq != txseq) {		hdlc->state.fr.n391cnt = 0; /* Ask for full report next time */		error = 1;	}	if (hdlc->state.fr.settings.dce) {		if (hdlc->state.fr.fullrep_sent && !error) {/* Stop sending full report - the last one has been confirmed by DTE */			hdlc->state.fr.fullrep_sent = 0;			pvc = hdlc->state.fr.first_pvc;			while (pvc) {				if (pvc->state.new) {					pvc->state.new = 0;/* Tell DTE that new PVC is now active */					hdlc->state.fr.dce_changed = 1;				}				pvc = pvc->next;			}		}		if (hdlc->state.fr.dce_changed) {			reptype = LMI_FULLREP;			hdlc->state.fr.fullrep_sent = 1;			hdlc->state.fr.dce_changed = 0;		}		fr_lmi_send(hdlc, reptype == LMI_FULLREP ? 1 : 0);		return 0;	}	/* DTE */	if (reptype != LMI_FULLREP || error)		return 0;	stat_len = 3;	pvc = hdlc->state.fr.first_pvc;	while (pvc) {		pvc->state.deleted = 1;		pvc = pvc->next;	}	no_ram = 0;	while (skb->len >= i + 2 + stat_len) {		u16 dlci;		unsigned int active, new;		if (skb->data[i] != ((hdlc->state.fr.settings.lmi == LMI_CCITT)				     ? LMI_CCITT_PVCSTAT : LMI_PVCSTAT)) {			printk(KERN_WARNING "%s: Invalid PVCSTAT ID: %x\n",			       hdlc_to_name(hdlc), skb->data[i]);			return 1;		}		i++;		if (skb->data[i] != stat_len) {			printk(KERN_WARNING "%s: Invalid PVCSTAT length: %x\n",			       hdlc_to_name(hdlc), skb->data[i]);			return 1;		}		i++;		dlci = status_to_dlci(skb->data + i, &active, &new);		pvc = add_pvc(hdlc, dlci);		if (!pvc && !no_ram) {			printk(KERN_WARNING			       "%s: Memory squeeze on fr_lmi_recv()\n",			       hdlc_to_name(hdlc));			no_ram = 1;		}		if (pvc) {			pvc->state.exist = 1;			pvc->state.deleted = 0;			if (active != pvc->state.active ||			    new != pvc->state.new) {				pvc->state.new = new;				pvc->state.active = active;				fr_log_dlci_active(pvc);			}		}		i += stat_len;	}	pvc = hdlc->state.fr.first_pvc;	while (pvc) {		if (pvc->state.deleted && pvc->state.exist) {			pvc->state.active = pvc->state.new = 0;			pvc->state.exist = 0;			fr_log_dlci_active(pvc);		}		pvc = pvc->next;	}	/* Next full report after N391 polls */	hdlc->state.fr.n391cnt = hdlc->state.fr.settings.n391;	return 0;}static void fr_rx(struct sk_buff *skb){	hdlc_device *hdlc = dev_to_hdlc(skb->dev);	fr_hdr *fh = (fr_hdr*)skb->data;	u8 *data = skb->data;	u16 dlci;	pvc_device *pvc;	struct net_device *dev = NULL;	if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)		goto rx_error;	dlci = q922_to_dlci(skb->data);	if (dlci == LMI_DLCI) {		if (hdlc->state.fr.settings.lmi == LMI_NONE)			goto rx_error; /* LMI packet with no LMI? */		if (data[3] == LMI_PROTO) {			if (fr_lmi_recv(hdlc, skb))				goto rx_error;			else {				/* No request pending */				hdlc->state.fr.request = 0;				hdlc->state.fr.last_poll = jiffies;				dev_kfree_skb_any(skb);				return;			}		}		printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",		       hdlc_to_name(hdlc));		goto rx_error;	}	pvc = find_pvc(hdlc, dlci);	if (!pvc) {#ifdef DEBUG_PKT		printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",		       hdlc_to_name(hdlc), dlci);#endif		dev_kfree_skb_any(skb);		return;	}	if (pvc->state.fecn != fh->fecn) {#ifdef DEBUG_ECN		printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", hdlc_to_name(pvc),		       dlci, fh->fecn ? "N" : "FF");#endif		pvc->state.fecn ^= 1;	}	if (pvc->state.becn != fh->becn) {#ifdef DEBUG_ECN		printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", hdlc_to_name(pvc),		       dlci, fh->becn ? "N" : "FF");#endif		pvc->state.becn ^= 1;	}	if (data[3] == NLPID_IP) {		skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */		dev = pvc->main;		skb->protocol = htons(ETH_P_IP);	} else if (data[3] == NLPID_IPV6) {		skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */		dev = pvc->main;		skb->protocol = htons(ETH_P_IPV6);	} else if (skb->len > 10 && data[3] == FR_PAD &&		   data[4] == NLPID_SNAP && data[5] == FR_PAD) {		u16 oui = ntohs(*(u16*)(data + 6));		u16 pid = ntohs(*(u16*)(data + 8));		skb_pull(skb, 10);		switch ((((u32)oui) << 16) | pid) {		case ETH_P_ARP: /* routed frame with SNAP */		case ETH_P_IPX:		case ETH_P_IP:	/* a long variant */		case ETH_P_IPV6:			dev = pvc->main;			skb->protocol = htons(pid);			break;		case 0x80C20007: /* bridged Ethernet frame */			if ((dev = pvc->ether) != NULL)				skb->protocol = eth_type_trans(skb, dev);			break;		default:			printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "			       "PID=%x\n", hdlc_to_name(hdlc), oui, pid);			dev_kfree_skb_any(skb);			return;		}	} else {		printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "		       "length = %i\n", hdlc_to_name(hdlc), data[3], skb->len);		dev_kfree_skb_any(skb);		return;	}	if (dev) {		struct net_device_stats *stats = pvc_get_stats(dev);		stats->rx_packets++; /* PVC traffic */		stats->rx_bytes += skb->len;		if (pvc->state.becn)			stats->rx_compressed++;		skb->dev = dev;		netif_rx(skb);	} else		dev_kfree_skb_any(skb);	return; rx_error:	hdlc->stats.rx_errors++; /* Mark error */	dev_kfree_skb_any(skb);}static int fr_open(hdlc_device *hdlc){	if (hdlc->state.fr.settings.lmi != LMI_NONE) {		hdlc->state.fr.last_poll = 0;		hdlc->state.fr.reliable = 0;		hdlc->state.fr.dce_changed = 1;		hdlc->state.fr.request = 0;		hdlc->state.fr.fullrep_sent = 0;		hdlc->state.fr.last_errors = 0xFFFFFFFF;		hdlc->state.fr.n391cnt = 0;		hdlc->state.fr.txseq = hdlc->state.fr.rxseq = 0;		init_timer(&hdlc->state.fr.timer);		/* First poll after 1 s */		hdlc->state.fr.timer.expires = jiffies + HZ;		hdlc->state.fr.timer.function = fr_timer;		hdlc->state.fr.timer.data = (unsigned long)hdlc;		add_timer(&hdlc->state.fr.timer);	} else		hdlc->state.fr.reliable = 1;	return 0;}static void fr_close(hdlc_device *hdlc){	pvc_device *pvc = hdlc->state.fr.first_pvc;	if (hdlc->state.fr.settings.lmi != LMI_NONE)		del_timer_sync(&hdlc->state.fr.timer);	while (pvc) {		/* Shutdown all PVCs for this FRAD */		if (pvc->main)			dev_close(pvc->main);		if (pvc->ether)			dev_close(pvc->ether);		pvc->state.active = pvc->state.new = pvc->state.fecn =			pvc->state.becn = 0;		pvc->state.exist = 0;		pvc = pvc->next;	}}static int fr_add_pvc(hdlc_device *hdlc, unsigned int dlci, int type){	pvc_device *pvc = NULL;	struct net_device *dev;	int result, used;	char * prefix = "pvc%d";	if (type == ARPHRD_ETHER)		prefix = "pvceth%d";	if ((pvc = add_pvc(hdlc, dlci)) == NULL) {		printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",		       hdlc_to_name(hdlc));		return -ENOBUFS;	}	if (*get_dev_p(pvc, type))		return -EEXIST;	used = pvc_is_used(pvc);	dev = kmalloc(sizeof(struct net_device) +		      sizeof(struct net_device_stats), GFP_KERNEL);	if (!dev) {		printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",		       hdlc_to_name(hdlc));		delete_unused_pvcs(hdlc);		return -ENOBUFS;	}	memset(dev, 0, sizeof(struct net_device) +	       sizeof(struct net_device_stats));	if (type == ARPHRD_ETHER) {		ether_setup(dev);		memcpy(dev->dev_addr, "\x00\x01", 2);                get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);	} else {		dev->type = ARPHRD_DLCI;		dev->flags = IFF_POINTOPOINT;		dev->hard_header_len = 10;		dev->addr_len = 2;		*(u16*)dev->dev_addr = htons(dlci);		dlci_to_q922(dev->broadcast, dlci);	}	dev->hard_start_xmit = pvc_xmit;	dev->get_stats = pvc_get_stats;	dev->open = pvc_open;	dev->stop = pvc_close;	dev->do_ioctl = pvc_ioctl;	dev->change_mtu = pvc_change_mtu;	dev->mtu = HDLC_MAX_MTU;	dev->tx_queue_len = 0;	dev->priv = pvc;	result = dev_alloc_name(dev, prefix);	if (result < 0) {		kfree(dev);		delete_unused_pvcs(hdlc);		return result;	}	if (register_netdevice(dev) != 0) {		kfree(dev);		delete_unused_pvcs(hdlc);		return -EIO;	}	*get_dev_p(pvc, type) = dev;	if (!used) {		hdlc->state.fr.dce_changed = 1;		hdlc->state.fr.dce_pvc_count++;	}	return 0;}static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type){	pvc_device *pvc;	struct net_device *dev;	if ((pvc = find_pvc(hdlc, dlci)) == NULL)		return -ENOENT;	if ((dev = *get_dev_p(pvc, type)) == NULL)		return -ENOENT;	if (dev->flags & IFF_UP)		return -EBUSY;		/* PVC in use */	unregister_netdevice(dev);	kfree(dev);	*get_dev_p(pvc, type) = NULL;	if (!pvc_is_used(pvc)) {		hdlc->state.fr.dce_pvc_count--;		hdlc->state.fr.dce_changed = 1;	}	delete_unused_pvcs(hdlc);	return 0;}static void fr_destroy(hdlc_device *hdlc){	pvc_device *pvc = hdlc->state.fr.first_pvc;	while (pvc) {		pvc_device *next = pvc->next;		if (pvc->main) {			unregister_netdevice(pvc->main);			kfree(pvc->main);		}		if (pvc->ether) {			unregister_netdevice(pvc->ether);			kfree(pvc->ether);		}		kfree(pvc);		pvc = next;	}	hdlc->state.fr.first_pvc = NULL; /* All PVCs destroyed */	hdlc->state.fr.dce_pvc_count = 0;	hdlc->state.fr.dce_changed = 1;}int hdlc_fr_ioctl(hdlc_device *hdlc, struct ifreq *ifr){	fr_proto *fr_s = ifr->ifr_settings.ifs_ifsu.fr;	const size_t size = sizeof(fr_proto);	fr_proto new_settings;	struct net_device *dev = hdlc_to_dev(hdlc);	fr_proto_pvc pvc;	int result;	switch (ifr->ifr_settings.type) {	case IF_GET_PROTO:		ifr->ifr_settings.type = IF_PROTO_FR;		if (ifr->ifr_settings.size < size) {			ifr->ifr_settings.size = size; /* data size wanted */			return -ENOBUFS;		}		if (copy_to_user(fr_s, &hdlc->state.fr.settings, size))			return -EFAULT;		return 0;	case IF_PROTO_FR:		if(!capable(CAP_NET_ADMIN))			return -EPERM;		if(dev->flags & IFF_UP)			return -EBUSY;		if (copy_from_user(&new_settings, fr_s, size))			return -EFAULT;		if (new_settings.lmi == LMI_DEFAULT)			new_settings.lmi = LMI_ANSI;		if ((new_settings.lmi != LMI_NONE &&		     new_settings.lmi != LMI_ANSI &&		     new_settings.lmi != LMI_CCITT) ||		    new_settings.t391 < 1 ||		    new_settings.t392 < 2 ||		    new_settings.n391 < 1 ||		    new_settings.n392 < 1 ||		    new_settings.n393 < new_settings.n392 ||		    new_settings.n393 > 32 ||		    (new_settings.dce != 0 &&		     new_settings.dce != 1))			return -EINVAL;		result=hdlc->attach(hdlc, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);		if (result)			return result;		if (hdlc->proto != IF_PROTO_FR) {			hdlc_proto_detach(hdlc);			hdlc->state.fr.first_pvc = NULL;			hdlc->state.fr.dce_pvc_count = 0;		}		memcpy(&hdlc->state.fr.settings, &new_settings, size);		hdlc->open = fr_open;		hdlc->stop = fr_close;		hdlc->netif_rx = fr_rx;		hdlc->type_trans = NULL;		hdlc->proto_detach = fr_destroy;		hdlc->proto = IF_PROTO_FR;		dev->hard_start_xmit = hdlc->xmit;		dev->hard_header = NULL;		dev->type = ARPHRD_FRAD;		dev->flags = IFF_POINTOPOINT | IFF_NOARP;		dev->addr_len = 0;		return 0;	case IF_PROTO_FR_ADD_PVC:	case IF_PROTO_FR_DEL_PVC:	case IF_PROTO_FR_ADD_ETH_PVC:	case IF_PROTO_FR_DEL_ETH_PVC:		if(!capable(CAP_NET_ADMIN))			return -EPERM;		if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,				   sizeof(fr_proto_pvc)))			return -EFAULT;		if (pvc.dlci <= 0 || pvc.dlci >= 1024)			return -EINVAL;	/* Only 10 bits, DLCI 0 reserved */		if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||		    ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)			result = ARPHRD_ETHER; /* bridged Ethernet device */		else			result = ARPHRD_DLCI;		if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||		    ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)			return fr_add_pvc(hdlc, pvc.dlci, result);		else			return fr_del_pvc(hdlc, pvc.dlci, result);	}	return -EINVAL;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -