1/*
2 * llc_input.c - Minimal input path for LLC
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#include <linux/netdevice.h>
15#include <net/llc.h>
16#include <net/llc_pdu.h>
17#include <net/llc_sap.h>
18
19#define dprintk(args...)
20
21/*
22 * Packet handler for the station, registerable because in the minimal
23 * LLC core that is taking shape only the very minimal subset of LLC that
24 * is needed for things like IPX, Appletalk, etc will stay, with all the
25 * rest in the llc1 and llc2 modules.
26 */
27static void (*llc_station_handler)(struct sk_buff *skb);
28
29/*
30 * Packet handlers for LLC_DEST_SAP and LLC_DEST_CONN.
31 */
32static void (*llc_type_handlers[2])(struct llc_sap *sap,
33				    struct sk_buff *skb);
34
35void llc_add_pack(int type, void (*handler)(struct llc_sap *sap,
36					    struct sk_buff *skb))
37{
38	if (type == LLC_DEST_SAP || type == LLC_DEST_CONN)
39		llc_type_handlers[type - 1] = handler;
40}
41
42void llc_remove_pack(int type)
43{
44	if (type == LLC_DEST_SAP || type == LLC_DEST_CONN)
45		llc_type_handlers[type - 1] = NULL;
46}
47
48void llc_set_station_handler(void (*handler)(struct sk_buff *skb))
49{
50	llc_station_handler = handler;
51}
52
53/**
54 *	llc_pdu_type - returns which LLC component must handle for PDU
55 *	@skb: input skb
56 *
57 *	This function returns which LLC component must handle this PDU.
58 */
59static __inline__ int llc_pdu_type(struct sk_buff *skb)
60{
61	int type = LLC_DEST_CONN; /* I-PDU or S-PDU type */
62	struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
63
64	if ((pdu->ctrl_1 & LLC_PDU_TYPE_MASK) != LLC_PDU_TYPE_U)
65		goto out;
66	switch (LLC_U_PDU_CMD(pdu)) {
67	case LLC_1_PDU_CMD_XID:
68	case LLC_1_PDU_CMD_UI:
69	case LLC_1_PDU_CMD_TEST:
70		type = LLC_DEST_SAP;
71		break;
72	case LLC_2_PDU_CMD_SABME:
73	case LLC_2_PDU_CMD_DISC:
74	case LLC_2_PDU_RSP_UA:
75	case LLC_2_PDU_RSP_DM:
76	case LLC_2_PDU_RSP_FRMR:
77		break;
78	default:
79		type = LLC_DEST_INVALID;
80		break;
81	}
82out:
83	return type;
84}
85
86/**
87 *	llc_fixup_skb - initializes skb pointers
88 *	@skb: This argument points to incoming skb
89 *
90 *	Initializes internal skb pointer to start of network layer by deriving
91 *	length of LLC header; finds length of LLC control field in LLC header
92 *	by looking at the two lowest-order bits of the first control field
93 *	byte; field is either 3 or 4 bytes long.
94 */
95static inline int llc_fixup_skb(struct sk_buff *skb)
96{
97	u8 llc_len = 2;
98	struct llc_pdu_un *pdu;
99
100	if (unlikely(!pskb_may_pull(skb, sizeof(*pdu))))
101		return 0;
102
103	pdu = (struct llc_pdu_un *)skb->data;
104	if ((pdu->ctrl_1 & LLC_PDU_TYPE_MASK) == LLC_PDU_TYPE_U)
105		llc_len = 1;
106	llc_len += 2;
107
108	if (unlikely(!pskb_may_pull(skb, llc_len)))
109		return 0;
110
111	skb->transport_header += llc_len;
112	skb_pull(skb, llc_len);
113	if (skb->protocol == htons(ETH_P_802_2)) {
114		__be16 pdulen = eth_hdr(skb)->h_proto;
115		u16 data_size = ntohs(pdulen) - llc_len;
116
117		if (unlikely(pskb_trim_rcsum(skb, data_size)))
118			return 0;
119	}
120	return 1;
121}
122
123/**
124 *	llc_rcv - 802.2 entry point from net lower layers
125 *	@skb: received pdu
126 *	@dev: device that receive pdu
127 *	@pt: packet type
128 *
129 *	When the system receives a 802.2 frame this function is called. It
130 *	checks SAP and connection of received pdu and passes frame to
131 *	llc_{station,sap,conn}_rcv for sending to proper state machine. If
132 *	the frame is related to a busy connection (a connection is sending
133 *	data now), it queues this frame in the connection's backlog.
134 */
135int llc_rcv(struct sk_buff *skb, struct net_device *dev,
136	    struct packet_type *pt, struct net_device *orig_dev)
137{
138	struct llc_sap *sap;
139	struct llc_pdu_sn *pdu;
140	int dest;
141	int (*rcv)(struct sk_buff *, struct net_device *,
142		   struct packet_type *, struct net_device *);
143
144	/*
145	 * When the interface is in promisc. mode, drop all the crap that it
146	 * receives, do not try to analyse it.
147	 */
148	if (unlikely(skb->pkt_type == PACKET_OTHERHOST)) {
149		dprintk("%s: PACKET_OTHERHOST\n", __FUNCTION__);
150		goto drop;
151	}
152	skb = skb_share_check(skb, GFP_ATOMIC);
153	if (unlikely(!skb))
154		goto out;
155	if (unlikely(!llc_fixup_skb(skb)))
156		goto drop;
157	pdu = llc_pdu_sn_hdr(skb);
158	if (unlikely(!pdu->dsap)) /* NULL DSAP, refer to station */
159	       goto handle_station;
160	sap = llc_sap_find(pdu->dsap);
161	if (unlikely(!sap)) {/* unknown SAP */
162		dprintk("%s: llc_sap_find(%02X) failed!\n", __FUNCTION__,
163			pdu->dsap);
164		goto drop;
165	}
166	/*
167	 * First the upper layer protocols that don't need the full
168	 * LLC functionality
169	 */
170	rcv = rcu_dereference(sap->rcv_func);
171	if (rcv) {
172		struct sk_buff *cskb = skb_clone(skb, GFP_ATOMIC);
173		if (cskb)
174			rcv(cskb, dev, pt, orig_dev);
175	}
176	dest = llc_pdu_type(skb);
177	if (unlikely(!dest || !llc_type_handlers[dest - 1]))
178		goto drop_put;
179	llc_type_handlers[dest - 1](sap, skb);
180out_put:
181	llc_sap_put(sap);
182out:
183	return 0;
184drop:
185	kfree_skb(skb);
186	goto out;
187drop_put:
188	kfree_skb(skb);
189	goto out_put;
190handle_station:
191	if (!llc_station_handler)
192		goto drop;
193	llc_station_handler(skb);
194	goto out;
195}
196
197EXPORT_SYMBOL(llc_add_pack);
198EXPORT_SYMBOL(llc_remove_pack);
199EXPORT_SYMBOL(llc_set_station_handler);
200