1
2
3
4
5
6
7
8
9
10
11
12
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#include <linux/slab.h>
27
28static 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);
34 }
35 return 0;
36}
37
38
39
40
41
42
43
44
45
46
47
48struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev,
49 u8 type, u32 data_size)
50{
51 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);
56
57 if (skb) {
58 skb_reset_mac_header(skb);
59 skb_reserve(skb, hlen);
60 skb_reset_network_header(skb);
61 skb_reset_transport_header(skb);
62 skb->protocol = htons(ETH_P_802_2);
63 skb->dev = dev;
64 if (sk != NULL)
65 skb_set_owner_w(skb, sk);
66 }
67 return skb;
68}
69
70void llc_save_primitive(struct sock *sk, struct sk_buff *skb, u8 prim)
71{
72 struct sockaddr_llc *addr;
73
74
75 addr = llc_ui_skb_cb(skb);
76
77 memset(addr, 0, sizeof(*addr));
78 addr->sllc_family = sk->sk_family;
79 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
89
90
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
110
111
112
113
114
115
116
117static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap,
118 struct sk_buff *skb)
119{
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
126
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];
131 break;
132 }
133 return rc;
134}
135
136
137
138
139
140
141
142
143
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;
150 const llc_sap_action_t *next_action = trans->ev_actions;
151
152 for (; next_action && *next_action; next_action++)
153 if ((*next_action)(sap, skb))
154 rc = 1;
155 return rc;
156}
157
158
159
160
161
162
163
164
165
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
179
180
181
182 rc = llc_exec_sap_trans_actions(sap, trans, skb);
183 if (rc)
184 goto out;
185
186
187
188 sap->state = trans->next_state;
189out:
190 return rc;
191}
192
193
194
195
196
197
198
199
200
201
202
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 ev->ind_cfm_flag = 0;
209 llc_sap_next_state(sap, skb);
210
211 if (ev->ind_cfm_flag == LLC_IND && skb->sk->sk_state != TCP_LISTEN) {
212 llc_save_primitive(skb->sk, skb, ev->prim);
213
214
215 if (sock_queue_rcv_skb(skb->sk, skb) == 0)
216 return;
217 }
218 kfree_skb(skb);
219}
220
221
222
223
224
225
226
227
228
229
230
231void llc_build_and_send_test_pkt(struct llc_sap *sap,
232 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);
240
241 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
249
250
251
252
253
254
255
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
275
276
277
278
279
280
281static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
282 struct sock *sk)
283{
284 struct llc_sap_state_ev *ev = llc_sap_ev(skb);
285
286 ev->type = LLC_SAP_EV_TYPE_PDU;
287 ev->reason = 0;
288 skb_orphan(skb);
289 sock_hold(sk);
290 skb->sk = sk;
291 skb->destructor = sock_efree;
292 llc_sap_state_process(sap, skb);
293}
294
295static 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 &&
303 ether_addr_equal(llc->laddr.mac, laddr->mac);
304}
305
306
307
308
309
310
311
312
313
314static struct sock *llc_lookup_dgram(struct llc_sap *sap,
315 const struct llc_addr *laddr)
316{
317 struct sock *rc;
318 struct hlist_nulls_node *node;
319 int slot = llc_sk_laddr_hashfn(sap, laddr);
320 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
321
322 rcu_read_lock_bh();
323again:
324 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
325 if (llc_dgram_match(sap, laddr, rc)) {
326
327 if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
328 goto again;
329 if (unlikely(llc_sk(rc)->sap != sap ||
330 !llc_dgram_match(sap, laddr, rc))) {
331 sock_put(rc);
332 continue;
333 }
334 goto found;
335 }
336 }
337 rc = NULL;
338
339
340
341
342
343 if (unlikely(get_nulls_value(node) != slot))
344 goto again;
345found:
346 rcu_read_unlock_bh();
347 return rc;
348}
349
350static 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
362static 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
380
381
382
383
384
385
386
387
388
389static void llc_sap_mcast(struct llc_sap *sap,
390 const struct llc_addr *laddr,
391 struct sk_buff *skb)
392{
393 int i = 0;
394 struct sock *sk;
395 struct sock *stack[256 / sizeof(struct sock *)];
396 struct llc_sock *llc;
397 struct hlist_head *dev_hb = llc_sk_dev_hash(sap, skb->dev->ifindex);
398
399 spin_lock_bh(&sap->sk_lock);
400 hlist_for_each_entry(llc, dev_hb, dev_hash_node) {
401
402 sk = &llc->sk;
403
404 if (!llc_mcast_match(sap, laddr, skb, sk))
405 continue;
406
407 sock_hold(sk);
408 if (i < ARRAY_SIZE(stack))
409 stack[i++] = sk;
410 else {
411 llc_do_mcast(sap, skb, stack, i);
412 i = 0;
413 }
414 }
415 spin_unlock_bh(&sap->sk_lock);
416
417 llc_do_mcast(sap, skb, stack, i);
418}
419
420
421void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
422{
423 struct llc_addr laddr;
424
425 llc_pdu_decode_da(skb, laddr.mac);
426 llc_pdu_decode_dsap(skb, &laddr.lsap);
427
428 if (is_multicast_ether_addr(laddr.mac)) {
429 llc_sap_mcast(sap, &laddr, skb);
430 kfree_skb(skb);
431 } else {
432 struct sock *sk = llc_lookup_dgram(sap, &laddr);
433 if (sk) {
434 llc_sap_rcv(sap, skb, sk);
435 sock_put(sk);
436 } else
437 kfree_skb(skb);
438 }
439}
440