Blame view
net/llc/llc_sap.c
11.3 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/* * llc_sap.c - driver routines for SAP component. * * 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 <net/llc.h> #include <net/llc_if.h> #include <net/llc_conn.h> #include <net/llc_pdu.h> #include <net/llc_sap.h> #include <net/llc_s_ac.h> #include <net/llc_s_ev.h> #include <net/llc_s_st.h> #include <net/sock.h> |
c752f0739 [TCP]: Move the t... |
24 |
#include <net/tcp_states.h> |
1da177e4c Linux-2.6.12-rc2 |
25 |
#include <linux/llc.h> |
5a0e3ad6a include cleanup: ... |
26 |
#include <linux/slab.h> |
1da177e4c Linux-2.6.12-rc2 |
27 |
|
f83f1768f [LLC]: skb alloca... |
28 29 30 31 32 33 |
static int llc_mac_header_len(unsigned short devtype) { switch (devtype) { case ARPHRD_ETHER: case ARPHRD_LOOPBACK: return sizeof(struct ethhdr); |
f83f1768f [LLC]: skb alloca... |
34 35 36 |
} return 0; } |
1da177e4c Linux-2.6.12-rc2 |
37 38 |
/** * llc_alloc_frame - allocates sk_buff for frame |
74c950c96 net: llc: kerneld... |
39 |
* @sk: socket to allocate frame to |
1d67e6501 [LLC]: Make llc_f... |
40 |
* @dev: network device this skb will be sent over |
f83f1768f [LLC]: skb alloca... |
41 42 |
* @type: pdu type to allocate * @data_size: data size to allocate |
1da177e4c Linux-2.6.12-rc2 |
43 44 45 46 |
* * Allocates an sk_buff for frame and initializes sk_buff fields. * Returns allocated skb or %NULL when out of memory. */ |
f83f1768f [LLC]: skb alloca... |
47 48 |
struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev, u8 type, u32 data_size) |
1da177e4c Linux-2.6.12-rc2 |
49 |
{ |
f83f1768f [LLC]: skb alloca... |
50 51 52 53 54 |
int hlen = type == LLC_PDU_TYPE_U ? 3 : 4; struct sk_buff *skb; hlen += llc_mac_header_len(dev->type); skb = alloc_skb(hlen + data_size, GFP_ATOMIC); |
1da177e4c Linux-2.6.12-rc2 |
55 56 |
if (skb) { |
0a1b0ad9a [LLC]: Use skb_re... |
57 |
skb_reset_mac_header(skb); |
f83f1768f [LLC]: skb alloca... |
58 |
skb_reserve(skb, hlen); |
c1d2bbe1c [SK_BUFF]: Introd... |
59 |
skb_reset_network_header(skb); |
badff6d01 [SK_BUFF]: Introd... |
60 |
skb_reset_transport_header(skb); |
1da177e4c Linux-2.6.12-rc2 |
61 |
skb->protocol = htons(ETH_P_802_2); |
1d67e6501 [LLC]: Make llc_f... |
62 |
skb->dev = dev; |
d389424e0 [LLC]: Fix the ac... |
63 64 |
if (sk != NULL) skb_set_owner_w(skb, sk); |
1da177e4c Linux-2.6.12-rc2 |
65 66 67 |
} return skb; } |
3cdba604d llc: "foo* bar" s... |
68 |
void llc_save_primitive(struct sock *sk, struct sk_buff *skb, u8 prim) |
1da177e4c Linux-2.6.12-rc2 |
69 |
{ |
8420e1b54 [LLC]: fix llc_ui... |
70 |
struct sockaddr_llc *addr; |
1da177e4c Linux-2.6.12-rc2 |
71 72 |
/* save primitive for use by the user. */ |
8420e1b54 [LLC]: fix llc_ui... |
73 |
addr = llc_ui_skb_cb(skb); |
30a584d94 [LLX]: SOCK_DGRAM... |
74 75 |
memset(addr, 0, sizeof(*addr)); |
04e4223f4 [LLC]: Do better ... |
76 |
addr->sllc_family = sk->sk_family; |
1da177e4c Linux-2.6.12-rc2 |
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 114 115 |
addr->sllc_arphrd = skb->dev->type; addr->sllc_test = prim == LLC_TEST_PRIM; addr->sllc_xid = prim == LLC_XID_PRIM; addr->sllc_ua = prim == LLC_DATAUNIT_PRIM; llc_pdu_decode_sa(skb, addr->sllc_mac); llc_pdu_decode_ssap(skb, &addr->sllc_sap); } /** * llc_sap_rtn_pdu - Informs upper layer on rx of an UI, XID or TEST pdu. * @sap: pointer to SAP * @skb: received pdu */ void llc_sap_rtn_pdu(struct llc_sap *sap, struct sk_buff *skb) { struct llc_sap_state_ev *ev = llc_sap_ev(skb); struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb); switch (LLC_U_PDU_RSP(pdu)) { case LLC_1_PDU_CMD_TEST: ev->prim = LLC_TEST_PRIM; break; case LLC_1_PDU_CMD_XID: ev->prim = LLC_XID_PRIM; break; case LLC_1_PDU_CMD_UI: ev->prim = LLC_DATAUNIT_PRIM; break; } ev->ind_cfm_flag = LLC_IND; } /** * llc_find_sap_trans - finds transition for event * @sap: pointer to SAP * @skb: happened event * * This function finds transition that matches with happened event. * Returns the pointer to found transition on success or %NULL for * failure. */ static struct llc_sap_state_trans *llc_find_sap_trans(struct llc_sap *sap, |
3cdba604d llc: "foo* bar" s... |
116 |
struct sk_buff *skb) |
1da177e4c Linux-2.6.12-rc2 |
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
{ int i = 0; struct llc_sap_state_trans *rc = NULL; struct llc_sap_state_trans **next_trans; struct llc_sap_state *curr_state = &llc_sap_state_table[sap->state - 1]; /* * Search thru events for this state until list exhausted or until * its obvious the event is not valid for the current state */ for (next_trans = curr_state->transitions; next_trans[i]->ev; i++) if (!next_trans[i]->ev(sap, skb)) { rc = next_trans[i]; /* got event match; return it */ break; } return rc; } /** * llc_exec_sap_trans_actions - execute actions related to event * @sap: pointer to SAP * @trans: pointer to transition that it's actions must be performed * @skb: happened event. * * This function executes actions that is related to happened event. * Returns 0 for success and 1 for failure of at least one action. */ static int llc_exec_sap_trans_actions(struct llc_sap *sap, struct llc_sap_state_trans *trans, struct sk_buff *skb) { int rc = 0; |
22bbf5f3e llc: Make llc_sap... |
148 |
const llc_sap_action_t *next_action = trans->ev_actions; |
1da177e4c Linux-2.6.12-rc2 |
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
for (; next_action && *next_action; next_action++) if ((*next_action)(sap, skb)) rc = 1; return rc; } /** * llc_sap_next_state - finds transition, execs actions & change SAP state * @sap: pointer to SAP * @skb: happened event * * This function finds transition that matches with happened event, then * executes related actions and finally changes state of SAP. It returns * 0 on success and 1 for failure. */ static int llc_sap_next_state(struct llc_sap *sap, struct sk_buff *skb) { int rc = 1; struct llc_sap_state_trans *trans; if (sap->state > LLC_NR_SAP_STATES) goto out; trans = llc_find_sap_trans(sap, skb); if (!trans) goto out; /* * Got the state to which we next transition; perform the actions * associated with this transition before actually transitioning to the * next state */ rc = llc_exec_sap_trans_actions(sap, trans, skb); if (rc) goto out; /* * Transition SAP to next state if all actions execute successfully */ sap->state = trans->next_state; out: return rc; } /** * llc_sap_state_process - sends event to SAP state machine * @sap: sap to use * @skb: pointer to occurred event * * After executing actions of the event, upper layer will be indicated * if needed(on receiving an UI frame). sk can be null for the * datalink_proto case. |
c6ee11c39 llc: fix sk_buff ... |
199 200 |
* * This function always consumes a reference to the skb. |
1da177e4c Linux-2.6.12-rc2 |
201 202 203 204 |
*/ static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb) { struct llc_sap_state_ev *ev = llc_sap_ev(skb); |
1da177e4c Linux-2.6.12-rc2 |
205 206 |
ev->ind_cfm_flag = 0; llc_sap_next_state(sap, skb); |
c6ee11c39 llc: fix sk_buff ... |
207 208 209 210 211 212 213 |
if (ev->ind_cfm_flag == LLC_IND && skb->sk->sk_state != TCP_LISTEN) { llc_save_primitive(skb->sk, skb, ev->prim); /* queue skb to the user. */ if (sock_queue_rcv_skb(skb->sk, skb) == 0) return; |
d57b1869b [NET] LLC: Fix wh... |
214 |
} |
1da177e4c Linux-2.6.12-rc2 |
215 216 217 218 219 220 221 222 223 224 225 226 227 |
kfree_skb(skb); } /** * llc_build_and_send_test_pkt - TEST interface for upper layers. * @sap: sap to use * @skb: packet to send * @dmac: destination mac address * @dsap: destination sap * * This function is called when upper layer wants to send a TEST pdu. * Returns 0 for success, 1 otherwise. */ |
d57b1869b [NET] LLC: Fix wh... |
228 |
void llc_build_and_send_test_pkt(struct llc_sap *sap, |
1da177e4c Linux-2.6.12-rc2 |
229 230 231 232 233 234 235 236 |
struct sk_buff *skb, u8 *dmac, u8 dsap) { struct llc_sap_state_ev *ev = llc_sap_ev(skb); ev->saddr.lsap = sap->laddr.lsap; ev->daddr.lsap = dsap; memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN); memcpy(ev->daddr.mac, dmac, IFHWADDRLEN); |
d57b1869b [NET] LLC: Fix wh... |
237 |
|
1da177e4c Linux-2.6.12-rc2 |
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
ev->type = LLC_SAP_EV_TYPE_PRIM; ev->prim = LLC_TEST_PRIM; ev->prim_type = LLC_PRIM_TYPE_REQ; llc_sap_state_process(sap, skb); } /** * llc_build_and_send_xid_pkt - XID interface for upper layers * @sap: sap to use * @skb: packet to send * @dmac: destination mac address * @dsap: destination sap * * This function is called when upper layer wants to send a XID pdu. * Returns 0 for success, 1 otherwise. */ void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb, u8 *dmac, u8 dsap) { struct llc_sap_state_ev *ev = llc_sap_ev(skb); ev->saddr.lsap = sap->laddr.lsap; ev->daddr.lsap = dsap; memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN); memcpy(ev->daddr.mac, dmac, IFHWADDRLEN); ev->type = LLC_SAP_EV_TYPE_PRIM; ev->prim = LLC_XID_PRIM; ev->prim_type = LLC_PRIM_TYPE_REQ; llc_sap_state_process(sap, skb); } /** * llc_sap_rcv - sends received pdus to the sap state machine * @sap: current sap component structure. * @skb: received frame. |
74c950c96 net: llc: kerneld... |
274 |
* @sk: socket to associate to frame |
1da177e4c Linux-2.6.12-rc2 |
275 276 277 |
* * Sends received pdus to the sap state machine. */ |
3446b9d57 llc: Fix double a... |
278 279 |
static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb, struct sock *sk) |
1da177e4c Linux-2.6.12-rc2 |
280 281 282 283 284 |
{ struct llc_sap_state_ev *ev = llc_sap_ev(skb); ev->type = LLC_SAP_EV_TYPE_PDU; ev->reason = 0; |
8b74d439e net/llc: avoid BU... |
285 286 |
skb_orphan(skb); sock_hold(sk); |
3446b9d57 llc: Fix double a... |
287 |
skb->sk = sk; |
8b74d439e net/llc: avoid BU... |
288 |
skb->destructor = sock_efree; |
1da177e4c Linux-2.6.12-rc2 |
289 290 |
llc_sap_state_process(sap, skb); } |
b76f5a842 llc: convert the ... |
291 292 293 294 295 296 297 298 |
static inline bool llc_dgram_match(const struct llc_sap *sap, const struct llc_addr *laddr, const struct sock *sk) { struct llc_sock *llc = llc_sk(sk); return sk->sk_type == SOCK_DGRAM && llc->laddr.lsap == laddr->lsap && |
951fd874c llc: Use normal e... |
299 |
ether_addr_equal(llc->laddr.mac, laddr->mac); |
b76f5a842 llc: convert the ... |
300 |
} |
1da177e4c Linux-2.6.12-rc2 |
301 302 303 304 305 306 307 308 309 |
/** * llc_lookup_dgram - Finds dgram socket for the local sap/mac * @sap: SAP * @laddr: address of local LLC (MAC + SAP) * * Search socket list of the SAP and finds connection using the local * mac, and local sap. Returns pointer for socket found, %NULL otherwise. */ static struct sock *llc_lookup_dgram(struct llc_sap *sap, |
bc0e64679 [LLC]: add multic... |
310 |
const struct llc_addr *laddr) |
1da177e4c Linux-2.6.12-rc2 |
311 312 |
{ struct sock *rc; |
b76f5a842 llc: convert the ... |
313 |
struct hlist_nulls_node *node; |
52d58aef5 llc: replace the ... |
314 315 |
int slot = llc_sk_laddr_hashfn(sap, laddr); struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot]; |
b76f5a842 llc: convert the ... |
316 317 318 |
rcu_read_lock_bh(); again: |
52d58aef5 llc: replace the ... |
319 |
sk_nulls_for_each_rcu(rc, node, laddr_hb) { |
b76f5a842 llc: convert the ... |
320 |
if (llc_dgram_match(sap, laddr, rc)) { |
5f0d5a3ae mm: Rename SLAB_D... |
321 |
/* Extra checks required by SLAB_TYPESAFE_BY_RCU */ |
41c6d650f net: convert sock... |
322 |
if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt))) |
b76f5a842 llc: convert the ... |
323 324 325 326 327 328 |
goto again; if (unlikely(llc_sk(rc)->sap != sap || !llc_dgram_match(sap, laddr, rc))) { sock_put(rc); continue; } |
1da177e4c Linux-2.6.12-rc2 |
329 330 331 332 |
goto found; } } rc = NULL; |
52d58aef5 llc: replace the ... |
333 334 335 336 337 338 339 |
/* * if the nulls value we got at the end of this lookup is * not the expected one, we must restart lookup. * We probably met an item that was moved to another chain. */ if (unlikely(get_nulls_value(node) != slot)) goto again; |
1da177e4c Linux-2.6.12-rc2 |
340 |
found: |
b76f5a842 llc: convert the ... |
341 |
rcu_read_unlock_bh(); |
1da177e4c Linux-2.6.12-rc2 |
342 343 |
return rc; } |
b76f5a842 llc: convert the ... |
344 345 346 347 348 349 350 351 352 353 354 |
static inline bool llc_mcast_match(const struct llc_sap *sap, const struct llc_addr *laddr, const struct sk_buff *skb, const struct sock *sk) { struct llc_sock *llc = llc_sk(sk); return sk->sk_type == SOCK_DGRAM && llc->laddr.lsap == laddr->lsap && llc->dev == skb->dev; } |
0f7b67dd9 llc: optimize mul... |
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
static void llc_do_mcast(struct llc_sap *sap, struct sk_buff *skb, struct sock **stack, int count) { struct sk_buff *skb1; int i; for (i = 0; i < count; i++) { skb1 = skb_clone(skb, GFP_ATOMIC); if (!skb1) { sock_put(stack[i]); continue; } llc_sap_rcv(sap, skb1, stack[i]); sock_put(stack[i]); } } |
bc0e64679 [LLC]: add multic... |
372 373 374 375 |
/** * llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets. * @sap: SAP * @laddr: address of local LLC (MAC + SAP) |
74c950c96 net: llc: kerneld... |
376 |
* @skb: PDU to deliver |
bc0e64679 [LLC]: add multic... |
377 378 379 380 381 382 383 384 |
* * Search socket list of the SAP and finds connections with same sap. * Deliver clone to each. */ static void llc_sap_mcast(struct llc_sap *sap, const struct llc_addr *laddr, struct sk_buff *skb) { |
678f4bda3 net: llc: drop VL... |
385 386 387 |
int i = 0; struct sock *sk; struct sock *stack[256 / sizeof(struct sock *)]; |
6d2e3ea28 llc: use a device... |
388 389 |
struct llc_sock *llc; struct hlist_head *dev_hb = llc_sk_dev_hash(sap, skb->dev->ifindex); |
bc0e64679 [LLC]: add multic... |
390 |
|
b76f5a842 llc: convert the ... |
391 |
spin_lock_bh(&sap->sk_lock); |
b67bfe0d4 hlist: drop the n... |
392 |
hlist_for_each_entry(llc, dev_hb, dev_hash_node) { |
6d2e3ea28 llc: use a device... |
393 394 |
sk = &llc->sk; |
bc0e64679 [LLC]: add multic... |
395 |
|
b76f5a842 llc: convert the ... |
396 |
if (!llc_mcast_match(sap, laddr, skb, sk)) |
7ee66fcb9 [LLC]: multicast ... |
397 |
continue; |
bc0e64679 [LLC]: add multic... |
398 |
sock_hold(sk); |
678f4bda3 net: llc: drop VL... |
399 |
if (i < ARRAY_SIZE(stack)) |
0f7b67dd9 llc: optimize mul... |
400 401 402 403 404 |
stack[i++] = sk; else { llc_do_mcast(sap, skb, stack, i); i = 0; } |
bc0e64679 [LLC]: add multic... |
405 |
} |
b76f5a842 llc: convert the ... |
406 |
spin_unlock_bh(&sap->sk_lock); |
0f7b67dd9 llc: optimize mul... |
407 408 |
llc_do_mcast(sap, skb, stack, i); |
bc0e64679 [LLC]: add multic... |
409 |
} |
1da177e4c Linux-2.6.12-rc2 |
410 411 412 |
void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb) { struct llc_addr laddr; |
1da177e4c Linux-2.6.12-rc2 |
413 414 415 |
llc_pdu_decode_da(skb, laddr.mac); llc_pdu_decode_dsap(skb, &laddr.lsap); |
951fd874c llc: Use normal e... |
416 |
if (is_multicast_ether_addr(laddr.mac)) { |
bc0e64679 [LLC]: add multic... |
417 |
llc_sap_mcast(sap, &laddr, skb); |
1da177e4c Linux-2.6.12-rc2 |
418 |
kfree_skb(skb); |
bc0e64679 [LLC]: add multic... |
419 420 421 |
} else { struct sock *sk = llc_lookup_dgram(sap, &laddr); if (sk) { |
3446b9d57 llc: Fix double a... |
422 |
llc_sap_rcv(sap, skb, sk); |
bc0e64679 [LLC]: add multic... |
423 424 425 426 |
sock_put(sk); } else kfree_skb(skb); } |
1da177e4c Linux-2.6.12-rc2 |
427 |
} |