ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / 802 / fc.c
1 /*
2  * NET3:        Fibre Channel device handling subroutines
3  * 
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  *              Vineet Abraham <vma@iol.unh.edu>
10  *              v 1.0 03/22/99
11  */
12
13 #include <linux/config.h>
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/socket.h>
22 #include <linux/in.h>
23 #include <linux/inet.h>
24 #include <linux/netdevice.h>
25 #include <linux/fcdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/timer.h>
29 #include <linux/net.h>
30 #include <linux/proc_fs.h>
31 #include <linux/init.h>
32 #include <net/arp.h>
33
34 /*
35  *      Put the headers on a Fibre Channel packet. 
36  */
37  
38 int fc_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
39               void *daddr, void *saddr, unsigned len) 
40 {
41         struct fch_hdr *fch;
42         int hdr_len;
43
44         /* 
45          * Add the 802.2 SNAP header if IP as the IPv4 code calls  
46          * dev->hard_header directly.
47          */
48         if (type == ETH_P_IP || type == ETH_P_ARP)
49         {
50                 struct fcllc *fcllc=(struct fcllc *)(fch+1);
51
52                 hdr_len = sizeof(struct fch_hdr) + sizeof(struct fcllc);
53                 fch = (struct fch_hdr *)skb_push(skb, hdr_len);
54                 fcllc = (struct fcllc *)(fch+1);
55                 fcllc->dsap = fcllc->ssap = EXTENDED_SAP;
56                 fcllc->llc = UI_CMD;
57                 fcllc->protid[0] = fcllc->protid[1] = fcllc->protid[2] = 0x00;
58                 fcllc->ethertype = htons(type);
59         }
60         else
61         {
62                 hdr_len = sizeof(struct fch_hdr);
63                 fch = (struct fch_hdr *)skb_push(skb, hdr_len); 
64         }
65
66         if(saddr)
67                 memcpy(fch->saddr,saddr,dev->addr_len);
68         else
69                 memcpy(fch->saddr,dev->dev_addr,dev->addr_len);
70
71         if(daddr) 
72         {
73                 memcpy(fch->daddr,daddr,dev->addr_len);
74                 return(hdr_len);
75         }
76         return -hdr_len;
77 }
78         
79 /*
80  *      A neighbour discovery of some species (eg arp) has completed. We
81  *      can now send the packet.
82  */
83  
84 int fc_rebuild_header(struct sk_buff *skb) 
85 {
86         struct fch_hdr *fch=(struct fch_hdr *)skb->data;
87         struct fcllc *fcllc=(struct fcllc *)(skb->data+sizeof(struct fch_hdr));
88         if(fcllc->ethertype != htons(ETH_P_IP)) {
89                 printk("fc_rebuild_header: Don't know how to resolve type %04X addresses ?\n",(unsigned int)htons(fcllc->ethertype));
90                 return 0;
91         }
92 #ifdef CONFIG_INET
93         return arp_find(fch->daddr, skb);
94 #else
95         return 0;
96 #endif
97 }
98
99 unsigned short
100 fc_type_trans(struct sk_buff *skb, struct net_device *dev)
101 {
102         struct fch_hdr *fch = (struct fch_hdr *)skb->data;
103         struct fcllc *fcllc;
104
105         skb->mac.raw = skb->data;
106         fcllc = (struct fcllc *)(skb->data + sizeof (struct fch_hdr) + 2);
107         skb_pull(skb, sizeof (struct fch_hdr) + 2);
108
109         if (*fch->daddr & 1) {
110                 if (!memcmp(fch->daddr, dev->broadcast, FC_ALEN))
111                         skb->pkt_type = PACKET_BROADCAST;
112                 else
113                         skb->pkt_type = PACKET_MULTICAST;
114         } else if (dev->flags & IFF_PROMISC) {
115                 if (memcmp(fch->daddr, dev->dev_addr, FC_ALEN))
116                         skb->pkt_type = PACKET_OTHERHOST;
117         }
118
119         /*
120          * Strip the SNAP header from ARP packets since we don't pass
121          * them through to the 802.2/SNAP layers.
122          */
123         if (fcllc->dsap == EXTENDED_SAP &&
124             (fcllc->ethertype == ntohs(ETH_P_IP) ||
125              fcllc->ethertype == ntohs(ETH_P_ARP))) {
126                 skb_pull(skb, sizeof (struct fcllc));
127                 return fcllc->ethertype;
128         }
129
130         return ntohs(ETH_P_802_2);
131 }