ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / 802 / fddi.c
1 /*
2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
3  *              operating system.  INET is implemented using the BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              FDDI-type device handling.
7  *
8  * Version:     @(#)fddi.c      1.0.0   08/12/96
9  *
10  * Authors:     Lawrence V. Stefani, <stefani@lkg.dec.com>
11  *
12  *              fddi.c is based on previous eth.c and tr.c work by
13  *                      Ross Biro, <bir7@leland.Stanford.Edu>
14  *                      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
15  *                      Mark Evans, <evansmp@uhura.aston.ac.uk>
16  *                      Florian La Roche, <rzsfl@rz.uni-sb.de>
17  *                      Alan Cox, <gw4pts@gw4pts.ampr.org>
18  * 
19  *              This program is free software; you can redistribute it and/or
20  *              modify it under the terms of the GNU General Public License
21  *              as published by the Free Software Foundation; either version
22  *              2 of the License, or (at your option) any later version.
23  *
24  *      Changes
25  *              Alan Cox                :       New arp/rebuild header
26  *              Maciej W. Rozycki       :       IPv6 support
27  */
28  
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <asm/system.h>
32 #include <linux/types.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/string.h>
36 #include <linux/mm.h>
37 #include <linux/socket.h>
38 #include <linux/in.h>
39 #include <linux/inet.h>
40 #include <linux/netdevice.h>
41 #include <linux/fddidevice.h>
42 #include <linux/if_ether.h>
43 #include <linux/skbuff.h>
44 #include <linux/errno.h>
45 #include <net/arp.h>
46 #include <net/sock.h>
47
48 /*
49  * Create the FDDI MAC header for an arbitrary protocol layer
50  *
51  * saddr=NULL   means use device source address
52  * daddr=NULL   means leave destination address (eg unresolved arp)
53  */
54
55 int fddi_header(struct sk_buff  *skb, struct net_device *dev, unsigned short type,
56                 void *daddr, void *saddr, unsigned len)
57 {
58         int hl = FDDI_K_SNAP_HLEN;
59         struct fddihdr *fddi;
60         
61         if(type != ETH_P_IP && type != ETH_P_IPV6 && type != ETH_P_ARP)
62                 hl=FDDI_K_8022_HLEN-3;
63         fddi = (struct fddihdr *)skb_push(skb, hl);
64         fddi->fc                         = FDDI_FC_K_ASYNC_LLC_DEF;
65         if(type == ETH_P_IP || type == ETH_P_IPV6 || type == ETH_P_ARP)
66         {
67                 fddi->hdr.llc_snap.dsap          = FDDI_EXTENDED_SAP;
68                 fddi->hdr.llc_snap.ssap          = FDDI_EXTENDED_SAP;
69                 fddi->hdr.llc_snap.ctrl          = FDDI_UI_CMD;
70                 fddi->hdr.llc_snap.oui[0]        = 0x00;
71                 fddi->hdr.llc_snap.oui[1]        = 0x00;
72                 fddi->hdr.llc_snap.oui[2]        = 0x00;
73                 fddi->hdr.llc_snap.ethertype     = htons(type);
74         }
75
76         /* Set the source and destination hardware addresses */
77          
78         if (saddr != NULL)
79                 memcpy(fddi->saddr, saddr, dev->addr_len);
80         else
81                 memcpy(fddi->saddr, dev->dev_addr, dev->addr_len);
82
83         if (daddr != NULL)
84         {
85                 memcpy(fddi->daddr, daddr, dev->addr_len);
86                 return(hl);
87         }
88
89         return(-hl);
90 }
91
92
93 /*
94  * Rebuild the FDDI MAC header. This is called after an ARP
95  * (or in future other address resolution) has completed on
96  * this sk_buff.  We now let ARP fill in the other fields.
97  */
98  
99 int fddi_rebuild_header(struct sk_buff  *skb)
100 {
101         struct fddihdr *fddi = (struct fddihdr *)skb->data;
102
103 #ifdef CONFIG_INET
104         if (fddi->hdr.llc_snap.ethertype == __constant_htons(ETH_P_IP))
105                 /* Try to get ARP to resolve the header and fill destination address */
106                 return arp_find(fddi->daddr, skb);
107         else
108 #endif  
109         {
110                 printk("%s: Don't know how to resolve type %02X addresses.\n",
111                        skb->dev->name, htons(fddi->hdr.llc_snap.ethertype));
112                 return(0);
113         }
114 }
115
116
117 /*
118  * Determine the packet's protocol ID and fill in skb fields.
119  * This routine is called before an incoming packet is passed
120  * up.  It's used to fill in specific skb fields and to set
121  * the proper pointer to the start of packet data (skb->data).
122  */
123  
124 unsigned short fddi_type_trans(struct sk_buff *skb, struct net_device *dev)
125 {
126         struct fddihdr *fddi = (struct fddihdr *)skb->data;
127         unsigned short type;
128         
129         /*
130          * Set mac.raw field to point to FC byte, set data field to point
131          * to start of packet data.  Assume 802.2 SNAP frames for now.
132          */
133
134         skb->mac.raw = skb->data;       /* point to frame control (FC) */
135         
136         if(fddi->hdr.llc_8022_1.dsap==0xe0)
137         {
138                 skb_pull(skb, FDDI_K_8022_HLEN-3);
139                 type = __constant_htons(ETH_P_802_2);
140         }
141         else
142         {
143                 skb_pull(skb, FDDI_K_SNAP_HLEN);                /* adjust for 21 byte header */
144                 type=fddi->hdr.llc_snap.ethertype;
145         }
146         
147         /* Set packet type based on destination address and flag settings */
148                         
149         if (*fddi->daddr & 0x01)
150         {
151                 if (memcmp(fddi->daddr, dev->broadcast, FDDI_K_ALEN) == 0)
152                         skb->pkt_type = PACKET_BROADCAST;
153                 else
154                         skb->pkt_type = PACKET_MULTICAST;
155         }
156         
157         else if (dev->flags & IFF_PROMISC)
158         {
159                 if (memcmp(fddi->daddr, dev->dev_addr, FDDI_K_ALEN))
160                         skb->pkt_type = PACKET_OTHERHOST;
161         }
162
163         /* Assume 802.2 SNAP frames, for now */
164
165         return(type);
166 }
167
168 EXPORT_SYMBOL(fddi_type_trans);