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