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

📄 af_ipx.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
	if (copy)		skb1 = skb_clone(skb, GFP_ATOMIC);	else		skb1 = skb;	rc = -ENOMEM;	if (!skb1)		goto out_put;	/* 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);	if (!skb2)		goto out_put;	if (sock2)		ipxitf_def_skb_handler(sock2, skb2);	rc = 0;out_put:	if (sock1)		sock_put(sock1);	if (sock2)		sock_put(sock2);out:	return rc;}#endif	/* CONFIG_IPX_INTERN */static struct sk_buff *ipxitf_adjust_skbuff(struct ipx_interface *intrfc,					    struct sk_buff *skb){	struct sk_buff *skb2;	int in_offset = (unsigned char *)ipx_hdr(skb) - 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);		skb_reset_network_header(skb2);		skb_reset_transport_header(skb2);		skb_put(skb2, skb->len);		memcpy(ipx_hdr(skb2), ipx_hdr(skb), 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 */int ipxitf_send(struct ipx_interface *intrfc, struct sk_buff *skb, char *node){	struct ipxhdr *ipx = ipx_hdr(skb);	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) {		__be32 *last_hop = (__be32 *)(((u8 *) skb->data) +				sizeof(struct ipxhdr) +				IPX_SKB_CB(skb)->last_hop.index *				sizeof(__be32));		*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 it's 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	= htons(ETH_P_IPX);	/* Send it out */	dl->request(dl, skb, dest_node);out:	return 0;}static int ipxitf_add_local_route(struct ipx_interface *intrfc){	return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);}static void ipxitf_discover_netnum(struct ipx_interface *intrfc,				   struct sk_buff *skb);static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb);static int ipxitf_rcv(struct ipx_interface *intrfc, struct sk_buff *skb){	struct ipxhdr *ipx = ipx_hdr(skb);	int rc = 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) {		rc = ipxitf_pprop(intrfc, skb);		if (rc)			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)				rc = 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)) {		rc = 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 rc;}static void ipxitf_discover_netnum(struct 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) {		struct 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) ntohl(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(struct ipx_interface *intrfc, struct sk_buff *skb){	struct ipxhdr *ipx = ipx_hdr(skb);	int i, rc = -EINVAL;	struct ipx_interface *ifcs;	char *c;	__be32 *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? */	rc = 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 = (__be32 *) 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);	list_for_each_entry(ifcs, &ipx_interfaces, node) {		/* Except unconfigured interfaces */		if (!ifcs->if_netnum)			continue;		/* That aren't in the list */		if (ifcs == intrfc)			continue;		l = (__be32 *) 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 rc;}static void ipxitf_insert(struct ipx_interface *intrfc){	spin_lock_bh(&ipx_interfaces_lock);	list_add_tail(&intrfc->node, &ipx_interfaces);	spin_unlock_bh(&ipx_interfaces_lock);	if (ipxcfg_auto_select_primary && !ipx_primary_net)		ipx_primary_net = intrfc;}static struct ipx_interface *ipxitf_alloc(struct net_device *dev, __be32 netnum,					  __be16 dlink_type,					  struct datalink_proto *dlink,					  unsigned char internal,					  int ipx_offset){	struct 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;		INIT_HLIST_HEAD(&intrfc->if_sklist);		atomic_set(&intrfc->refcnt, 1);		spin_lock_init(&intrfc->if_sklist_lock);	}	return intrfc;}static int ipxitf_create_internal(struct ipx_interface_definition *idef){	struct ipx_interface *intrfc;	int rc = -EEXIST;	/* Only one primary network allowed */	if (ipx_primary_net)		goto out;	/* Must have a valid network number */	rc = -EADDRNOTAVAIL;	if (!idef->ipx_network)		goto out;	intrfc = ipxitf_find_using_net(idef->ipx_network);	rc = -EADDRINUSE;	if (intrfc) {		ipxitf_put(intrfc);		goto out;	}	intrfc = ipxitf_alloc(NULL, idef->ipx_network, 0, NULL, 1, 0);	rc = -EAGAIN;	if (!intrfc)		goto out;	memcpy((char *)&(intrfc->if_node), idef->ipx_node, IPX_NODE_LEN);	ipx_internal_net = ipx_primary_net = intrfc;	ipxitf_hold(intrfc);	ipxitf_insert(intrfc);	rc = ipxitf_add_local_route(intrfc);	ipxitf_put(intrfc);out:	return rc;}static __be16 ipx_map_frame_type(unsigned char type){	__be16 rc = 0;	switch (type) {	case IPX_FRAME_ETHERII:	rc = htons(ETH_P_IPX);		break;	case IPX_FRAME_8022:	rc = htons(ETH_P_802_2);	break;	case IPX_FRAME_SNAP:	rc = htons(ETH_P_SNAP);		break;	case IPX_FRAME_8023:	rc = htons(ETH_P_802_3);	break;	}	return rc;}static int ipxitf_create(struct ipx_interface_definition *idef){	struct net_device *dev;	__be16 dlink_type = 0;	struct datalink_proto *datalink = NULL;	struct ipx_interface *intrfc;	int rc;	if (idef->ipx_special == IPX_INTERNAL) {		rc = ipxitf_create_internal(idef);		goto out;	}	rc = -EEXIST;	if (idef->ipx_special == IPX_PRIMARY && ipx_primary_net)		goto out;	intrfc = ipxitf_find_using_net(idef->ipx_network);	rc = -EADDRINUSE;	if (idef->ipx_network && intrfc) {		ipxitf_put(intrfc);		goto out;	}	if (intrfc)		ipxitf_put(intrfc);	dev = dev_get_by_name(&init_net, idef->ipx_device);	rc = -ENODEV;	if (!dev)		goto out;	switch (idef->ipx_dlink_type) {	case IPX_FRAME_TR_8022:		printk(KERN_WARNING "IPX frame type 802.2TR is "			"obsolete Use 802.2 instead.\n");		/* fall through */	case IPX_FRAME_8022:		dlink_type 	= htons(ETH_P_802_2);		datalink 	= p8022_datalink;		break;	case IPX_FRAME_ETHERII:		if (dev->type != ARPHRD_IEEE802) {			dlink_type 	= htons(ETH_P_IPX);			datalink 	= pEII_datalink;			break;		} else			printk(KERN_WARNING "IPX frame type EtherII over "					"token-ring is obsolete. Use SNAP "					"instead.\n");		/* fall through */	case IPX_FRAME_SNAP:		dlink_type 	= htons(ETH_P_SNAP);		datalink 	= pSNAP_datalink;		break;	case IPX_FRAME_8023:		dlink_type 	= htons(ETH_P_802_3);		datalink 	= p8023_datalink;		break;	case IPX_FRAME_NONE:	default:		rc = -EPROTONOSUPPORT;		goto out_dev;	}	rc = -ENETDOWN;	if (!(dev->flags & IFF_UP))		goto out_dev;	/* Check addresses are suitable */	rc = -EINVAL;	if (dev->addr_len > IPX_NODE_LEN)		goto out_dev;	intrfc = ipxitf_find_using_phys(dev, dlink_type);	if (!intrfc) {		/* Ok now create */		intrfc = ipxitf_alloc(dev, idef->ipx_network, dlink_type,				      datalink, 0, dev->hard_header_len +					datalink->header_length);

⌨️ 快捷键说明

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