blob: ad6e6e1cf22fce1eefa43d1726537d3b5bf644dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070024#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/llc.h>
26
Joonwoo Parkf83f1762008-03-31 21:02:47 -070027static int llc_mac_header_len(unsigned short devtype)
28{
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
37 }
38 return 0;
39}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/**
42 * llc_alloc_frame - allocates sk_buff for frame
Arnaldo Carvalho de Melo1d67e652005-09-22 03:27:56 -030043 * @dev: network device this skb will be sent over
Joonwoo Parkf83f1762008-03-31 21:02:47 -070044 * @type: pdu type to allocate
45 * @data_size: data size to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
47 * Allocates an sk_buff for frame and initializes sk_buff fields.
48 * Returns allocated skb or %NULL when out of memory.
49 */
Joonwoo Parkf83f1762008-03-31 21:02:47 -070050struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev,
51 u8 type, u32 data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Joonwoo Parkf83f1762008-03-31 21:02:47 -070053 int hlen = type == LLC_PDU_TYPE_U ? 3 : 4;
54 struct sk_buff *skb;
55
56 hlen += llc_mac_header_len(dev->type);
57 skb = alloc_skb(hlen + data_size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 if (skb) {
Arnaldo Carvalho de Melo0a1b0ad2007-03-10 12:14:56 -030060 skb_reset_mac_header(skb);
Joonwoo Parkf83f1762008-03-31 21:02:47 -070061 skb_reserve(skb, hlen);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070062 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -030063 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 skb->protocol = htons(ETH_P_802_2);
Arnaldo Carvalho de Melo1d67e652005-09-22 03:27:56 -030065 skb->dev = dev;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030066 if (sk != NULL)
67 skb_set_owner_w(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
69 return skb;
70}
71
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030072void llc_save_primitive(struct sock *sk, struct sk_buff* skb, u8 prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -030074 struct sockaddr_llc *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /* save primitive for use by the user. */
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -030077 addr = llc_ui_skb_cb(skb);
Stephen Hemminger30a584d2006-08-03 16:38:49 -070078
79 memset(addr, 0, sizeof(*addr));
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030080 addr->sllc_family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 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);
87}
88
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
93 */
94void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb)
95{
96 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
97 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
98
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;
106 }
107 ev->ind_cfm_flag = LLC_IND;
108}
109
110/**
111 * llc_find_sap_trans - finds transition for event
112 * @sap: pointer to SAP
113 * @skb: happened event
114 *
115 * This function finds transition that matches with happened event.
116 * Returns the pointer to found transition on success or %NULL for
117 * failure.
118 */
119static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap,
120 struct sk_buff* skb)
121{
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];
126 /*
127 * Search thru events for this state until list exhausted or until
128 * its obvious the event is not valid for the current state
129 */
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;
134 }
135 return rc;
136}
137
138/**
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.
143 *
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.
146 */
147static int llc_exec_sap_trans_actions(struct llc_sap *sap,
148 struct llc_sap_state_trans *trans,
149 struct sk_buff *skb)
150{
151 int rc = 0;
152 llc_sap_action_t *next_action = trans->ev_actions;
153
154 for (; next_action && *next_action; next_action++)
155 if ((*next_action)(sap, skb))
156 rc = 1;
157 return rc;
158}
159
160/**
161 * llc_sap_next_state - finds transition, execs actions & change SAP state
162 * @sap: pointer to SAP
163 * @skb: happened event
164 *
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.
168 */
169static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb)
170{
171 int rc = 1;
172 struct llc_sap_state_trans *trans;
173
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;
179 /*
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
183 */
184 rc = llc_exec_sap_trans_actions(sap, trans, skb);
185 if (rc)
186 goto out;
187 /*
188 * Transition SAP to next state if all actions execute successfully
189 */
190 sap->state = trans->next_state;
191out:
192 return rc;
193}
194
195/**
196 * llc_sap_state_process - sends event to SAP state machine
197 * @sap: sap to use
198 * @skb: pointer to occurred event
199 *
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.
203 */
204static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
205{
206 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
207
208 /*
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.
212 */
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 {
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300220 llc_save_primitive(skb->sk, skb, ev->prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* queue skb to the user. */
223 if (sock_queue_rcv_skb(skb->sk, skb))
224 kfree_skb(skb);
225 }
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 kfree_skb(skb);
228}
229
230/**
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
236 *
237 * This function is called when upper layer wants to send a TEST pdu.
238 * Returns 0 for success, 1 otherwise.
239 */
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900240void llc_build_and_send_test_pkt(struct llc_sap *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct sk_buff *skb, u8 *dmac, u8 dsap)
242{
243 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
244
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);
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 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);
254}
255
256/**
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
262 *
263 * This function is called when upper layer wants to send a XID pdu.
264 * Returns 0 for success, 1 otherwise.
265 */
266void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
267 u8 *dmac, u8 dsap)
268{
269 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
270
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);
275
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);
280}
281
282/**
283 * llc_sap_rcv - sends received pdus to the sap state machine
284 * @sap: current sap component structure.
285 * @skb: received frame.
286 *
287 * Sends received pdus to the sap state machine.
288 */
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700289static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
290 struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
293
294 ev->type = LLC_SAP_EV_TYPE_PDU;
295 ev->reason = 0;
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700296 skb->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 llc_sap_state_process(sap, skb);
298}
299
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000300static inline bool llc_dgram_match(const struct llc_sap *sap,
301 const struct llc_addr *laddr,
302 const struct sock *sk)
303{
304 struct llc_sock *llc = llc_sk(sk);
305
306 return sk->sk_type == SOCK_DGRAM &&
307 llc->laddr.lsap == laddr->lsap &&
308 llc_mac_match(llc->laddr.mac, laddr->mac);
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/**
312 * llc_lookup_dgram - Finds dgram socket for the local sap/mac
313 * @sap: SAP
314 * @laddr: address of local LLC (MAC + SAP)
315 *
316 * Search socket list of the SAP and finds connection using the local
317 * mac, and local sap. Returns pointer for socket found, %NULL otherwise.
318 */
319static struct sock *llc_lookup_dgram(struct llc_sap *sap,
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700320 const struct llc_addr *laddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 struct sock *rc;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000323 struct hlist_nulls_node *node;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000324 int slot = llc_sk_laddr_hashfn(sap, laddr);
325 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000327 rcu_read_lock_bh();
328again:
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000329 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000330 if (llc_dgram_match(sap, laddr, rc)) {
331 /* Extra checks required by SLAB_DESTROY_BY_RCU */
332 if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
333 goto again;
334 if (unlikely(llc_sk(rc)->sap != sap ||
335 !llc_dgram_match(sap, laddr, rc))) {
336 sock_put(rc);
337 continue;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 goto found;
340 }
341 }
342 rc = NULL;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000343 /*
344 * if the nulls value we got at the end of this lookup is
345 * not the expected one, we must restart lookup.
346 * We probably met an item that was moved to another chain.
347 */
348 if (unlikely(get_nulls_value(node) != slot))
349 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350found:
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000351 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return rc;
353}
354
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000355static inline bool llc_mcast_match(const struct llc_sap *sap,
356 const struct llc_addr *laddr,
357 const struct sk_buff *skb,
358 const struct sock *sk)
359{
360 struct llc_sock *llc = llc_sk(sk);
361
362 return sk->sk_type == SOCK_DGRAM &&
363 llc->laddr.lsap == laddr->lsap &&
364 llc->dev == skb->dev;
365}
366
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000367static void llc_do_mcast(struct llc_sap *sap, struct sk_buff *skb,
368 struct sock **stack, int count)
369{
370 struct sk_buff *skb1;
371 int i;
372
373 for (i = 0; i < count; i++) {
374 skb1 = skb_clone(skb, GFP_ATOMIC);
375 if (!skb1) {
376 sock_put(stack[i]);
377 continue;
378 }
379
380 llc_sap_rcv(sap, skb1, stack[i]);
381 sock_put(stack[i]);
382 }
383}
384
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700385/**
386 * llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
387 * @sap: SAP
388 * @laddr: address of local LLC (MAC + SAP)
389 *
390 * Search socket list of the SAP and finds connections with same sap.
391 * Deliver clone to each.
392 */
393static void llc_sap_mcast(struct llc_sap *sap,
394 const struct llc_addr *laddr,
395 struct sk_buff *skb)
396{
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000397 int i = 0, count = 256 / sizeof(struct sock *);
398 struct sock *sk, *stack[count];
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000399 struct hlist_node *node;
400 struct llc_sock *llc;
401 struct hlist_head *dev_hb = llc_sk_dev_hash(sap, skb->dev->ifindex);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700402
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000403 spin_lock_bh(&sap->sk_lock);
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000404 hlist_for_each_entry(llc, node, dev_hb, dev_hash_node) {
405
406 sk = &llc->sk;
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700407
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000408 if (!llc_mcast_match(sap, laddr, skb, sk))
Stephen Hemminger7ee66fc2006-08-13 18:56:26 -0700409 continue;
410
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700411 sock_hold(sk);
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000412 if (i < count)
413 stack[i++] = sk;
414 else {
415 llc_do_mcast(sap, skb, stack, i);
416 i = 0;
417 }
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700418 }
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000419 spin_unlock_bh(&sap->sk_lock);
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000420
421 llc_do_mcast(sap, skb, stack, i);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700422}
423
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
426{
427 struct llc_addr laddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 llc_pdu_decode_da(skb, laddr.mac);
430 llc_pdu_decode_dsap(skb, &laddr.lsap);
431
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700432 if (llc_mac_multicast(laddr.mac)) {
433 llc_sap_mcast(sap, &laddr, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 kfree_skb(skb);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700435 } else {
436 struct sock *sk = llc_lookup_dgram(sap, &laddr);
437 if (sk) {
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700438 llc_sap_rcv(sap, skb, sk);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700439 sock_put(sk);
440 } else
441 kfree_skb(skb);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}