blob: 7c5073badc7349ac6e8f74af69902526fb66bda5 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Joonwoo Parkf83f1762008-03-31 21:02:47 -070028static int llc_mac_header_len(unsigned short devtype)
29{
30 switch (devtype) {
31 case ARPHRD_ETHER:
32 case ARPHRD_LOOPBACK:
33 return sizeof(struct ethhdr);
Joonwoo Parkf83f1762008-03-31 21:02:47 -070034 }
35 return 0;
36}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/**
39 * llc_alloc_frame - allocates sk_buff for frame
Arnaldo Carvalho de Melo1d67e652005-09-22 03:27:56 -030040 * @dev: network device this skb will be sent over
Joonwoo Parkf83f1762008-03-31 21:02:47 -070041 * @type: pdu type to allocate
42 * @data_size: data size to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 * Allocates an sk_buff for frame and initializes sk_buff fields.
45 * Returns allocated skb or %NULL when out of memory.
46 */
Joonwoo Parkf83f1762008-03-31 21:02:47 -070047struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev,
48 u8 type, u32 data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Joonwoo Parkf83f1762008-03-31 21:02:47 -070050 int hlen = type == LLC_PDU_TYPE_U ? 3 : 4;
51 struct sk_buff *skb;
52
53 hlen += llc_mac_header_len(dev->type);
54 skb = alloc_skb(hlen + data_size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 if (skb) {
Arnaldo Carvalho de Melo0a1b0ad2007-03-10 12:14:56 -030057 skb_reset_mac_header(skb);
Joonwoo Parkf83f1762008-03-31 21:02:47 -070058 skb_reserve(skb, hlen);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070059 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -030060 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 skb->protocol = htons(ETH_P_802_2);
Arnaldo Carvalho de Melo1d67e652005-09-22 03:27:56 -030062 skb->dev = dev;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030063 if (sk != NULL)
64 skb_set_owner_w(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66 return skb;
67}
68
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030069void llc_save_primitive(struct sock *sk, struct sk_buff* skb, u8 prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -030071 struct sockaddr_llc *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /* save primitive for use by the user. */
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -030074 addr = llc_ui_skb_cb(skb);
Stephen Hemminger30a584d2006-08-03 16:38:49 -070075
76 memset(addr, 0, sizeof(*addr));
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030077 addr->sllc_family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 addr->sllc_arphrd = skb->dev->type;
79 addr->sllc_test = prim == LLC_TEST_PRIM;
80 addr->sllc_xid = prim == LLC_XID_PRIM;
81 addr->sllc_ua = prim == LLC_DATAUNIT_PRIM;
82 llc_pdu_decode_sa(skb, addr->sllc_mac);
83 llc_pdu_decode_ssap(skb, &addr->sllc_sap);
84}
85
86/**
87 * llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu.
88 * @sap: pointer to SAP
89 * @skb: received pdu
90 */
91void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb)
92{
93 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
94 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
95
96 switch (LLC_U_PDU_RSP(pdu)) {
97 case LLC_1_PDU_CMD_TEST:
98 ev->prim = LLC_TEST_PRIM; break;
99 case LLC_1_PDU_CMD_XID:
100 ev->prim = LLC_XID_PRIM; break;
101 case LLC_1_PDU_CMD_UI:
102 ev->prim = LLC_DATAUNIT_PRIM; break;
103 }
104 ev->ind_cfm_flag = LLC_IND;
105}
106
107/**
108 * llc_find_sap_trans - finds transition for event
109 * @sap: pointer to SAP
110 * @skb: happened event
111 *
112 * This function finds transition that matches with happened event.
113 * Returns the pointer to found transition on success or %NULL for
114 * failure.
115 */
116static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap,
117 struct sk_buff* skb)
118{
119 int i = 0;
120 struct llc_sap_state_trans *rc = NULL;
121 struct llc_sap_state_trans **next_trans;
122 struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1];
123 /*
124 * Search thru events for this state until list exhausted or until
125 * its obvious the event is not valid for the current state
126 */
127 for (next_trans = curr_state->transitions; next_trans[i]->ev; i++)
128 if (!next_trans[i]->ev(sap, skb)) {
129 rc = next_trans[i]; /* got event match; return it */
130 break;
131 }
132 return rc;
133}
134
135/**
136 * llc_exec_sap_trans_actions - execute actions related to event
137 * @sap: pointer to SAP
138 * @trans: pointer to transition that it's actions must be performed
139 * @skb: happened event.
140 *
141 * This function executes actions that is related to happened event.
142 * Returns 0 for success and 1 for failure of at least one action.
143 */
144static int llc_exec_sap_trans_actions(struct llc_sap *sap,
145 struct llc_sap_state_trans *trans,
146 struct sk_buff *skb)
147{
148 int rc = 0;
149 llc_sap_action_t *next_action = trans->ev_actions;
150
151 for (; next_action && *next_action; next_action++)
152 if ((*next_action)(sap, skb))
153 rc = 1;
154 return rc;
155}
156
157/**
158 * llc_sap_next_state - finds transition, execs actions & change SAP state
159 * @sap: pointer to SAP
160 * @skb: happened event
161 *
162 * This function finds transition that matches with happened event, then
163 * executes related actions and finally changes state of SAP. It returns
164 * 0 on success and 1 for failure.
165 */
166static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb)
167{
168 int rc = 1;
169 struct llc_sap_state_trans *trans;
170
171 if (sap->state > LLC_NR_SAP_STATES)
172 goto out;
173 trans = llc_find_sap_trans(sap, skb);
174 if (!trans)
175 goto out;
176 /*
177 * Got the state to which we next transition; perform the actions
178 * associated with this transition before actually transitioning to the
179 * next state
180 */
181 rc = llc_exec_sap_trans_actions(sap, trans, skb);
182 if (rc)
183 goto out;
184 /*
185 * Transition SAP to next state if all actions execute successfully
186 */
187 sap->state = trans->next_state;
188out:
189 return rc;
190}
191
192/**
193 * llc_sap_state_process - sends event to SAP state machine
194 * @sap: sap to use
195 * @skb: pointer to occurred event
196 *
197 * After executing actions of the event, upper layer will be indicated
198 * if needed(on receiving an UI frame). sk can be null for the
199 * datalink_proto case.
200 */
201static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
202{
203 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
204
205 /*
206 * We have to hold the skb, because llc_sap_next_state
207 * will kfree it in the sending path and we need to
208 * look at the skb->cb, where we encode llc_sap_state_ev.
209 */
210 skb_get(skb);
211 ev->ind_cfm_flag = 0;
212 llc_sap_next_state(sap, skb);
213 if (ev->ind_cfm_flag == LLC_IND) {
214 if (skb->sk->sk_state == TCP_LISTEN)
215 kfree_skb(skb);
216 else {
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300217 llc_save_primitive(skb->sk, skb, ev->prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* queue skb to the user. */
220 if (sock_queue_rcv_skb(skb->sk, skb))
221 kfree_skb(skb);
222 }
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 kfree_skb(skb);
225}
226
227/**
228 * llc_build_and_send_test_pkt - TEST interface for upper layers.
229 * @sap: sap to use
230 * @skb: packet to send
231 * @dmac: destination mac address
232 * @dsap: destination sap
233 *
234 * This function is called when upper layer wants to send a TEST pdu.
235 * Returns 0 for success, 1 otherwise.
236 */
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900237void llc_build_and_send_test_pkt(struct llc_sap *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct sk_buff *skb, u8 *dmac, u8 dsap)
239{
240 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
241
242 ev->saddr.lsap = sap->laddr.lsap;
243 ev->daddr.lsap = dsap;
244 memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
245 memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ev->type = LLC_SAP_EV_TYPE_PRIM;
248 ev->prim = LLC_TEST_PRIM;
249 ev->prim_type = LLC_PRIM_TYPE_REQ;
250 llc_sap_state_process(sap, skb);
251}
252
253/**
254 * llc_build_and_send_xid_pkt - XID interface for upper layers
255 * @sap: sap to use
256 * @skb: packet to send
257 * @dmac: destination mac address
258 * @dsap: destination sap
259 *
260 * This function is called when upper layer wants to send a XID pdu.
261 * Returns 0 for success, 1 otherwise.
262 */
263void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
264 u8 *dmac, u8 dsap)
265{
266 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
267
268 ev->saddr.lsap = sap->laddr.lsap;
269 ev->daddr.lsap = dsap;
270 memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
271 memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
272
273 ev->type = LLC_SAP_EV_TYPE_PRIM;
274 ev->prim = LLC_XID_PRIM;
275 ev->prim_type = LLC_PRIM_TYPE_REQ;
276 llc_sap_state_process(sap, skb);
277}
278
279/**
280 * llc_sap_rcv - sends received pdus to the sap state machine
281 * @sap: current sap component structure.
282 * @skb: received frame.
283 *
284 * Sends received pdus to the sap state machine.
285 */
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700286static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
287 struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
290
291 ev->type = LLC_SAP_EV_TYPE_PDU;
292 ev->reason = 0;
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700293 skb->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 llc_sap_state_process(sap, skb);
295}
296
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000297static inline bool llc_dgram_match(const struct llc_sap *sap,
298 const struct llc_addr *laddr,
299 const struct sock *sk)
300{
301 struct llc_sock *llc = llc_sk(sk);
302
303 return sk->sk_type == SOCK_DGRAM &&
304 llc->laddr.lsap == laddr->lsap &&
305 llc_mac_match(llc->laddr.mac, laddr->mac);
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/**
309 * llc_lookup_dgram - Finds dgram socket for the local sap/mac
310 * @sap: SAP
311 * @laddr: address of local LLC (MAC + SAP)
312 *
313 * Search socket list of the SAP and finds connection using the local
314 * mac, and local sap. Returns pointer for socket found, %NULL otherwise.
315 */
316static struct sock *llc_lookup_dgram(struct llc_sap *sap,
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700317 const struct llc_addr *laddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct sock *rc;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000320 struct hlist_nulls_node *node;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000321 int slot = llc_sk_laddr_hashfn(sap, laddr);
322 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000324 rcu_read_lock_bh();
325again:
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000326 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000327 if (llc_dgram_match(sap, laddr, rc)) {
328 /* Extra checks required by SLAB_DESTROY_BY_RCU */
329 if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
330 goto again;
331 if (unlikely(llc_sk(rc)->sap != sap ||
332 !llc_dgram_match(sap, laddr, rc))) {
333 sock_put(rc);
334 continue;
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto found;
337 }
338 }
339 rc = NULL;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000340 /*
341 * if the nulls value we got at the end of this lookup is
342 * not the expected one, we must restart lookup.
343 * We probably met an item that was moved to another chain.
344 */
345 if (unlikely(get_nulls_value(node) != slot))
346 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347found:
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000348 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return rc;
350}
351
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000352static inline bool llc_mcast_match(const struct llc_sap *sap,
353 const struct llc_addr *laddr,
354 const struct sk_buff *skb,
355 const struct sock *sk)
356{
357 struct llc_sock *llc = llc_sk(sk);
358
359 return sk->sk_type == SOCK_DGRAM &&
360 llc->laddr.lsap == laddr->lsap &&
361 llc->dev == skb->dev;
362}
363
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000364static void llc_do_mcast(struct llc_sap *sap, struct sk_buff *skb,
365 struct sock **stack, int count)
366{
367 struct sk_buff *skb1;
368 int i;
369
370 for (i = 0; i < count; i++) {
371 skb1 = skb_clone(skb, GFP_ATOMIC);
372 if (!skb1) {
373 sock_put(stack[i]);
374 continue;
375 }
376
377 llc_sap_rcv(sap, skb1, stack[i]);
378 sock_put(stack[i]);
379 }
380}
381
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700382/**
383 * llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
384 * @sap: SAP
385 * @laddr: address of local LLC (MAC + SAP)
386 *
387 * Search socket list of the SAP and finds connections with same sap.
388 * Deliver clone to each.
389 */
390static void llc_sap_mcast(struct llc_sap *sap,
391 const struct llc_addr *laddr,
392 struct sk_buff *skb)
393{
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000394 int i = 0, count = 256 / sizeof(struct sock *);
395 struct sock *sk, *stack[count];
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000396 struct hlist_node *node;
397 struct llc_sock *llc;
398 struct hlist_head *dev_hb = llc_sk_dev_hash(sap, skb->dev->ifindex);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700399
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000400 spin_lock_bh(&sap->sk_lock);
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000401 hlist_for_each_entry(llc, node, dev_hb, dev_hash_node) {
402
403 sk = &llc->sk;
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700404
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000405 if (!llc_mcast_match(sap, laddr, skb, sk))
Stephen Hemminger7ee66fc2006-08-13 18:56:26 -0700406 continue;
407
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700408 sock_hold(sk);
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000409 if (i < count)
410 stack[i++] = sk;
411 else {
412 llc_do_mcast(sap, skb, stack, i);
413 i = 0;
414 }
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700415 }
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000416 spin_unlock_bh(&sap->sk_lock);
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000417
418 llc_do_mcast(sap, skb, stack, i);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700419}
420
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
423{
424 struct llc_addr laddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 llc_pdu_decode_da(skb, laddr.mac);
427 llc_pdu_decode_dsap(skb, &laddr.lsap);
428
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700429 if (llc_mac_multicast(laddr.mac)) {
430 llc_sap_mcast(sap, &laddr, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 kfree_skb(skb);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700432 } else {
433 struct sock *sk = llc_lookup_dgram(sap, &laddr);
434 if (sk) {
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700435 llc_sap_rcv(sap, skb, sk);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700436 sock_put(sk);
437 } else
438 kfree_skb(skb);
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}