Merge /pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / net / llc / llc_sap.c
1 /*
2  * llc_sap.c - driver routines for SAP component.
3  *
4  * Copyright (c) 1997 by Procom Technology, Inc.
5  *               2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6  *
7  * This program can be redistributed or modified under the terms of the
8  * GNU General Public License as published by the Free Software Foundation.
9  * This program is distributed without any warranty or implied warranty
10  * of merchantability or fitness for a particular purpose.
11  *
12  * See the GNU General Public License for more details.
13  */
14
15 #include <net/llc.h>
16 #include <net/llc_if.h>
17 #include <net/llc_conn.h>
18 #include <net/llc_pdu.h>
19 #include <net/llc_sap.h>
20 #include <net/llc_s_ac.h>
21 #include <net/llc_s_ev.h>
22 #include <net/llc_s_st.h>
23 #include <net/sock.h>
24 #include <net/tcp_states.h>
25 #include <linux/llc.h>
26
27 /**
28  *      llc_alloc_frame - allocates sk_buff for frame
29  *      @dev: network device this skb will be sent over
30  *
31  *      Allocates an sk_buff for frame and initializes sk_buff fields.
32  *      Returns allocated skb or %NULL when out of memory.
33  */
34 struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev)
35 {
36         struct sk_buff *skb = alloc_skb(128, GFP_ATOMIC);
37
38         if (skb) {
39                 skb_reset_mac_header(skb);
40                 skb_reserve(skb, 50);
41                 skb_reset_network_header(skb);
42                 skb_reset_transport_header(skb);
43                 skb->protocol = htons(ETH_P_802_2);
44                 skb->dev      = dev;
45                 if (sk != NULL)
46                         skb_set_owner_w(skb, sk);
47         }
48         return skb;
49 }
50
51 void llc_save_primitive(struct sock *sk, struct sk_buff* skb, u8 prim)
52 {
53         struct sockaddr_llc *addr;
54
55        /* save primitive for use by the user. */
56         addr              = llc_ui_skb_cb(skb);
57
58         memset(addr, 0, sizeof(*addr));
59         addr->sllc_family = sk->sk_family;
60         addr->sllc_arphrd = skb->dev->type;
61         addr->sllc_test   = prim == LLC_TEST_PRIM;
62         addr->sllc_xid    = prim == LLC_XID_PRIM;
63         addr->sllc_ua     = prim == LLC_DATAUNIT_PRIM;
64         llc_pdu_decode_sa(skb, addr->sllc_mac);
65         llc_pdu_decode_ssap(skb, &addr->sllc_sap);
66 }
67
68 /**
69  *      llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu.
70  *      @sap: pointer to SAP
71  *      @skb: received pdu
72  */
73 void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb)
74 {
75         struct llc_sap_state_ev *ev = llc_sap_ev(skb);
76         struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
77
78         switch (LLC_U_PDU_RSP(pdu)) {
79         case LLC_1_PDU_CMD_TEST:
80                 ev->prim = LLC_TEST_PRIM;       break;
81         case LLC_1_PDU_CMD_XID:
82                 ev->prim = LLC_XID_PRIM;        break;
83         case LLC_1_PDU_CMD_UI:
84                 ev->prim = LLC_DATAUNIT_PRIM;   break;
85         }
86         ev->ind_cfm_flag = LLC_IND;
87 }
88
89 /**
90  *      llc_find_sap_trans - finds transition for event
91  *      @sap: pointer to SAP
92  *      @skb: happened event
93  *
94  *      This function finds transition that matches with happened event.
95  *      Returns the pointer to found transition on success or %NULL for
96  *      failure.
97  */
98 static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap,
99                                                       struct sk_buff* skb)
100 {
101         int i = 0;
102         struct llc_sap_state_trans *rc = NULL;
103         struct llc_sap_state_trans **next_trans;
104         struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1];
105         /*
106          * Search thru events for this state until list exhausted or until
107          * its obvious the event is not valid for the current state
108          */
109         for (next_trans = curr_state->transitions; next_trans[i]->ev; i++)
110                 if (!next_trans[i]->ev(sap, skb)) {
111                         rc = next_trans[i]; /* got event match; return it */
112                         break;
113                 }
114         return rc;
115 }
116
117 /**
118  *      llc_exec_sap_trans_actions - execute actions related to event
119  *      @sap: pointer to SAP
120  *      @trans: pointer to transition that it's actions must be performed
121  *      @skb: happened event.
122  *
123  *      This function executes actions that is related to happened event.
124  *      Returns 0 for success and 1 for failure of at least one action.
125  */
126 static int llc_exec_sap_trans_actions(struct llc_sap *sap,
127                                       struct llc_sap_state_trans *trans,
128                                       struct sk_buff *skb)
129 {
130         int rc = 0;
131         llc_sap_action_t *next_action = trans->ev_actions;
132
133         for (; next_action && *next_action; next_action++)
134                 if ((*next_action)(sap, skb))
135                         rc = 1;
136         return rc;
137 }
138
139 /**
140  *      llc_sap_next_state - finds transition, execs actions & change SAP state
141  *      @sap: pointer to SAP
142  *      @skb: happened event
143  *
144  *      This function finds transition that matches with happened event, then
145  *      executes related actions and finally changes state of SAP. It returns
146  *      0 on success and 1 for failure.
147  */
148 static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb)
149 {
150         int rc = 1;
151         struct llc_sap_state_trans *trans;
152
153         if (sap->state > LLC_NR_SAP_STATES)
154                 goto out;
155         trans = llc_find_sap_trans(sap, skb);
156         if (!trans)
157                 goto out;
158         /*
159          * Got the state to which we next transition; perform the actions
160          * associated with this transition before actually transitioning to the
161          * next state
162          */
163         rc = llc_exec_sap_trans_actions(sap, trans, skb);
164         if (rc)
165                 goto out;
166         /*
167          * Transition SAP to next state if all actions execute successfully
168          */
169         sap->state = trans->next_state;
170 out:
171         return rc;
172 }
173
174 /**
175  *      llc_sap_state_process - sends event to SAP state machine
176  *      @sap: sap to use
177  *      @skb: pointer to occurred event
178  *
179  *      After executing actions of the event, upper layer will be indicated
180  *      if needed(on receiving an UI frame). sk can be null for the
181  *      datalink_proto case.
182  */
183 static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
184 {
185         struct llc_sap_state_ev *ev = llc_sap_ev(skb);
186
187         /*
188          * We have to hold the skb, because llc_sap_next_state
189          * will kfree it in the sending path and we need to
190          * look at the skb->cb, where we encode llc_sap_state_ev.
191          */
192         skb_get(skb);
193         ev->ind_cfm_flag = 0;
194         llc_sap_next_state(sap, skb);
195         if (ev->ind_cfm_flag == LLC_IND) {
196                 if (skb->sk->sk_state == TCP_LISTEN)
197                         kfree_skb(skb);
198                 else {
199                         llc_save_primitive(skb->sk, skb, ev->prim);
200
201                         /* queue skb to the user. */
202                         if (sock_queue_rcv_skb(skb->sk, skb))
203                                 kfree_skb(skb);
204                 }
205         }
206         kfree_skb(skb);
207 }
208
209 /**
210  *      llc_build_and_send_test_pkt - TEST interface for upper layers.
211  *      @sap: sap to use
212  *      @skb: packet to send
213  *      @dmac: destination mac address
214  *      @dsap: destination sap
215  *
216  *      This function is called when upper layer wants to send a TEST pdu.
217  *      Returns 0 for success, 1 otherwise.
218  */
219 void llc_build_and_send_test_pkt(struct llc_sap *sap,
220                                  struct sk_buff *skb, u8 *dmac, u8 dsap)
221 {
222         struct llc_sap_state_ev *ev = llc_sap_ev(skb);
223
224         ev->saddr.lsap = sap->laddr.lsap;
225         ev->daddr.lsap = dsap;
226         memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
227         memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
228
229         ev->type      = LLC_SAP_EV_TYPE_PRIM;
230         ev->prim      = LLC_TEST_PRIM;
231         ev->prim_type = LLC_PRIM_TYPE_REQ;
232         llc_sap_state_process(sap, skb);
233 }
234
235 /**
236  *      llc_build_and_send_xid_pkt - XID interface for upper layers
237  *      @sap: sap to use
238  *      @skb: packet to send
239  *      @dmac: destination mac address
240  *      @dsap: destination sap
241  *
242  *      This function is called when upper layer wants to send a XID pdu.
243  *      Returns 0 for success, 1 otherwise.
244  */
245 void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
246                                 u8 *dmac, u8 dsap)
247 {
248         struct llc_sap_state_ev *ev = llc_sap_ev(skb);
249
250         ev->saddr.lsap = sap->laddr.lsap;
251         ev->daddr.lsap = dsap;
252         memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
253         memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
254
255         ev->type      = LLC_SAP_EV_TYPE_PRIM;
256         ev->prim      = LLC_XID_PRIM;
257         ev->prim_type = LLC_PRIM_TYPE_REQ;
258         llc_sap_state_process(sap, skb);
259 }
260
261 /**
262  *      llc_sap_rcv - sends received pdus to the sap state machine
263  *      @sap: current sap component structure.
264  *      @skb: received frame.
265  *
266  *      Sends received pdus to the sap state machine.
267  */
268 static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb)
269 {
270         struct llc_sap_state_ev *ev = llc_sap_ev(skb);
271
272         ev->type   = LLC_SAP_EV_TYPE_PDU;
273         ev->reason = 0;
274         llc_sap_state_process(sap, skb);
275 }
276
277 /**
278  *      llc_lookup_dgram - Finds dgram socket for the local sap/mac
279  *      @sap: SAP
280  *      @laddr: address of local LLC (MAC + SAP)
281  *
282  *      Search socket list of the SAP and finds connection using the local
283  *      mac, and local sap. Returns pointer for socket found, %NULL otherwise.
284  */
285 static struct sock *llc_lookup_dgram(struct llc_sap *sap,
286                                      const struct llc_addr *laddr)
287 {
288         struct sock *rc;
289         struct hlist_node *node;
290
291         read_lock_bh(&sap->sk_list.lock);
292         sk_for_each(rc, node, &sap->sk_list.list) {
293                 struct llc_sock *llc = llc_sk(rc);
294
295                 if (rc->sk_type == SOCK_DGRAM &&
296                     llc->laddr.lsap == laddr->lsap &&
297                     llc_mac_match(llc->laddr.mac, laddr->mac)) {
298                         sock_hold(rc);
299                         goto found;
300                 }
301         }
302         rc = NULL;
303 found:
304         read_unlock_bh(&sap->sk_list.lock);
305         return rc;
306 }
307
308 /**
309  *      llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
310  *      @sap: SAP
311  *      @laddr: address of local LLC (MAC + SAP)
312  *
313  *      Search socket list of the SAP and finds connections with same sap.
314  *      Deliver clone to each.
315  */
316 static void llc_sap_mcast(struct llc_sap *sap,
317                           const struct llc_addr *laddr,
318                           struct sk_buff *skb)
319 {
320         struct sock *sk;
321         struct hlist_node *node;
322
323         read_lock_bh(&sap->sk_list.lock);
324         sk_for_each(sk, node, &sap->sk_list.list) {
325                 struct llc_sock *llc = llc_sk(sk);
326                 struct sk_buff *skb1;
327
328                 if (sk->sk_type != SOCK_DGRAM)
329                         continue;
330
331                 if (llc->laddr.lsap != laddr->lsap)
332                         continue;
333
334                 if (llc->dev != skb->dev)
335                         continue;
336
337                 skb1 = skb_clone(skb, GFP_ATOMIC);
338                 if (!skb1)
339                         break;
340
341                 sock_hold(sk);
342                 skb_set_owner_r(skb1, sk);
343                 llc_sap_rcv(sap, skb1);
344                 sock_put(sk);
345         }
346         read_unlock_bh(&sap->sk_list.lock);
347 }
348
349
350 void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
351 {
352         struct llc_addr laddr;
353
354         llc_pdu_decode_da(skb, laddr.mac);
355         llc_pdu_decode_dsap(skb, &laddr.lsap);
356
357         if (llc_mac_multicast(laddr.mac)) {
358                 llc_sap_mcast(sap, &laddr, skb);
359                 kfree_skb(skb);
360         } else {
361                 struct sock *sk = llc_lookup_dgram(sap, &laddr);
362                 if (sk) {
363                         skb_set_owner_r(skb, sk);
364                         llc_sap_rcv(sap, skb);
365                         sock_put(sk);
366                 } else
367                         kfree_skb(skb);
368         }
369 }