llc_sap.c 11 KB

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