Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * llc_core.c - Minimum needed routines for sap handling and module init/exit |
| 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 <linux/module.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/if_ether.h> |
| 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/init.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 22 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <net/llc.h> |
| 24 | |
| 25 | LIST_HEAD(llc_sap_list); |
stephen hemminger | 5e419e6 | 2014-01-03 09:19:51 -0800 | [diff] [blame] | 26 | static DEFINE_SPINLOCK(llc_sap_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | /** |
| 29 | * llc_sap_alloc - allocates and initializes sap. |
| 30 | * |
| 31 | * Allocates and initializes sap. |
| 32 | */ |
| 33 | static struct llc_sap *llc_sap_alloc(void) |
| 34 | { |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 35 | struct llc_sap *sap = kzalloc(sizeof(*sap), GFP_ATOMIC); |
Octavian Purdila | 52d58ae | 2009-12-26 11:51:05 +0000 | [diff] [blame] | 36 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | if (sap) { |
Joonwoo Park | a5a0481 | 2008-03-28 16:28:36 -0700 | [diff] [blame] | 39 | /* sap->laddr.mac - leave as a null, it's filled by bind */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | sap->state = LLC_SAP_STATE_ACTIVE; |
Octavian Purdila | b76f5a8 | 2009-12-26 11:51:02 +0000 | [diff] [blame] | 41 | spin_lock_init(&sap->sk_lock); |
Octavian Purdila | 52d58ae | 2009-12-26 11:51:05 +0000 | [diff] [blame] | 42 | for (i = 0; i < LLC_SK_LADDR_HASH_ENTRIES; i++) |
| 43 | INIT_HLIST_NULLS_HEAD(&sap->sk_laddr_hash[i], i); |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 44 | atomic_set(&sap->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | return sap; |
| 47 | } |
| 48 | |
Arnaldo Carvalho de Melo | 2928c19 | 2005-09-22 05:14:33 -0300 | [diff] [blame] | 49 | static struct llc_sap *__llc_sap_find(unsigned char sap_value) |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 50 | { |
Weilong Chen | 3cdba60 | 2013-12-20 11:14:59 +0800 | [diff] [blame] | 51 | struct llc_sap *sap; |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 52 | |
| 53 | list_for_each_entry(sap, &llc_sap_list, node) |
| 54 | if (sap->laddr.lsap == sap_value) |
| 55 | goto out; |
| 56 | sap = NULL; |
| 57 | out: |
| 58 | return sap; |
| 59 | } |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /** |
| 62 | * llc_sap_find - searchs a SAP in station |
| 63 | * @sap_value: sap to be found |
| 64 | * |
| 65 | * Searchs for a sap in the sap list of the LLC's station upon the sap ID. |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 66 | * If the sap is found it will be refcounted and the user will have to do |
| 67 | * a llc_sap_put after use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | * Returns the sap or %NULL if not found. |
| 69 | */ |
| 70 | struct llc_sap *llc_sap_find(unsigned char sap_value) |
| 71 | { |
Octavian Purdila | 8beb9ab | 2009-12-26 11:51:06 +0000 | [diff] [blame] | 72 | struct llc_sap *sap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Octavian Purdila | 8beb9ab | 2009-12-26 11:51:06 +0000 | [diff] [blame] | 74 | rcu_read_lock_bh(); |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 75 | sap = __llc_sap_find(sap_value); |
| 76 | if (sap) |
| 77 | llc_sap_hold(sap); |
Octavian Purdila | 8beb9ab | 2009-12-26 11:51:06 +0000 | [diff] [blame] | 78 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return sap; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * llc_sap_open - open interface to the upper layers. |
| 84 | * @lsap: SAP number. |
| 85 | * @func: rcv func for datalink protos |
| 86 | * |
| 87 | * Interface function to upper layer. Each one who wants to get a SAP |
| 88 | * (for example NetBEUI) should call this function. Returns the opened |
| 89 | * SAP for success, NULL for failure. |
| 90 | */ |
| 91 | struct llc_sap *llc_sap_open(unsigned char lsap, |
| 92 | int (*func)(struct sk_buff *skb, |
| 93 | struct net_device *dev, |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 94 | struct packet_type *pt, |
| 95 | struct net_device *orig_dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 97 | struct llc_sap *sap = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Octavian Purdila | 8beb9ab | 2009-12-26 11:51:06 +0000 | [diff] [blame] | 99 | spin_lock_bh(&llc_sap_list_lock); |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 100 | if (__llc_sap_find(lsap)) /* SAP already exists */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | sap = llc_sap_alloc(); |
| 103 | if (!sap) |
| 104 | goto out; |
| 105 | sap->laddr.lsap = lsap; |
| 106 | sap->rcv_func = func; |
Octavian Purdila | 8beb9ab | 2009-12-26 11:51:06 +0000 | [diff] [blame] | 107 | list_add_tail_rcu(&sap->node, &llc_sap_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | out: |
Octavian Purdila | 8beb9ab | 2009-12-26 11:51:06 +0000 | [diff] [blame] | 109 | spin_unlock_bh(&llc_sap_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return sap; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * llc_sap_close - close interface for upper layers. |
| 115 | * @sap: SAP to be closed. |
| 116 | * |
| 117 | * Close interface function to upper layer. Each one who wants to |
| 118 | * close an open SAP (for example NetBEUI) should call this function. |
| 119 | * Removes this sap from the list of saps in the station and then |
| 120 | * frees the memory for this sap. |
| 121 | */ |
| 122 | void llc_sap_close(struct llc_sap *sap) |
| 123 | { |
Octavian Purdila | 52d58ae | 2009-12-26 11:51:05 +0000 | [diff] [blame] | 124 | WARN_ON(sap->sk_count); |
Octavian Purdila | 8beb9ab | 2009-12-26 11:51:06 +0000 | [diff] [blame] | 125 | |
| 126 | spin_lock_bh(&llc_sap_list_lock); |
| 127 | list_del_rcu(&sap->node); |
| 128 | spin_unlock_bh(&llc_sap_list_lock); |
| 129 | |
| 130 | synchronize_rcu(); |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | kfree(sap); |
| 133 | } |
| 134 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 135 | static struct packet_type llc_packet_type __read_mostly = { |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 136 | .type = cpu_to_be16(ETH_P_802_2), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | .func = llc_rcv, |
| 138 | }; |
| 139 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 140 | static struct packet_type llc_tr_packet_type __read_mostly = { |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 141 | .type = cpu_to_be16(ETH_P_TR_802_2), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .func = llc_rcv, |
| 143 | }; |
| 144 | |
| 145 | static int __init llc_init(void) |
| 146 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | dev_add_pack(&llc_packet_type); |
| 148 | dev_add_pack(&llc_tr_packet_type); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static void __exit llc_exit(void) |
| 153 | { |
| 154 | dev_remove_pack(&llc_packet_type); |
| 155 | dev_remove_pack(&llc_tr_packet_type); |
| 156 | } |
| 157 | |
| 158 | module_init(llc_init); |
| 159 | module_exit(llc_exit); |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | EXPORT_SYMBOL(llc_sap_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | EXPORT_SYMBOL(llc_sap_find); |
| 163 | EXPORT_SYMBOL(llc_sap_open); |
| 164 | EXPORT_SYMBOL(llc_sap_close); |
| 165 | |
| 166 | MODULE_LICENSE("GPL"); |
| 167 | MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003"); |
| 168 | MODULE_DESCRIPTION("LLC IEEE 802.2 core support"); |