davinci: Macro to convert GPIO signal to GPIO pin number
[linux-ginger.git] / net / llc / llc_sap.c
blob008de1fc42ca79b07cafed68ccb3e7e396161dc9
1 /*
2 * llc_sap.c - driver routines for SAP component.
4 * Copyright (c) 1997 by Procom Technology, Inc.
5 * 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
12 * See the GNU General Public License for more details.
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>
27 static int llc_mac_header_len(unsigned short devtype)
29 switch (devtype) {
30 case ARPHRD_ETHER:
31 case ARPHRD_LOOPBACK:
32 return sizeof(struct ethhdr);
33 #ifdef CONFIG_TR
34 case ARPHRD_IEEE802_TR:
35 return sizeof(struct trh_hdr);
36 #endif
38 return 0;
41 /**
42 * llc_alloc_frame - allocates sk_buff for frame
43 * @dev: network device this skb will be sent over
44 * @type: pdu type to allocate
45 * @data_size: data size to allocate
47 * Allocates an sk_buff for frame and initializes sk_buff fields.
48 * Returns allocated skb or %NULL when out of memory.
50 struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev,
51 u8 type, u32 data_size)
53 int hlen = type == LLC_PDU_TYPE_U ? 3 : 4;
54 struct sk_buff *skb;
56 hlen += llc_mac_header_len(dev->type);
57 skb = alloc_skb(hlen + data_size, GFP_ATOMIC);
59 if (skb) {
60 skb_reset_mac_header(skb);
61 skb_reserve(skb, hlen);
62 skb_reset_network_header(skb);
63 skb_reset_transport_header(skb);
64 skb->protocol = htons(ETH_P_802_2);
65 skb->dev = dev;
66 if (sk != NULL)
67 skb_set_owner_w(skb, sk);
69 return skb;
72 void llc_save_primitive(struct sock *sk, struct sk_buff* skb, u8 prim)
74 struct sockaddr_llc *addr;
76 /* save primitive for use by the user. */
77 addr = llc_ui_skb_cb(skb);
79 memset(addr, 0, sizeof(*addr));
80 addr->sllc_family = sk->sk_family;
81 addr->sllc_arphrd = skb->dev->type;
82 addr->sllc_test = prim == LLC_TEST_PRIM;
83 addr->sllc_xid = prim == LLC_XID_PRIM;
84 addr->sllc_ua = prim == LLC_DATAUNIT_PRIM;
85 llc_pdu_decode_sa(skb, addr->sllc_mac);
86 llc_pdu_decode_ssap(skb, &addr->sllc_sap);
89 /**
90 * llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu.
91 * @sap: pointer to SAP
92 * @skb: received pdu
94 void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb)
96 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
97 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
99 switch (LLC_U_PDU_RSP(pdu)) {
100 case LLC_1_PDU_CMD_TEST:
101 ev->prim = LLC_TEST_PRIM; break;
102 case LLC_1_PDU_CMD_XID:
103 ev->prim = LLC_XID_PRIM; break;
104 case LLC_1_PDU_CMD_UI:
105 ev->prim = LLC_DATAUNIT_PRIM; break;
107 ev->ind_cfm_flag = LLC_IND;
111 * llc_find_sap_trans - finds transition for event
112 * @sap: pointer to SAP
113 * @skb: happened event
115 * This function finds transition that matches with happened event.
116 * Returns the pointer to found transition on success or %NULL for
117 * failure.
119 static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap,
120 struct sk_buff* skb)
122 int i = 0;
123 struct llc_sap_state_trans *rc = NULL;
124 struct llc_sap_state_trans **next_trans;
125 struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1];
127 * Search thru events for this state until list exhausted or until
128 * its obvious the event is not valid for the current state
130 for (next_trans = curr_state->transitions; next_trans[i]->ev; i++)
131 if (!next_trans[i]->ev(sap, skb)) {
132 rc = next_trans[i]; /* got event match; return it */
133 break;
135 return rc;
139 * llc_exec_sap_trans_actions - execute actions related to event
140 * @sap: pointer to SAP
141 * @trans: pointer to transition that it's actions must be performed
142 * @skb: happened event.
144 * This function executes actions that is related to happened event.
145 * Returns 0 for success and 1 for failure of at least one action.
147 static int llc_exec_sap_trans_actions(struct llc_sap *sap,
148 struct llc_sap_state_trans *trans,
149 struct sk_buff *skb)
151 int rc = 0;
152 llc_sap_action_t *next_action = trans->ev_actions;
154 for (; next_action && *next_action; next_action++)
155 if ((*next_action)(sap, skb))
156 rc = 1;
157 return rc;
161 * llc_sap_next_state - finds transition, execs actions & change SAP state
162 * @sap: pointer to SAP
163 * @skb: happened event
165 * This function finds transition that matches with happened event, then
166 * executes related actions and finally changes state of SAP. It returns
167 * 0 on success and 1 for failure.
169 static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb)
171 int rc = 1;
172 struct llc_sap_state_trans *trans;
174 if (sap->state > LLC_NR_SAP_STATES)
175 goto out;
176 trans = llc_find_sap_trans(sap, skb);
177 if (!trans)
178 goto out;
180 * Got the state to which we next transition; perform the actions
181 * associated with this transition before actually transitioning to the
182 * next state
184 rc = llc_exec_sap_trans_actions(sap, trans, skb);
185 if (rc)
186 goto out;
188 * Transition SAP to next state if all actions execute successfully
190 sap->state = trans->next_state;
191 out:
192 return rc;
196 * llc_sap_state_process - sends event to SAP state machine
197 * @sap: sap to use
198 * @skb: pointer to occurred event
200 * After executing actions of the event, upper layer will be indicated
201 * if needed(on receiving an UI frame). sk can be null for the
202 * datalink_proto case.
204 static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
206 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
209 * We have to hold the skb, because llc_sap_next_state
210 * will kfree it in the sending path and we need to
211 * look at the skb->cb, where we encode llc_sap_state_ev.
213 skb_get(skb);
214 ev->ind_cfm_flag = 0;
215 llc_sap_next_state(sap, skb);
216 if (ev->ind_cfm_flag == LLC_IND) {
217 if (skb->sk->sk_state == TCP_LISTEN)
218 kfree_skb(skb);
219 else {
220 llc_save_primitive(skb->sk, skb, ev->prim);
222 /* queue skb to the user. */
223 if (sock_queue_rcv_skb(skb->sk, skb))
224 kfree_skb(skb);
227 kfree_skb(skb);
231 * llc_build_and_send_test_pkt - TEST interface for upper layers.
232 * @sap: sap to use
233 * @skb: packet to send
234 * @dmac: destination mac address
235 * @dsap: destination sap
237 * This function is called when upper layer wants to send a TEST pdu.
238 * Returns 0 for success, 1 otherwise.
240 void llc_build_and_send_test_pkt(struct llc_sap *sap,
241 struct sk_buff *skb, u8 *dmac, u8 dsap)
243 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
245 ev->saddr.lsap = sap->laddr.lsap;
246 ev->daddr.lsap = dsap;
247 memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
248 memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
250 ev->type = LLC_SAP_EV_TYPE_PRIM;
251 ev->prim = LLC_TEST_PRIM;
252 ev->prim_type = LLC_PRIM_TYPE_REQ;
253 llc_sap_state_process(sap, skb);
257 * llc_build_and_send_xid_pkt - XID interface for upper layers
258 * @sap: sap to use
259 * @skb: packet to send
260 * @dmac: destination mac address
261 * @dsap: destination sap
263 * This function is called when upper layer wants to send a XID pdu.
264 * Returns 0 for success, 1 otherwise.
266 void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
267 u8 *dmac, u8 dsap)
269 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
271 ev->saddr.lsap = sap->laddr.lsap;
272 ev->daddr.lsap = dsap;
273 memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
274 memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
276 ev->type = LLC_SAP_EV_TYPE_PRIM;
277 ev->prim = LLC_XID_PRIM;
278 ev->prim_type = LLC_PRIM_TYPE_REQ;
279 llc_sap_state_process(sap, skb);
283 * llc_sap_rcv - sends received pdus to the sap state machine
284 * @sap: current sap component structure.
285 * @skb: received frame.
287 * Sends received pdus to the sap state machine.
289 static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
290 struct sock *sk)
292 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
294 ev->type = LLC_SAP_EV_TYPE_PDU;
295 ev->reason = 0;
296 skb->sk = sk;
297 llc_sap_state_process(sap, skb);
301 * llc_lookup_dgram - Finds dgram socket for the local sap/mac
302 * @sap: SAP
303 * @laddr: address of local LLC (MAC + SAP)
305 * Search socket list of the SAP and finds connection using the local
306 * mac, and local sap. Returns pointer for socket found, %NULL otherwise.
308 static struct sock *llc_lookup_dgram(struct llc_sap *sap,
309 const struct llc_addr *laddr)
311 struct sock *rc;
312 struct hlist_node *node;
314 read_lock_bh(&sap->sk_list.lock);
315 sk_for_each(rc, node, &sap->sk_list.list) {
316 struct llc_sock *llc = llc_sk(rc);
318 if (rc->sk_type == SOCK_DGRAM &&
319 llc->laddr.lsap == laddr->lsap &&
320 llc_mac_match(llc->laddr.mac, laddr->mac)) {
321 sock_hold(rc);
322 goto found;
325 rc = NULL;
326 found:
327 read_unlock_bh(&sap->sk_list.lock);
328 return rc;
332 * llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
333 * @sap: SAP
334 * @laddr: address of local LLC (MAC + SAP)
336 * Search socket list of the SAP and finds connections with same sap.
337 * Deliver clone to each.
339 static void llc_sap_mcast(struct llc_sap *sap,
340 const struct llc_addr *laddr,
341 struct sk_buff *skb)
343 struct sock *sk;
344 struct hlist_node *node;
346 read_lock_bh(&sap->sk_list.lock);
347 sk_for_each(sk, node, &sap->sk_list.list) {
348 struct llc_sock *llc = llc_sk(sk);
349 struct sk_buff *skb1;
351 if (sk->sk_type != SOCK_DGRAM)
352 continue;
354 if (llc->laddr.lsap != laddr->lsap)
355 continue;
357 if (llc->dev != skb->dev)
358 continue;
360 skb1 = skb_clone(skb, GFP_ATOMIC);
361 if (!skb1)
362 break;
364 sock_hold(sk);
365 llc_sap_rcv(sap, skb1, sk);
366 sock_put(sk);
368 read_unlock_bh(&sap->sk_list.lock);
372 void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
374 struct llc_addr laddr;
376 llc_pdu_decode_da(skb, laddr.mac);
377 llc_pdu_decode_dsap(skb, &laddr.lsap);
379 if (llc_mac_multicast(laddr.mac)) {
380 llc_sap_mcast(sap, &laddr, skb);
381 kfree_skb(skb);
382 } else {
383 struct sock *sk = llc_lookup_dgram(sap, &laddr);
384 if (sk) {
385 llc_sap_rcv(sap, skb, sk);
386 sock_put(sk);
387 } else
388 kfree_skb(skb);