llc_input.c 5.88 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * llc_input.c - Minimal input path for LLC
 *
 * Copyright (c) 1997 by Procom Technology, Inc.
 * 		 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 *
 * This program can be redistributed or modified under the terms of the
 * GNU General Public License as published by the Free Software Foundation.
 * This program is distributed without any warranty or implied warranty
 * of merchantability or fitness for a particular purpose.
 *
 * See the GNU General Public License for more details.
 */
#include <linux/netdevice.h>
15
#include <linux/slab.h>
16
#include <linux/export.h>
17
#include <net/net_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <net/llc.h>
#include <net/llc_pdu.h>
#include <net/llc_sap.h>

#if 0
#define dprintk(args...) printk(KERN_DEBUG args)
#else
#define dprintk(args...)
#endif

/*
 * Packet handler for the station, registerable because in the minimal
 * LLC core that is taking shape only the very minimal subset of LLC that
 * is needed for things like IPX, Appletalk, etc will stay, with all the
 * rest in the llc1 and llc2 modules.
 */
static void (*llc_station_handler)(struct sk_buff *skb);

/*
 * Packet handlers for LLC_DEST_SAP and LLC_DEST_CONN.
 */
static void (*llc_type_handlers[2])(struct llc_sap *sap,
				    struct sk_buff *skb);

void llc_add_pack(int type, void (*handler)(struct llc_sap *sap,
					    struct sk_buff *skb))
{
45
	smp_wmb(); /* ensure initialisation is complete before it's called */
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51 52 53
	if (type == LLC_DEST_SAP || type == LLC_DEST_CONN)
		llc_type_handlers[type - 1] = handler;
}

void llc_remove_pack(int type)
{
	if (type == LLC_DEST_SAP || type == LLC_DEST_CONN)
		llc_type_handlers[type - 1] = NULL;
54
	synchronize_net();
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58
}

void llc_set_station_handler(void (*handler)(struct sk_buff *skb))
{
59 60 61 62
	/* Ensure initialisation is complete before it's called */
	if (handler)
		smp_wmb();

Linus Torvalds's avatar
Linus Torvalds committed
63
	llc_station_handler = handler;
64 65 66

	if (!handler)
		synchronize_net();
Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
}

/**
 *	llc_pdu_type - returns which LLC component must handle for PDU
 *	@skb: input skb
 *
 *	This function returns which LLC component must handle this PDU.
 */
static __inline__ int llc_pdu_type(struct sk_buff *skb)
{
	int type = LLC_DEST_CONN; /* I-PDU or S-PDU type */
	struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);

	if ((pdu->ctrl_1 & LLC_PDU_TYPE_MASK) != LLC_PDU_TYPE_U)
		goto out;
	switch (LLC_U_PDU_CMD(pdu)) {
	case LLC_1_PDU_CMD_XID:
	case LLC_1_PDU_CMD_UI:
	case LLC_1_PDU_CMD_TEST:
		type = LLC_DEST_SAP;
		break;
	case LLC_2_PDU_CMD_SABME:
	case LLC_2_PDU_CMD_DISC:
	case LLC_2_PDU_RSP_UA:
	case LLC_2_PDU_RSP_DM:
	case LLC_2_PDU_RSP_FRMR:
		break;
	default:
		type = LLC_DEST_INVALID;
		break;
	}
out:
	return type;
}

/**
 *	llc_fixup_skb - initializes skb pointers
 *	@skb: This argument points to incoming skb
 *
 *	Initializes internal skb pointer to start of network layer by deriving
 *	length of LLC header; finds length of LLC control field in LLC header
 *	by looking at the two lowest-order bits of the first control field
 *	byte; field is either 3 or 4 bytes long.
 */
static inline int llc_fixup_skb(struct sk_buff *skb)
{
	u8 llc_len = 2;
114
	struct llc_pdu_un *pdu;
Linus Torvalds's avatar
Linus Torvalds committed
115

116
	if (unlikely(!pskb_may_pull(skb, sizeof(*pdu))))
Linus Torvalds's avatar
Linus Torvalds committed
117 118
		return 0;

119
	pdu = (struct llc_pdu_un *)skb->data;
Linus Torvalds's avatar
Linus Torvalds committed
120 121 122
	if ((pdu->ctrl_1 & LLC_PDU_TYPE_MASK) == LLC_PDU_TYPE_U)
		llc_len = 1;
	llc_len += 2;
123 124 125 126

	if (unlikely(!pskb_may_pull(skb, llc_len)))
		return 0;

127
	skb->transport_header += llc_len;
Linus Torvalds's avatar
Linus Torvalds committed
128 129
	skb_pull(skb, llc_len);
	if (skb->protocol == htons(ETH_P_802_2)) {
Al Viro's avatar
Al Viro committed
130
		__be16 pdulen = eth_hdr(skb)->h_proto;
131
		s32 data_size = ntohs(pdulen) - llc_len;
Linus Torvalds's avatar
Linus Torvalds committed
132

133
		if (data_size < 0 ||
134
		    !pskb_may_pull(skb, data_size))
135
			return 0;
136 137
		if (unlikely(pskb_trim_rcsum(skb, data_size)))
			return 0;
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	}
	return 1;
}

/**
 *	llc_rcv - 802.2 entry point from net lower layers
 *	@skb: received pdu
 *	@dev: device that receive pdu
 *	@pt: packet type
 *
 *	When the system receives a 802.2 frame this function is called. It
 *	checks SAP and connection of received pdu and passes frame to
 *	llc_{station,sap,conn}_rcv for sending to proper state machine. If
 *	the frame is related to a busy connection (a connection is sending
 *	data now), it queues this frame in the connection's backlog.
 */
int llc_rcv(struct sk_buff *skb, struct net_device *dev,
155
	    struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds's avatar
Linus Torvalds committed
156 157 158 159
{
	struct llc_sap *sap;
	struct llc_pdu_sn *pdu;
	int dest;
160 161
	int (*rcv)(struct sk_buff *, struct net_device *,
		   struct packet_type *, struct net_device *);
162 163
	void (*sta_handler)(struct sk_buff *skb);
	void (*sap_handler)(struct llc_sap *sap, struct sk_buff *skb);
Linus Torvalds's avatar
Linus Torvalds committed
164

165
	if (!net_eq(dev_net(dev), &init_net))
166 167
		goto drop;

Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171 172
	/*
	 * When the interface is in promisc. mode, drop all the crap that it
	 * receives, do not try to analyse it.
	 */
	if (unlikely(skb->pkt_type == PACKET_OTHERHOST)) {
173
		dprintk("%s: PACKET_OTHERHOST\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177 178 179 180 181 182 183 184 185
		goto drop;
	}
	skb = skb_share_check(skb, GFP_ATOMIC);
	if (unlikely(!skb))
		goto out;
	if (unlikely(!llc_fixup_skb(skb)))
		goto drop;
	pdu = llc_pdu_sn_hdr(skb);
	if (unlikely(!pdu->dsap)) /* NULL DSAP, refer to station */
	       goto handle_station;
	sap = llc_sap_find(pdu->dsap);
	if (unlikely(!sap)) {/* unknown SAP */
186
		dprintk("%s: llc_sap_find(%02X) failed!\n", __func__,
187
			pdu->dsap);
Linus Torvalds's avatar
Linus Torvalds committed
188 189 190 191 192 193
		goto drop;
	}
	/*
	 * First the upper layer protocols that don't need the full
	 * LLC functionality
	 */
194
	rcv = rcu_dereference(sap->rcv_func);
Linus Torvalds's avatar
Linus Torvalds committed
195
	dest = llc_pdu_type(skb);
196
	sap_handler = dest ? READ_ONCE(llc_type_handlers[dest - 1]) : NULL;
197
	if (unlikely(!sap_handler)) {
198 199 200 201 202 203 204 205 206 207
		if (rcv)
			rcv(skb, dev, pt, orig_dev);
		else
			kfree_skb(skb);
	} else {
		if (rcv) {
			struct sk_buff *cskb = skb_clone(skb, GFP_ATOMIC);
			if (cskb)
				rcv(cskb, dev, pt, orig_dev);
		}
208
		sap_handler(sap, skb);
209
	}
210
	llc_sap_put(sap);
Linus Torvalds's avatar
Linus Torvalds committed
211 212 213 214 215 216
out:
	return 0;
drop:
	kfree_skb(skb);
	goto out;
handle_station:
217
	sta_handler = READ_ONCE(llc_station_handler);
218
	if (!sta_handler)
Linus Torvalds's avatar
Linus Torvalds committed
219
		goto drop;
220
	sta_handler(skb);
Linus Torvalds's avatar
Linus Torvalds committed
221 222 223 224 225 226
	goto out;
}

EXPORT_SYMBOL(llc_add_pack);
EXPORT_SYMBOL(llc_remove_pack);
EXPORT_SYMBOL(llc_set_station_handler);