Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * llc_sap.c - driver routines for SAP component. |
| 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 | |
| 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> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 24 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/llc.h> |
| 26 | |
| 27 | /** |
| 28 | * llc_alloc_frame - allocates sk_buff for frame |
Arnaldo Carvalho de Melo | 1d67e65 | 2005-09-22 03:27:56 -0300 | [diff] [blame] | 29 | * @dev: network device this skb will be sent over |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * |
| 31 | * Allocates an sk_buff for frame and initializes sk_buff fields. |
| 32 | * Returns allocated skb or %NULL when out of memory. |
| 33 | */ |
Arnaldo Carvalho de Melo | d389424 | 2005-09-22 07:57:21 -0300 | [diff] [blame^] | 34 | struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | struct sk_buff *skb = alloc_skb(128, GFP_ATOMIC); |
| 37 | |
| 38 | if (skb) { |
| 39 | skb_reserve(skb, 50); |
| 40 | skb->nh.raw = skb->h.raw = skb->data; |
| 41 | skb->protocol = htons(ETH_P_802_2); |
Arnaldo Carvalho de Melo | 1d67e65 | 2005-09-22 03:27:56 -0300 | [diff] [blame] | 42 | skb->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | skb->mac.raw = skb->head; |
Arnaldo Carvalho de Melo | d389424 | 2005-09-22 07:57:21 -0300 | [diff] [blame^] | 44 | if (sk != NULL) |
| 45 | skb_set_owner_w(skb, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | return skb; |
| 48 | } |
| 49 | |
Arnaldo Carvalho de Melo | 04e4223 | 2005-09-22 04:40:59 -0300 | [diff] [blame] | 50 | void llc_save_primitive(struct sock *sk, struct sk_buff* skb, u8 prim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | struct sockaddr_llc *addr = llc_ui_skb_cb(skb); |
| 53 | |
| 54 | /* save primitive for use by the user. */ |
Arnaldo Carvalho de Melo | 04e4223 | 2005-09-22 04:40:59 -0300 | [diff] [blame] | 55 | addr->sllc_family = sk->sk_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | addr->sllc_arphrd = skb->dev->type; |
| 57 | addr->sllc_test = prim == LLC_TEST_PRIM; |
| 58 | addr->sllc_xid = prim == LLC_XID_PRIM; |
| 59 | addr->sllc_ua = prim == LLC_DATAUNIT_PRIM; |
| 60 | llc_pdu_decode_sa(skb, addr->sllc_mac); |
| 61 | llc_pdu_decode_ssap(skb, &addr->sllc_sap); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu. |
| 66 | * @sap: pointer to SAP |
| 67 | * @skb: received pdu |
| 68 | */ |
| 69 | void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb) |
| 70 | { |
| 71 | struct llc_sap_state_ev *ev = llc_sap_ev(skb); |
| 72 | struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb); |
| 73 | |
| 74 | switch (LLC_U_PDU_RSP(pdu)) { |
| 75 | case LLC_1_PDU_CMD_TEST: |
| 76 | ev->prim = LLC_TEST_PRIM; break; |
| 77 | case LLC_1_PDU_CMD_XID: |
| 78 | ev->prim = LLC_XID_PRIM; break; |
| 79 | case LLC_1_PDU_CMD_UI: |
| 80 | ev->prim = LLC_DATAUNIT_PRIM; break; |
| 81 | } |
| 82 | ev->ind_cfm_flag = LLC_IND; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * llc_find_sap_trans - finds transition for event |
| 87 | * @sap: pointer to SAP |
| 88 | * @skb: happened event |
| 89 | * |
| 90 | * This function finds transition that matches with happened event. |
| 91 | * Returns the pointer to found transition on success or %NULL for |
| 92 | * failure. |
| 93 | */ |
| 94 | static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap, |
| 95 | struct sk_buff* skb) |
| 96 | { |
| 97 | int i = 0; |
| 98 | struct llc_sap_state_trans *rc = NULL; |
| 99 | struct llc_sap_state_trans **next_trans; |
| 100 | struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1]; |
| 101 | /* |
| 102 | * Search thru events for this state until list exhausted or until |
| 103 | * its obvious the event is not valid for the current state |
| 104 | */ |
| 105 | for (next_trans = curr_state->transitions; next_trans[i]->ev; i++) |
| 106 | if (!next_trans[i]->ev(sap, skb)) { |
| 107 | rc = next_trans[i]; /* got event match; return it */ |
| 108 | break; |
| 109 | } |
| 110 | return rc; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * llc_exec_sap_trans_actions - execute actions related to event |
| 115 | * @sap: pointer to SAP |
| 116 | * @trans: pointer to transition that it's actions must be performed |
| 117 | * @skb: happened event. |
| 118 | * |
| 119 | * This function executes actions that is related to happened event. |
| 120 | * Returns 0 for success and 1 for failure of at least one action. |
| 121 | */ |
| 122 | static int llc_exec_sap_trans_actions(struct llc_sap *sap, |
| 123 | struct llc_sap_state_trans *trans, |
| 124 | struct sk_buff *skb) |
| 125 | { |
| 126 | int rc = 0; |
| 127 | llc_sap_action_t *next_action = trans->ev_actions; |
| 128 | |
| 129 | for (; next_action && *next_action; next_action++) |
| 130 | if ((*next_action)(sap, skb)) |
| 131 | rc = 1; |
| 132 | return rc; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * llc_sap_next_state - finds transition, execs actions & change SAP state |
| 137 | * @sap: pointer to SAP |
| 138 | * @skb: happened event |
| 139 | * |
| 140 | * This function finds transition that matches with happened event, then |
| 141 | * executes related actions and finally changes state of SAP. It returns |
| 142 | * 0 on success and 1 for failure. |
| 143 | */ |
| 144 | static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb) |
| 145 | { |
| 146 | int rc = 1; |
| 147 | struct llc_sap_state_trans *trans; |
| 148 | |
| 149 | if (sap->state > LLC_NR_SAP_STATES) |
| 150 | goto out; |
| 151 | trans = llc_find_sap_trans(sap, skb); |
| 152 | if (!trans) |
| 153 | goto out; |
| 154 | /* |
| 155 | * Got the state to which we next transition; perform the actions |
| 156 | * associated with this transition before actually transitioning to the |
| 157 | * next state |
| 158 | */ |
| 159 | rc = llc_exec_sap_trans_actions(sap, trans, skb); |
| 160 | if (rc) |
| 161 | goto out; |
| 162 | /* |
| 163 | * Transition SAP to next state if all actions execute successfully |
| 164 | */ |
| 165 | sap->state = trans->next_state; |
| 166 | out: |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * llc_sap_state_process - sends event to SAP state machine |
| 172 | * @sap: sap to use |
| 173 | * @skb: pointer to occurred event |
| 174 | * |
| 175 | * After executing actions of the event, upper layer will be indicated |
| 176 | * if needed(on receiving an UI frame). sk can be null for the |
| 177 | * datalink_proto case. |
| 178 | */ |
| 179 | static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb) |
| 180 | { |
| 181 | struct llc_sap_state_ev *ev = llc_sap_ev(skb); |
| 182 | |
| 183 | /* |
| 184 | * We have to hold the skb, because llc_sap_next_state |
| 185 | * will kfree it in the sending path and we need to |
| 186 | * look at the skb->cb, where we encode llc_sap_state_ev. |
| 187 | */ |
| 188 | skb_get(skb); |
| 189 | ev->ind_cfm_flag = 0; |
| 190 | llc_sap_next_state(sap, skb); |
| 191 | if (ev->ind_cfm_flag == LLC_IND) { |
| 192 | if (skb->sk->sk_state == TCP_LISTEN) |
| 193 | kfree_skb(skb); |
| 194 | else { |
Arnaldo Carvalho de Melo | 04e4223 | 2005-09-22 04:40:59 -0300 | [diff] [blame] | 195 | llc_save_primitive(skb->sk, skb, ev->prim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | /* queue skb to the user. */ |
| 198 | if (sock_queue_rcv_skb(skb->sk, skb)) |
| 199 | kfree_skb(skb); |
| 200 | } |
| 201 | } |
| 202 | kfree_skb(skb); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * llc_build_and_send_test_pkt - TEST interface for upper layers. |
| 207 | * @sap: sap to use |
| 208 | * @skb: packet to send |
| 209 | * @dmac: destination mac address |
| 210 | * @dsap: destination sap |
| 211 | * |
| 212 | * This function is called when upper layer wants to send a TEST pdu. |
| 213 | * Returns 0 for success, 1 otherwise. |
| 214 | */ |
| 215 | void llc_build_and_send_test_pkt(struct llc_sap *sap, |
| 216 | struct sk_buff *skb, u8 *dmac, u8 dsap) |
| 217 | { |
| 218 | struct llc_sap_state_ev *ev = llc_sap_ev(skb); |
| 219 | |
| 220 | ev->saddr.lsap = sap->laddr.lsap; |
| 221 | ev->daddr.lsap = dsap; |
| 222 | memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN); |
| 223 | memcpy(ev->daddr.mac, dmac, IFHWADDRLEN); |
| 224 | |
| 225 | ev->type = LLC_SAP_EV_TYPE_PRIM; |
| 226 | ev->prim = LLC_TEST_PRIM; |
| 227 | ev->prim_type = LLC_PRIM_TYPE_REQ; |
| 228 | llc_sap_state_process(sap, skb); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * llc_build_and_send_xid_pkt - XID interface for upper layers |
| 233 | * @sap: sap to use |
| 234 | * @skb: packet to send |
| 235 | * @dmac: destination mac address |
| 236 | * @dsap: destination sap |
| 237 | * |
| 238 | * This function is called when upper layer wants to send a XID pdu. |
| 239 | * Returns 0 for success, 1 otherwise. |
| 240 | */ |
| 241 | void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb, |
| 242 | u8 *dmac, u8 dsap) |
| 243 | { |
| 244 | struct llc_sap_state_ev *ev = llc_sap_ev(skb); |
| 245 | |
| 246 | ev->saddr.lsap = sap->laddr.lsap; |
| 247 | ev->daddr.lsap = dsap; |
| 248 | memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN); |
| 249 | memcpy(ev->daddr.mac, dmac, IFHWADDRLEN); |
| 250 | |
| 251 | ev->type = LLC_SAP_EV_TYPE_PRIM; |
| 252 | ev->prim = LLC_XID_PRIM; |
| 253 | ev->prim_type = LLC_PRIM_TYPE_REQ; |
| 254 | llc_sap_state_process(sap, skb); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * llc_sap_rcv - sends received pdus to the sap state machine |
| 259 | * @sap: current sap component structure. |
| 260 | * @skb: received frame. |
| 261 | * |
| 262 | * Sends received pdus to the sap state machine. |
| 263 | */ |
| 264 | static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb) |
| 265 | { |
| 266 | struct llc_sap_state_ev *ev = llc_sap_ev(skb); |
| 267 | |
| 268 | ev->type = LLC_SAP_EV_TYPE_PDU; |
| 269 | ev->reason = 0; |
| 270 | llc_sap_state_process(sap, skb); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * llc_lookup_dgram - Finds dgram socket for the local sap/mac |
| 275 | * @sap: SAP |
| 276 | * @laddr: address of local LLC (MAC + SAP) |
| 277 | * |
| 278 | * Search socket list of the SAP and finds connection using the local |
| 279 | * mac, and local sap. Returns pointer for socket found, %NULL otherwise. |
| 280 | */ |
| 281 | static struct sock *llc_lookup_dgram(struct llc_sap *sap, |
| 282 | struct llc_addr *laddr) |
| 283 | { |
| 284 | struct sock *rc; |
| 285 | struct hlist_node *node; |
| 286 | |
| 287 | read_lock_bh(&sap->sk_list.lock); |
| 288 | sk_for_each(rc, node, &sap->sk_list.list) { |
| 289 | struct llc_sock *llc = llc_sk(rc); |
| 290 | |
| 291 | if (rc->sk_type == SOCK_DGRAM && |
| 292 | llc->laddr.lsap == laddr->lsap && |
| 293 | llc_mac_match(llc->laddr.mac, laddr->mac)) { |
| 294 | sock_hold(rc); |
| 295 | goto found; |
| 296 | } |
| 297 | } |
| 298 | rc = NULL; |
| 299 | found: |
| 300 | read_unlock_bh(&sap->sk_list.lock); |
| 301 | return rc; |
| 302 | } |
| 303 | |
| 304 | void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb) |
| 305 | { |
| 306 | struct llc_addr laddr; |
| 307 | struct sock *sk; |
| 308 | |
| 309 | llc_pdu_decode_da(skb, laddr.mac); |
| 310 | llc_pdu_decode_dsap(skb, &laddr.lsap); |
| 311 | |
| 312 | sk = llc_lookup_dgram(sap, &laddr); |
| 313 | if (sk) { |
Arnaldo Carvalho de Melo | 04e4223 | 2005-09-22 04:40:59 -0300 | [diff] [blame] | 314 | skb_set_owner_r(skb, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | llc_sap_rcv(sap, skb); |
| 316 | sock_put(sk); |
| 317 | } else |
| 318 | kfree_skb(skb); |
| 319 | } |