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

📄 af_ipx.c

📁 嵌入式系统设计与实例开发实验教材二源码 多线程应用程序设计 串行端口程序设计 AD接口实验 CAN总线通信实验 GPS通信实验 Linux内核移植与编译实验 IC卡读写实验 SD驱动使
💻 C
📖 第 1 页 / 共 5 页
字号:
				break;		}		s = s->next;	}	/* skb was solely for us, and we did not make a copy, so free it. */	if (!copy)		kfree_skb(skb);	ret = 0;out:	spin_unlock_bh(&intrfc->if_sklist_lock);	return ret;}#elsestatic struct sock *ncp_connection_hack(ipx_interface *intrfc,					struct ipxhdr *ipx){	/* The packet's target is a NCP connection handler. We want to hand it	 * to the correct socket directly within the kernel, so that the	 * mars_nwe packet distribution process does not have to do it. Here we	 * only care about NCP and BURST packets.	 *	 * You might call this a hack, but believe me, you do not want a	 * complete NCP layer in the kernel, and this is VERY fast as well. */	struct sock *sk = NULL; 	int connection = 0;	u8 *ncphdr = (u8 *)(ipx + 1); 	if (*ncphdr == 0x22 && *(ncphdr + 1) == 0x22) /* NCP request */		connection = (((int) *(ncphdr + 5)) << 8) | (int) *(ncphdr + 3);	else if (*ncphdr == 0x77 && *(ncphdr + 1) == 0x77) /* BURST packet */		connection = (((int) *(ncphdr + 9)) << 8) | (int) *(ncphdr + 8);	if (connection) {		/* Now we have to look for a special NCP connection handling		 * socket. Only these sockets have ipx_ncp_conn != 0, set by		 * SIOCIPXNCPCONN. */		spin_lock_bh(&intrfc->if_sklist_lock);		for (sk = intrfc->if_sklist;		     sk && sk->protinfo.af_ipx.ipx_ncp_conn != connection;		     sk = sk->next);		if (sk)			sock_hold(sk);		spin_unlock_bh(&intrfc->if_sklist_lock);	}	return sk;}static int ipxitf_demux_socket(ipx_interface *intrfc, struct sk_buff *skb,				int copy){	struct ipxhdr *ipx = skb->nh.ipxh;	struct sock *sock1 = NULL, *sock2 = NULL;	struct sk_buff *skb1 = NULL, *skb2 = NULL;	int ret;	if (intrfc == ipx_primary_net && ntohs(ipx->ipx_dest.sock) == 0x451)		sock1 = ncp_connection_hack(intrfc, ipx);        if (!sock1)		/* No special socket found, forward the packet the normal way */		sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);	/*	 * We need to check if there is a primary net and if	 * this is addressed to one of the *SPECIAL* sockets because	 * these need to be propagated to the primary net.	 * The *SPECIAL* socket list contains: 0x452(SAP), 0x453(RIP) and	 * 0x456(Diagnostic).	 */	if (ipx_primary_net && intrfc != ipx_primary_net) {		const int dsock = ntohs(ipx->ipx_dest.sock);		if (dsock == 0x452 || dsock == 0x453 || dsock == 0x456)			/* The appropriate thing to do here is to dup the			 * packet and route to the primary net interface via			 * ipxitf_send; however, we'll cheat and just demux it			 * here. */			sock2 = ipxitf_find_socket(ipx_primary_net,							ipx->ipx_dest.sock);	}	/*	 * If there is nothing to do return. The kfree will cancel any charging.	 */	if (!sock1 && !sock2) {		if (!copy)			kfree_skb(skb);		return 0;	}	/*	 * This next segment of code is a little awkward, but it sets it up	 * so that the appropriate number of copies of the SKB are made and	 * that skb1 and skb2 point to it (them) so that it (they) can be	 * demuxed to sock1 and/or sock2.  If we are unable to make enough	 * copies, we do as much as is possible.	 */	if (copy)		skb1 = skb_clone(skb, GFP_ATOMIC);	else		skb1 = skb;	ret = -ENOMEM;	if (!skb1)		goto out;	/* Do we need 2 SKBs? */	if (sock1 && sock2)		skb2 = skb_clone(skb1, GFP_ATOMIC);	else		skb2 = skb1;	if (sock1)		ipxitf_def_skb_handler(sock1, skb1);	ret = -ENOMEM;	if (!skb2)		goto out;	if (sock2)		ipxitf_def_skb_handler(sock2, skb2);	ret = 0;out:	if (sock1)		sock_put(sock1);	if (sock2)		sock_put(sock2);	return ret;}#endif	/* CONFIG_IPX_INTERN */static struct sk_buff *ipxitf_adjust_skbuff(ipx_interface *intrfc,					    struct sk_buff *skb){	struct sk_buff *skb2;	int in_offset = skb->h.raw - skb->head;	int out_offset = intrfc->if_ipx_offset;	int len;	/* Hopefully, most cases */	if (in_offset >= out_offset)		return skb;	/* Need new SKB */	len  = skb->len + out_offset;	skb2 = alloc_skb(len, GFP_ATOMIC);	if (skb2) {		skb_reserve(skb2, out_offset);		skb2->nh.raw = skb2->h.raw = skb_put(skb2, skb->len);		memcpy(skb2->h.raw, skb->h.raw, skb->len);		memcpy(skb2->cb, skb->cb, sizeof(skb->cb));	}	kfree_skb(skb);	return skb2;}/* caller must hold a reference to intrfc and the skb has to be unshared */static int ipxitf_send(ipx_interface *intrfc, struct sk_buff *skb, char *node){	struct ipxhdr *ipx = skb->nh.ipxh;	struct net_device *dev = intrfc->if_dev;	struct datalink_proto *dl = intrfc->if_dlink;	char dest_node[IPX_NODE_LEN];	int send_to_wire = 1;	int addr_len;	ipx->ipx_tctrl = IPX_SKB_CB(skb)->ipx_tctrl;	ipx->ipx_dest.net = IPX_SKB_CB(skb)->ipx_dest_net;	ipx->ipx_source.net = IPX_SKB_CB(skb)->ipx_source_net;	/* see if we need to include the netnum in the route list */	if (IPX_SKB_CB(skb)->last_hop.index >= 0) {		u32 *last_hop = (u32 *)(((u8 *) skb->data) +				sizeof(struct ipxhdr) +				IPX_SKB_CB(skb)->last_hop.index *				sizeof(u32));		*last_hop = IPX_SKB_CB(skb)->last_hop.netnum;		IPX_SKB_CB(skb)->last_hop.index = -1;	}		/* 	 * We need to know how many skbuffs it will take to send out this	 * packet to avoid unnecessary copies.	 */	 	if (!dl || !dev || dev->flags & IFF_LOOPBACK) 		send_to_wire = 0;	/* No non looped */	/*	 * See if this should be demuxed to sockets on this interface 	 *	 * We want to ensure the original was eaten or that we only use	 * up clones.	 */	 	if (ipx->ipx_dest.net == intrfc->if_netnum) {		/*		 * To our own node, loop and free the original.		 * The internal net will receive on all node address.		 */		if (intrfc == ipx_internal_net ||		    !memcmp(intrfc->if_node, node, IPX_NODE_LEN)) {			/* Don't charge sender */			skb_orphan(skb);			/* Will charge receiver */			return ipxitf_demux_socket(intrfc, skb, 0);		}		/* Broadcast, loop and possibly keep to send on. */		if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN)) {			if (!send_to_wire)				skb_orphan(skb);			ipxitf_demux_socket(intrfc, skb, send_to_wire);			if (!send_to_wire)				goto out;		}	}	/*	 * If the originating net is not equal to our net; this is routed	 * We are still charging the sender. Which is right - the driver	 * free will handle this fairly.	 */	if (ipx->ipx_source.net != intrfc->if_netnum) {		/*		 * Unshare the buffer before modifying the count in		 * case its a flood or tcpdump		 */		skb = skb_unshare(skb, GFP_ATOMIC);		if (!skb)			goto out;		if (++ipx->ipx_tctrl > ipxcfg_max_hops)			send_to_wire = 0;	}	if (!send_to_wire) {		kfree_skb(skb);		goto out;	}	/* Determine the appropriate hardware address */	addr_len = dev->addr_len;	if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN))		memcpy(dest_node, dev->broadcast, addr_len);	else		memcpy(dest_node, &(node[IPX_NODE_LEN-addr_len]), addr_len);	/* Make any compensation for differing physical/data link size */	skb = ipxitf_adjust_skbuff(intrfc, skb);	if (!skb)		goto out;	/* set up data link and physical headers */	skb->dev	= dev;	skb->protocol	= __constant_htons(ETH_P_IPX);	dl->datalink_header(dl, skb, dest_node);	/* Send it out */	dev_queue_xmit(skb);out:	return 0;}static int ipxrtr_add_route(__u32, ipx_interface *, unsigned char *);static int ipxitf_add_local_route(ipx_interface *intrfc){	return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);}static const char * ipx_frame_name(unsigned short);static const char * ipx_device_name(ipx_interface *);static void ipxitf_discover_netnum(ipx_interface *intrfc, struct sk_buff *skb);static int ipxitf_pprop(ipx_interface *intrfc, struct sk_buff *skb);static int ipxitf_rcv(ipx_interface *intrfc, struct sk_buff *skb){	struct ipxhdr	*ipx = skb->nh.ipxh;	int ret = 0;	ipxitf_hold(intrfc);	/* See if we should update our network number */	if (!intrfc->if_netnum) /* net number of intrfc not known yet */ 		ipxitf_discover_netnum(intrfc, skb);		IPX_SKB_CB(skb)->last_hop.index = -1;	if (ipx->ipx_type == IPX_TYPE_PPROP) {		ret = ipxitf_pprop(intrfc, skb);		if (ret)			goto out_free_skb;	}	/* local processing follows */	if (!IPX_SKB_CB(skb)->ipx_dest_net)		IPX_SKB_CB(skb)->ipx_dest_net = intrfc->if_netnum;	if (!IPX_SKB_CB(skb)->ipx_source_net)		IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;	/* it doesn't make sense to route a pprop packet, there's no meaning	 * in the ipx_dest_net for such packets */	if (ipx->ipx_type != IPX_TYPE_PPROP &&	    intrfc->if_netnum != IPX_SKB_CB(skb)->ipx_dest_net) {		/* We only route point-to-point packets. */		if (skb->pkt_type == PACKET_HOST) {			skb = skb_unshare(skb, GFP_ATOMIC);			if (skb)				ret = ipxrtr_route_skb(skb);			goto out_intrfc;		}		goto out_free_skb;	}	/* see if we should keep it */	if (!memcmp(ipx_broadcast_node, ipx->ipx_dest.node, IPX_NODE_LEN) ||	    !memcmp(intrfc->if_node, ipx->ipx_dest.node, IPX_NODE_LEN)) {		ret = ipxitf_demux_socket(intrfc, skb, 0);		goto out_intrfc;	}	/* we couldn't pawn it off so unload it */out_free_skb:	kfree_skb(skb);out_intrfc:	ipxitf_put(intrfc);	return ret;}static void ipxitf_discover_netnum(ipx_interface *intrfc, struct sk_buff *skb){ 	const struct ipx_cb *cb = IPX_SKB_CB(skb);	/* see if this is an intra packet: source_net == dest_net */	if (cb->ipx_source_net == cb->ipx_dest_net && cb->ipx_source_net) {		ipx_interface *i = ipxitf_find_using_net(cb->ipx_source_net);		/* NB: NetWare servers lie about their hop count so we		 * dropped the test based on it. This is the best way		 * to determine this is a 0 hop count packet. */		if (!i) {			intrfc->if_netnum = cb->ipx_source_net;			ipxitf_add_local_route(intrfc);		} else {			printk(KERN_WARNING "IPX: Network number collision "				"%lx\n        %s %s and %s %s\n",				(unsigned long) htonl(cb->ipx_source_net),				ipx_device_name(i),				ipx_frame_name(i->if_dlink_type),				ipx_device_name(intrfc),				ipx_frame_name(intrfc->if_dlink_type));			ipxitf_put(i);		}	}}/** * ipxitf_pprop - Process packet propagation IPX packet type 0x14, used for * 		  NetBIOS broadcasts * @intrfc: IPX interface receiving this packet * @skb: Received packet * * Checks if packet is valid: if its more than %IPX_MAX_PPROP_HOPS hops or if it * is smaller than a IPX header + the room for %IPX_MAX_PPROP_HOPS hops we drop * it, not even processing it locally, if it has exact %IPX_MAX_PPROP_HOPS we * don't broadcast it, but process it locally. See chapter 5 of Novell's "IPX * RIP and SAP Router Specification", Part Number 107-000029-001. *  * If it is valid, check if we have pprop broadcasting enabled by the user, * if not, just return zero for local processing. * * If it is enabled check the packet and don't broadcast it if we have already * seen this packet. * * Broadcast: send it to the interfaces that aren't on the packet visited nets * array, just after the IPX header. * * Returns -EINVAL for invalid packets, so that the calling function drops * the packet without local processing. 0 if packet is to be locally processed. */static int ipxitf_pprop(ipx_interface *intrfc, struct sk_buff *skb){	struct ipxhdr *ipx = skb->nh.ipxh;	int i, ret = -EINVAL;	ipx_interface *ifcs;	char *c;	u32 *l;	/* Illegal packet - too many hops or too short */	/* We decide to throw it away: no broadcasting, no local processing.	 * NetBIOS unaware implementations route them as normal packets -	 * tctrl <= 15, any data payload... */	if (IPX_SKB_CB(skb)->ipx_tctrl > IPX_MAX_PPROP_HOPS ||	    ntohs(ipx->ipx_pktsize) < sizeof(struct ipxhdr) +	    				IPX_MAX_PPROP_HOPS * sizeof(u32))		goto out;	/* are we broadcasting this damn thing? */	ret = 0;	if (!sysctl_ipx_pprop_broadcasting)		goto out;	/* We do broadcast packet on the IPX_MAX_PPROP_HOPS hop, but we	 * process it locally. All previous hops broadcasted it, and process it	 * locally. */	if (IPX_SKB_CB(skb)->ipx_tctrl == IPX_MAX_PPROP_HOPS)		goto out;		c = ((u8 *) ipx) + sizeof(struct ipxhdr);	l = (u32 *) c;	/* Don't broadcast packet if already seen this net */	for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)		if (*l++ == intrfc->if_netnum)			goto out;	/* < IPX_MAX_PPROP_HOPS hops && input interface not in list. Save the	 * position where we will insert recvd netnum into list, later on,	 * in ipxitf_send */	IPX_SKB_CB(skb)->last_hop.index = i;	IPX_SKB_CB(skb)->last_hop.netnum = intrfc->if_netnum;	/* xmit on all other interfaces... */	spin_lock_bh(&ipx_interfaces_lock);	for (ifcs = ipx_interfaces; ifcs; ifcs = ifcs->if_next) {		/* Except unconfigured interfaces */		if (!ifcs->if_netnum)			continue;							/* That aren't in the list */		if (ifcs == intrfc)			continue;		l = (__u32 *) c;		/* don't consider the last entry in the packet list,		 * it is our netnum, and it is not there yet */		for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)			if (ifcs->if_netnum == *l++)				break;		if (i == IPX_SKB_CB(skb)->ipx_tctrl) {			struct sk_buff *s = skb_copy(skb, GFP_ATOMIC);			if (s) {				IPX_SKB_CB(s)->ipx_dest_net = ifcs->if_netnum;				ipxrtr_route_skb(s);			}		}	}	spin_unlock_bh(&ipx_interfaces_lock);out:	return ret;}static void ipxitf_insert(ipx_interface *intrfc){	intrfc->if_next = NULL;	spin_lock_bh(&ipx_interfaces_lock);	if (!ipx_interfaces)		ipx_interfaces = intrfc;	else {		ipx_interface *i = ipx_interfaces;		while (i->if_next)			i = i->if_next;		i->if_next = intrfc;	}	spin_unlock_bh(&ipx_interfaces_lock);	if (ipxcfg_auto_select_primary && !ipx_primary_net)		ipx_primary_net = intrfc;}static ipx_interface *ipxitf_alloc(struct net_device *dev, __u32 netnum,				   unsigned short dlink_type,				   struct datalink_proto *dlink,				   unsigned char internal, int ipx_offset){	ipx_interface *intrfc = kmalloc(sizeof(*intrfc), GFP_ATOMIC);	if (intrfc) {		intrfc->if_dev		= dev;		intrfc->if_netnum	= netnum;		intrfc->if_dlink_type 	= dlink_type;		intrfc->if_dlink 	= dlink;		intrfc->if_internal 	= internal;		intrfc->if_ipx_offset 	= ipx_offset;		intrfc->if_sknum 	= IPX_MIN_EPHEMERAL_SOCKET;		intrfc->if_sklist 	= NULL;		atomic_set(&intrfc->refcnt, 1);		spin_lock_init(&intrfc->if_sklist_lock);		MOD_INC_USE_COUNT;	}	return intrfc;}static int ipxitf_create_internal(ipx_interface_definition *idef){	ipx_interface *intrfc;	int ret = -EEXIST;	/* Only one primary network allowed */	if (ipx_primary_net)		goto out;	/* Must have a valid network number */	ret = -EADDRNOTAVAIL;	if (!idef->ipx_network)		goto out;	intrfc = ipxitf_find_using_net(idef->ipx_network);	ret = -EADDRINUSE;	if (intrfc) {		ipxitf_put(intrfc);		goto out;	}	intrfc = ipxitf_alloc(NULL, idef->ipx_network, 0, NULL, 1, 0);	ret = -EAGAIN;

⌨️ 快捷键说明

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