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