blob: 6805ce43a055c6b1a66be3079a1d71c973883e75 [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
Andrew Lunn74c950c2020-07-13 01:15:04 +020040 * @sk: socket to allocate frame to
Arnaldo Carvalho de Melo1d67e6502005-09-22 03:27:56 -030041 * @dev: network device this skb will be sent over
Joonwoo Parkf83f1762008-03-31 21:02:47 -070042 * @type: pdu type to allocate
43 * @data_size: data size to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 * Allocates an sk_buff for frame and initializes sk_buff fields.
46 * Returns allocated skb or %NULL when out of memory.
47 */
Joonwoo Parkf83f1762008-03-31 21:02:47 -070048struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev,
49 u8 type, u32 data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Joonwoo Parkf83f1762008-03-31 21:02:47 -070051 int hlen = type == LLC_PDU_TYPE_U ? 3 : 4;
52 struct sk_buff *skb;
53
54 hlen += llc_mac_header_len(dev->type);
55 skb = alloc_skb(hlen + data_size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 if (skb) {
Arnaldo Carvalho de Melo0a1b0ad2007-03-10 12:14:56 -030058 skb_reset_mac_header(skb);
Joonwoo Parkf83f1762008-03-31 21:02:47 -070059 skb_reserve(skb, hlen);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070060 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -030061 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 skb->protocol = htons(ETH_P_802_2);
Arnaldo Carvalho de Melo1d67e6502005-09-22 03:27:56 -030063 skb->dev = dev;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030064 if (sk != NULL)
65 skb_set_owner_w(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67 return skb;
68}
69
Weilong Chen3cdba602013-12-20 11:14:59 +080070void llc_save_primitive(struct sock *sk, struct sk_buff *skb, u8 prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -030072 struct sockaddr_llc *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /* save primitive for use by the user. */
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -030075 addr = llc_ui_skb_cb(skb);
Stephen Hemminger30a584d2006-08-03 16:38:49 -070076
77 memset(addr, 0, sizeof(*addr));
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030078 addr->sllc_family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 addr->sllc_arphrd = skb->dev->type;
80 addr->sllc_test = prim == LLC_TEST_PRIM;
81 addr->sllc_xid = prim == LLC_XID_PRIM;
82 addr->sllc_ua = prim == LLC_DATAUNIT_PRIM;
83 llc_pdu_decode_sa(skb, addr->sllc_mac);
84 llc_pdu_decode_ssap(skb, &addr->sllc_sap);
85}
86
87/**
88 * llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu.
89 * @sap: pointer to SAP
90 * @skb: received pdu
91 */
92void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb)
93{
94 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
95 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
96
97 switch (LLC_U_PDU_RSP(pdu)) {
98 case LLC_1_PDU_CMD_TEST:
99 ev->prim = LLC_TEST_PRIM; break;
100 case LLC_1_PDU_CMD_XID:
101 ev->prim = LLC_XID_PRIM; break;
102 case LLC_1_PDU_CMD_UI:
103 ev->prim = LLC_DATAUNIT_PRIM; break;
104 }
105 ev->ind_cfm_flag = LLC_IND;
106}
107
108/**
109 * llc_find_sap_trans - finds transition for event
110 * @sap: pointer to SAP
111 * @skb: happened event
112 *
113 * This function finds transition that matches with happened event.
114 * Returns the pointer to found transition on success or %NULL for
115 * failure.
116 */
117static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap,
Weilong Chen3cdba602013-12-20 11:14:59 +0800118 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int i = 0;
121 struct llc_sap_state_trans *rc = NULL;
122 struct llc_sap_state_trans **next_trans;
123 struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1];
124 /*
125 * Search thru events for this state until list exhausted or until
126 * its obvious the event is not valid for the current state
127 */
128 for (next_trans = curr_state->transitions; next_trans[i]->ev; i++)
129 if (!next_trans[i]->ev(sap, skb)) {
130 rc = next_trans[i]; /* got event match; return it */
131 break;
132 }
133 return rc;
134}
135
136/**
137 * llc_exec_sap_trans_actions - execute actions related to event
138 * @sap: pointer to SAP
139 * @trans: pointer to transition that it's actions must be performed
140 * @skb: happened event.
141 *
142 * This function executes actions that is related to happened event.
143 * Returns 0 for success and 1 for failure of at least one action.
144 */
145static int llc_exec_sap_trans_actions(struct llc_sap *sap,
146 struct llc_sap_state_trans *trans,
147 struct sk_buff *skb)
148{
149 int rc = 0;
Joe Perches22bbf5f2014-12-10 09:55:50 -0800150 const llc_sap_action_t *next_action = trans->ev_actions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 for (; next_action && *next_action; next_action++)
153 if ((*next_action)(sap, skb))
154 rc = 1;
155 return rc;
156}
157
158/**
159 * llc_sap_next_state - finds transition, execs actions & change SAP state
160 * @sap: pointer to SAP
161 * @skb: happened event
162 *
163 * This function finds transition that matches with happened event, then
164 * executes related actions and finally changes state of SAP. It returns
165 * 0 on success and 1 for failure.
166 */
167static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb)
168{
169 int rc = 1;
170 struct llc_sap_state_trans *trans;
171
172 if (sap->state > LLC_NR_SAP_STATES)
173 goto out;
174 trans = llc_find_sap_trans(sap, skb);
175 if (!trans)
176 goto out;
177 /*
178 * Got the state to which we next transition; perform the actions
179 * associated with this transition before actually transitioning to the
180 * next state
181 */
182 rc = llc_exec_sap_trans_actions(sap, trans, skb);
183 if (rc)
184 goto out;
185 /*
186 * Transition SAP to next state if all actions execute successfully
187 */
188 sap->state = trans->next_state;
189out:
190 return rc;
191}
192
193/**
194 * llc_sap_state_process - sends event to SAP state machine
195 * @sap: sap to use
196 * @skb: pointer to occurred event
197 *
198 * After executing actions of the event, upper layer will be indicated
199 * if needed(on receiving an UI frame). sk can be null for the
200 * datalink_proto case.
Eric Biggersc6ee11c2019-10-06 14:24:24 -0700201 *
202 * This function always consumes a reference to the skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 ev->ind_cfm_flag = 0;
209 llc_sap_next_state(sap, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Eric Biggersc6ee11c2019-10-06 14:24:24 -0700211 if (ev->ind_cfm_flag == LLC_IND && skb->sk->sk_state != TCP_LISTEN) {
212 llc_save_primitive(skb->sk, skb, ev->prim);
213
214 /* queue skb to the user. */
215 if (sock_queue_rcv_skb(skb->sk, skb) == 0)
216 return;
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 kfree_skb(skb);
219}
220
221/**
222 * llc_build_and_send_test_pkt - TEST interface for upper layers.
223 * @sap: sap to use
224 * @skb: packet to send
225 * @dmac: destination mac address
226 * @dsap: destination sap
227 *
228 * This function is called when upper layer wants to send a TEST pdu.
229 * Returns 0 for success, 1 otherwise.
230 */
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900231void llc_build_and_send_test_pkt(struct llc_sap *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct sk_buff *skb, u8 *dmac, u8 dsap)
233{
234 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
235
236 ev->saddr.lsap = sap->laddr.lsap;
237 ev->daddr.lsap = dsap;
238 memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
239 memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 ev->type = LLC_SAP_EV_TYPE_PRIM;
242 ev->prim = LLC_TEST_PRIM;
243 ev->prim_type = LLC_PRIM_TYPE_REQ;
244 llc_sap_state_process(sap, skb);
245}
246
247/**
248 * llc_build_and_send_xid_pkt - XID interface for upper layers
249 * @sap: sap to use
250 * @skb: packet to send
251 * @dmac: destination mac address
252 * @dsap: destination sap
253 *
254 * This function is called when upper layer wants to send a XID pdu.
255 * Returns 0 for success, 1 otherwise.
256 */
257void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
258 u8 *dmac, u8 dsap)
259{
260 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
261
262 ev->saddr.lsap = sap->laddr.lsap;
263 ev->daddr.lsap = dsap;
264 memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
265 memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
266
267 ev->type = LLC_SAP_EV_TYPE_PRIM;
268 ev->prim = LLC_XID_PRIM;
269 ev->prim_type = LLC_PRIM_TYPE_REQ;
270 llc_sap_state_process(sap, skb);
271}
272
273/**
274 * llc_sap_rcv - sends received pdus to the sap state machine
275 * @sap: current sap component structure.
276 * @skb: received frame.
Andrew Lunn74c950c2020-07-13 01:15:04 +0200277 * @sk: socket to associate to frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 *
279 * Sends received pdus to the sap state machine.
280 */
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700281static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
282 struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
285
286 ev->type = LLC_SAP_EV_TYPE_PDU;
287 ev->reason = 0;
Eric Dumazet8b74d432017-02-12 14:03:52 -0800288 skb_orphan(skb);
289 sock_hold(sk);
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700290 skb->sk = sk;
Eric Dumazet8b74d432017-02-12 14:03:52 -0800291 skb->destructor = sock_efree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 llc_sap_state_process(sap, skb);
293}
294
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000295static inline bool llc_dgram_match(const struct llc_sap *sap,
296 const struct llc_addr *laddr,
297 const struct sock *sk)
298{
299 struct llc_sock *llc = llc_sk(sk);
300
301 return sk->sk_type == SOCK_DGRAM &&
302 llc->laddr.lsap == laddr->lsap &&
Joe Perches951fd872013-09-01 13:11:55 -0700303 ether_addr_equal(llc->laddr.mac, laddr->mac);
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/**
307 * llc_lookup_dgram - Finds dgram socket for the local sap/mac
308 * @sap: SAP
309 * @laddr: address of local LLC (MAC + SAP)
310 *
311 * Search socket list of the SAP and finds connection using the local
312 * mac, and local sap. Returns pointer for socket found, %NULL otherwise.
313 */
314static struct sock *llc_lookup_dgram(struct llc_sap *sap,
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700315 const struct llc_addr *laddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct sock *rc;
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000318 struct hlist_nulls_node *node;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000319 int slot = llc_sk_laddr_hashfn(sap, laddr);
320 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000322 rcu_read_lock_bh();
323again:
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000324 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000325 if (llc_dgram_match(sap, laddr, rc)) {
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800326 /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
Reshetova, Elena41c6d652017-06-30 13:08:01 +0300327 if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000328 goto again;
329 if (unlikely(llc_sk(rc)->sap != sap ||
330 !llc_dgram_match(sap, laddr, rc))) {
331 sock_put(rc);
332 continue;
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto found;
335 }
336 }
337 rc = NULL;
Octavian Purdila52d58ae2009-12-26 11:51:05 +0000338 /*
339 * if the nulls value we got at the end of this lookup is
340 * not the expected one, we must restart lookup.
341 * We probably met an item that was moved to another chain.
342 */
343 if (unlikely(get_nulls_value(node) != slot))
344 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345found:
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000346 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return rc;
348}
349
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000350static inline bool llc_mcast_match(const struct llc_sap *sap,
351 const struct llc_addr *laddr,
352 const struct sk_buff *skb,
353 const struct sock *sk)
354{
355 struct llc_sock *llc = llc_sk(sk);
356
357 return sk->sk_type == SOCK_DGRAM &&
358 llc->laddr.lsap == laddr->lsap &&
359 llc->dev == skb->dev;
360}
361
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000362static void llc_do_mcast(struct llc_sap *sap, struct sk_buff *skb,
363 struct sock **stack, int count)
364{
365 struct sk_buff *skb1;
366 int i;
367
368 for (i = 0; i < count; i++) {
369 skb1 = skb_clone(skb, GFP_ATOMIC);
370 if (!skb1) {
371 sock_put(stack[i]);
372 continue;
373 }
374
375 llc_sap_rcv(sap, skb1, stack[i]);
376 sock_put(stack[i]);
377 }
378}
379
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700380/**
381 * llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
382 * @sap: SAP
383 * @laddr: address of local LLC (MAC + SAP)
Andrew Lunn74c950c2020-07-13 01:15:04 +0200384 * @skb: PDU to deliver
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700385 *
386 * Search socket list of the SAP and finds connections with same sap.
387 * Deliver clone to each.
388 */
389static void llc_sap_mcast(struct llc_sap *sap,
390 const struct llc_addr *laddr,
391 struct sk_buff *skb)
392{
Salvatore Mesoraca678f4bd2018-03-11 22:12:04 +0100393 int i = 0;
394 struct sock *sk;
395 struct sock *stack[256 / sizeof(struct sock *)];
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000396 struct llc_sock *llc;
397 struct hlist_head *dev_hb = llc_sk_dev_hash(sap, skb->dev->ifindex);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700398
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000399 spin_lock_bh(&sap->sk_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800400 hlist_for_each_entry(llc, dev_hb, dev_hash_node) {
Octavian Purdila6d2e3ea2009-12-26 11:51:04 +0000401
402 sk = &llc->sk;
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700403
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000404 if (!llc_mcast_match(sap, laddr, skb, sk))
Stephen Hemminger7ee66fc2006-08-13 18:56:26 -0700405 continue;
406
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700407 sock_hold(sk);
Salvatore Mesoraca678f4bd2018-03-11 22:12:04 +0100408 if (i < ARRAY_SIZE(stack))
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000409 stack[i++] = sk;
410 else {
411 llc_do_mcast(sap, skb, stack, i);
412 i = 0;
413 }
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700414 }
Octavian Purdilab76f5a82009-12-26 11:51:02 +0000415 spin_unlock_bh(&sap->sk_lock);
Octavian Purdila0f7b67d2009-12-26 11:51:03 +0000416
417 llc_do_mcast(sap, skb, stack, i);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700418}
419
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
422{
423 struct llc_addr laddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 llc_pdu_decode_da(skb, laddr.mac);
426 llc_pdu_decode_dsap(skb, &laddr.lsap);
427
Joe Perches951fd872013-09-01 13:11:55 -0700428 if (is_multicast_ether_addr(laddr.mac)) {
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700429 llc_sap_mcast(sap, &laddr, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 kfree_skb(skb);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700431 } else {
432 struct sock *sk = llc_lookup_dgram(sap, &laddr);
433 if (sk) {
Arnaldo Carvalho de Melo3446b9d2008-05-30 02:57:29 -0700434 llc_sap_rcv(sap, skb, sk);
Stephen Hemmingerbc0e6462006-05-25 15:10:37 -0700435 sock_put(sk);
436 } else
437 kfree_skb(skb);
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}