more changes on original files
[linux-2.4.git] / net / 802 / p8022.c
1 /*
2  *      NET3:   Support for 802.2 demultiplexing off Ethernet (Token ring
3  *              is kept separate see p8022tr.c)
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  *              Demultiplex 802.2 encoded protocols. We match the entry by the
10  *              SSAP/DSAP pair and then deliver to the registered datalink that
11  *              matches. The control byte is ignored and handling of such items
12  *              is up to the routine passed the frame.
13  *
14  *              Unlike the 802.3 datalink we have a list of 802.2 entries as there
15  *              are multiple protocols to demux. The list is currently short (3 or
16  *              4 entries at most). The current demux assumes this.
17  */
18
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
21 #include <linux/skbuff.h>
22 #include <net/datalink.h>
23 #include <linux/mm.h>
24 #include <linux/in.h>
25 #include <linux/init.h>
26 #include <net/p8022.h>
27
28 static struct datalink_proto *p8022_list = NULL;
29
30 /*
31  *      We don't handle the loopback SAP stuff, the extended
32  *      802.2 command set, multicast SAP identifiers and non UI
33  *      frames. We have the absolute minimum needed for IPX,
34  *      IP and Appletalk phase 2. See the llc_* routines for
35  *      support libraries if your protocol needs these.
36  */
37
38 static struct datalink_proto *find_8022_client(unsigned char type)
39 {
40         struct datalink_proto   *proto;
41
42         for (proto = p8022_list;
43                 ((proto != NULL) && (*(proto->type) != type));
44                 proto = proto->next)
45                 ;
46
47         return proto;
48 }
49
50 int p8022_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
51 {
52         struct datalink_proto   *proto;
53
54         proto = find_8022_client(*(skb->h.raw));
55         if (proto != NULL) 
56         {
57                 skb->h.raw += 3;
58                 skb->nh.raw += 3;
59                 skb_pull(skb,3);
60                 return proto->rcvfunc(skb, dev, pt);
61         }
62
63         skb->sk = NULL;
64         kfree_skb(skb);
65         return 0;
66 }
67
68 static void p8022_datalink_header(struct datalink_proto *dl,
69                 struct sk_buff *skb, unsigned char *dest_node)
70 {
71         struct net_device       *dev = skb->dev;
72         unsigned char   *rawp;
73
74         rawp = skb_push(skb,3);
75         *rawp++ = dl->type[0];
76         *rawp++ = dl->type[0];
77         *rawp = 0x03;   /* UI */
78         dev->hard_header(skb, dev, ETH_P_802_3, dest_node, NULL, skb->len);
79 }
80
81 static struct packet_type p8022_packet_type =
82 {
83         0,      /* MUTTER ntohs(ETH_P_8022),*/
84         NULL,           /* All devices */
85         p8022_rcv,
86         NULL,
87         NULL,
88 };
89
90 EXPORT_SYMBOL(register_8022_client);
91 EXPORT_SYMBOL(unregister_8022_client);
92
93 static int __init p8022_init(void)
94 {
95         p8022_packet_type.type=htons(ETH_P_802_2);
96         dev_add_pack(&p8022_packet_type);
97         return 0;
98 }
99
100 static void __exit p8022_exit(void)
101 {
102         dev_remove_pack(&p8022_packet_type);
103         return;
104 }
105
106 module_init(p8022_init);
107 module_exit(p8022_exit);
108
109 struct datalink_proto *register_8022_client(unsigned char type, int (*rcvfunc)(struct sk_buff *, struct net_device *, struct packet_type *))
110 {
111         struct datalink_proto   *proto;
112
113         if (find_8022_client(type) != NULL)
114                 return NULL;
115
116         proto = (struct datalink_proto *) kmalloc(sizeof(*proto), GFP_ATOMIC);
117         if (proto != NULL) {
118                 proto->type[0] = type;
119                 proto->type_len = 1;
120                 proto->rcvfunc = rcvfunc;
121                 proto->header_length = 3;
122                 proto->datalink_header = p8022_datalink_header;
123                 proto->string_name = "802.2";
124                 proto->next = p8022_list;
125                 p8022_list = proto;
126         }
127
128         return proto;
129 }
130
131 void unregister_8022_client(unsigned char type)
132 {
133         struct datalink_proto *tmp, **clients = &p8022_list;
134         unsigned long flags;
135
136         save_flags(flags);
137         cli();
138
139         while ((tmp = *clients) != NULL)
140         {
141                 if (tmp->type[0] == type) {
142                         *clients = tmp->next;
143                         kfree(tmp);
144                         break;
145                 } else {
146                         clients = &tmp->next;
147                 }
148         }
149
150         restore_flags(flags);
151 }
152
153 MODULE_LICENSE("GPL");