Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * af_llc.c - LLC User Interface SAPs |
| 3 | * Description: |
| 4 | * Functions in this module are implementation of socket based llc |
| 5 | * communications for the Linux operating system. Support of llc class |
| 6 | * one and class two is provided via SOCK_DGRAM and SOCK_STREAM |
| 7 | * respectively. |
| 8 | * |
| 9 | * An llc2 connection is (mac + sap), only one llc2 sap connection |
| 10 | * is allowed per mac. Though one sap may have multiple mac + sap |
| 11 | * connections. |
| 12 | * |
| 13 | * Copyright (c) 2001 by Jay Schulist <jschlst@samba.org> |
| 14 | * 2002-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 15 | * |
| 16 | * This program can be redistributed or modified under the terms of the |
| 17 | * GNU General Public License as published by the Free Software Foundation. |
| 18 | * This program is distributed without any warranty or implied warranty |
| 19 | * of merchantability or fitness for a particular purpose. |
| 20 | * |
| 21 | * See the GNU General Public License for more details. |
| 22 | */ |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 23 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/rtnetlink.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <net/llc.h> |
| 29 | #include <net/llc_sap.h> |
| 30 | #include <net/llc_pdu.h> |
| 31 | #include <net/llc_conn.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 32 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* remember: uninitialized global data is zeroed because its in .bss */ |
| 35 | static u16 llc_ui_sap_last_autoport = LLC_SAP_DYN_START; |
| 36 | static u16 llc_ui_sap_link_no_max[256]; |
| 37 | static struct sockaddr_llc llc_ui_addrnull; |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 38 | static const struct proto_ops llc_ui_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 40 | static int llc_ui_wait_for_conn(struct sock *sk, long timeout); |
| 41 | static int llc_ui_wait_for_disc(struct sock *sk, long timeout); |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 42 | static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #if 0 |
| 45 | #define dprintk(args...) printk(KERN_DEBUG args) |
| 46 | #else |
| 47 | #define dprintk(args...) |
| 48 | #endif |
| 49 | |
| 50 | /** |
| 51 | * llc_ui_next_link_no - return the next unused link number for a sap |
| 52 | * @sap: Address of sap to get link number from. |
| 53 | * |
| 54 | * Return the next unused link number for a given sap. |
| 55 | */ |
Arnaldo Carvalho de Melo | 2342c99 | 2006-03-20 21:27:43 -0800 | [diff] [blame] | 56 | static inline u16 llc_ui_next_link_no(int sap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | return llc_ui_sap_link_no_max[sap]++; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * llc_proto_type - return eth protocol for ARP header type |
| 63 | * @arphrd: ARP header type. |
| 64 | * |
| 65 | * Given an ARP header type return the corresponding ethernet protocol. |
| 66 | */ |
Al Viro | 3fbd418 | 2006-11-08 00:26:05 -0800 | [diff] [blame] | 67 | static inline __be16 llc_proto_type(u16 arphrd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | return arphrd == ARPHRD_IEEE802_TR ? |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 70 | htons(ETH_P_TR_802_2) : htons(ETH_P_802_2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * llc_ui_addr_null - determines if a address structure is null |
| 75 | * @addr: Address to test if null. |
| 76 | */ |
Arnaldo Carvalho de Melo | 2342c99 | 2006-03-20 21:27:43 -0800 | [diff] [blame] | 77 | static inline u8 llc_ui_addr_null(struct sockaddr_llc *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | return !memcmp(addr, &llc_ui_addrnull, sizeof(*addr)); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * llc_ui_header_len - return length of llc header based on operation |
| 84 | * @sk: Socket which contains a valid llc socket type. |
| 85 | * @addr: Complete sockaddr_llc structure received from the user. |
| 86 | * |
| 87 | * Provide the length of the llc header depending on what kind of |
| 88 | * operation the user would like to perform and the type of socket. |
| 89 | * Returns the correct llc header length. |
| 90 | */ |
Arnaldo Carvalho de Melo | 2342c99 | 2006-03-20 21:27:43 -0800 | [diff] [blame] | 91 | static inline u8 llc_ui_header_len(struct sock *sk, struct sockaddr_llc *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | u8 rc = LLC_PDU_LEN_U; |
| 94 | |
| 95 | if (addr->sllc_test || addr->sllc_xid) |
| 96 | rc = LLC_PDU_LEN_U; |
| 97 | else if (sk->sk_type == SOCK_STREAM) |
| 98 | rc = LLC_PDU_LEN_I; |
| 99 | return rc; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * llc_ui_send_data - send data via reliable llc2 connection |
| 104 | * @sk: Connection the socket is using. |
| 105 | * @skb: Data the user wishes to send. |
| 106 | * @addr: Source and destination fields provided by the user. |
| 107 | * @noblock: can we block waiting for data? |
| 108 | * |
| 109 | * Send data via reliable llc2 connection. |
| 110 | * Returns 0 upon success, non-zero if action did not succeed. |
| 111 | */ |
| 112 | static int llc_ui_send_data(struct sock* sk, struct sk_buff *skb, int noblock) |
| 113 | { |
| 114 | struct llc_sock* llc = llc_sk(sk); |
| 115 | int rc = 0; |
| 116 | |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 117 | if (unlikely(llc_data_accept_state(llc->state) || |
Jochen Friedrich | 451677c | 2005-11-14 21:57:46 -0800 | [diff] [blame] | 118 | llc->remote_busy_flag || |
| 119 | llc->p_flag)) { |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 120 | long timeout = sock_sndtimeo(sk, noblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | rc = llc_ui_wait_for_busy_core(sk, timeout); |
| 123 | } |
Arnaldo Carvalho de Melo | 249ff1c | 2005-09-22 04:32:10 -0300 | [diff] [blame] | 124 | if (unlikely(!rc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | rc = llc_build_and_send_pkt(sk, skb); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | static void llc_ui_sk_init(struct socket *sock, struct sock *sk) |
| 130 | { |
| 131 | sk->sk_type = sock->type; |
| 132 | sk->sk_sleep = &sock->wait; |
| 133 | sk->sk_socket = sock; |
| 134 | sock->sk = sk; |
| 135 | sock->ops = &llc_ui_ops; |
| 136 | } |
| 137 | |
| 138 | static struct proto llc_proto = { |
Arnaldo Carvalho de Melo | 9c005e0 | 2006-03-20 21:27:23 -0800 | [diff] [blame] | 139 | .name = "LLC", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | .owner = THIS_MODULE, |
| 141 | .obj_size = sizeof(struct llc_sock), |
| 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * llc_ui_create - alloc and init a new llc_ui socket |
| 146 | * @sock: Socket to initialize and attach allocated sk to. |
| 147 | * @protocol: Unused. |
| 148 | * |
| 149 | * Allocate and initialize a new llc_ui socket, validate the user wants a |
| 150 | * socket type we have available. |
| 151 | * Returns 0 upon success, negative upon failure. |
| 152 | */ |
| 153 | static int llc_ui_create(struct socket *sock, int protocol) |
| 154 | { |
| 155 | struct sock *sk; |
| 156 | int rc = -ESOCKTNOSUPPORT; |
| 157 | |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 158 | if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | rc = -ENOMEM; |
| 160 | sk = llc_sk_alloc(PF_LLC, GFP_KERNEL, &llc_proto); |
| 161 | if (sk) { |
| 162 | rc = 0; |
| 163 | llc_ui_sk_init(sock, sk); |
| 164 | } |
| 165 | } |
| 166 | return rc; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * llc_ui_release - shutdown socket |
| 171 | * @sock: Socket to release. |
| 172 | * |
| 173 | * Shutdown and deallocate an existing socket. |
| 174 | */ |
| 175 | static int llc_ui_release(struct socket *sock) |
| 176 | { |
| 177 | struct sock *sk = sock->sk; |
| 178 | struct llc_sock *llc; |
| 179 | |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 180 | if (unlikely(sk == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | goto out; |
| 182 | sock_hold(sk); |
| 183 | lock_sock(sk); |
| 184 | llc = llc_sk(sk); |
| 185 | dprintk("%s: closing local(%02X) remote(%02X)\n", __FUNCTION__, |
| 186 | llc->laddr.lsap, llc->daddr.lsap); |
| 187 | if (!llc_send_disc(sk)) |
| 188 | llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo); |
Arnaldo Carvalho de Melo | 2e1f47c | 2006-03-20 21:28:11 -0800 | [diff] [blame] | 189 | if (!sock_flag(sk, SOCK_ZAPPED)) { |
| 190 | llc_sap_put(llc->sap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | llc_sap_remove_socket(llc->sap, sk); |
Arnaldo Carvalho de Melo | 2e1f47c | 2006-03-20 21:28:11 -0800 | [diff] [blame] | 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (llc->dev) |
| 195 | dev_put(llc->dev); |
| 196 | sock_put(sk); |
| 197 | llc_sk_free(sk); |
| 198 | out: |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * llc_ui_autoport - provide dynamically allocate SAP number |
| 204 | * |
| 205 | * Provide the caller with a dynamically allocated SAP number according |
| 206 | * to the rules that are set in this function. Returns: 0, upon failure, |
| 207 | * SAP number otherwise. |
| 208 | */ |
| 209 | static int llc_ui_autoport(void) |
| 210 | { |
| 211 | struct llc_sap *sap; |
| 212 | int i, tries = 0; |
| 213 | |
| 214 | while (tries < LLC_SAP_DYN_TRIES) { |
| 215 | for (i = llc_ui_sap_last_autoport; |
| 216 | i < LLC_SAP_DYN_STOP; i += 2) { |
| 217 | sap = llc_sap_find(i); |
| 218 | if (!sap) { |
| 219 | llc_ui_sap_last_autoport = i + 2; |
| 220 | goto out; |
| 221 | } |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 222 | llc_sap_put(sap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | llc_ui_sap_last_autoport = LLC_SAP_DYN_START; |
| 225 | tries++; |
| 226 | } |
| 227 | i = 0; |
| 228 | out: |
| 229 | return i; |
| 230 | } |
| 231 | |
| 232 | /** |
Arnaldo Carvalho de Melo | 5a770c0 | 2005-09-22 03:56:26 -0300 | [diff] [blame] | 233 | * llc_ui_autobind - automatically bind a socket to a sap |
| 234 | * @sock: socket to bind |
| 235 | * @addr: address to connect to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * |
Arnaldo Carvalho de Melo | 5a770c0 | 2005-09-22 03:56:26 -0300 | [diff] [blame] | 237 | * Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't |
| 238 | * specifically used llc_ui_bind to bind to an specific address/sap |
| 239 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | * Returns: 0 upon success, negative otherwise. |
| 241 | */ |
| 242 | static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr) |
| 243 | { |
| 244 | struct sock *sk = sock->sk; |
| 245 | struct llc_sock *llc = llc_sk(sk); |
| 246 | struct llc_sap *sap; |
| 247 | int rc = -EINVAL; |
| 248 | |
| 249 | if (!sock_flag(sk, SOCK_ZAPPED)) |
| 250 | goto out; |
| 251 | rc = -ENODEV; |
| 252 | llc->dev = dev_getfirstbyhwtype(addr->sllc_arphrd); |
| 253 | if (!llc->dev) |
| 254 | goto out; |
| 255 | rc = -EUSERS; |
| 256 | llc->laddr.lsap = llc_ui_autoport(); |
| 257 | if (!llc->laddr.lsap) |
| 258 | goto out; |
| 259 | rc = -EBUSY; /* some other network layer is using the sap */ |
| 260 | sap = llc_sap_open(llc->laddr.lsap, NULL); |
| 261 | if (!sap) |
| 262 | goto out; |
| 263 | memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN); |
| 264 | memcpy(&llc->addr, addr, sizeof(llc->addr)); |
| 265 | /* assign new connection to its SAP */ |
| 266 | llc_sap_add_socket(sap, sk); |
| 267 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 268 | rc = 0; |
| 269 | out: |
| 270 | return rc; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * llc_ui_bind - bind a socket to a specific address. |
| 275 | * @sock: Socket to bind an address to. |
| 276 | * @uaddr: Address the user wants the socket bound to. |
| 277 | * @addrlen: Length of the uaddr structure. |
| 278 | * |
| 279 | * Bind a socket to a specific address. For llc a user is able to bind to |
Arnaldo Carvalho de Melo | 5a770c0 | 2005-09-22 03:56:26 -0300 | [diff] [blame] | 280 | * a specific sap only or mac + sap. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | * If the user desires to bind to a specific mac + sap, it is possible to |
| 282 | * have multiple sap connections via multiple macs. |
| 283 | * Bind and autobind for that matter must enforce the correct sap usage |
| 284 | * otherwise all hell will break loose. |
| 285 | * Returns: 0 upon success, negative otherwise. |
| 286 | */ |
| 287 | static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen) |
| 288 | { |
| 289 | struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr; |
| 290 | struct sock *sk = sock->sk; |
| 291 | struct llc_sock *llc = llc_sk(sk); |
| 292 | struct llc_sap *sap; |
| 293 | int rc = -EINVAL; |
| 294 | |
| 295 | dprintk("%s: binding %02X\n", __FUNCTION__, addr->sllc_sap); |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 296 | if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | goto out; |
| 298 | rc = -EAFNOSUPPORT; |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 299 | if (unlikely(addr->sllc_family != AF_LLC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | goto out; |
Jochen Friedrich | cf309e3 | 2005-09-22 04:44:55 -0300 | [diff] [blame] | 301 | rc = -ENODEV; |
| 302 | rtnl_lock(); |
| 303 | llc->dev = dev_getbyhwaddr(addr->sllc_arphrd, addr->sllc_mac); |
| 304 | rtnl_unlock(); |
| 305 | if (!llc->dev) |
| 306 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | if (!addr->sllc_sap) { |
| 308 | rc = -EUSERS; |
| 309 | addr->sllc_sap = llc_ui_autoport(); |
| 310 | if (!addr->sllc_sap) |
| 311 | goto out; |
| 312 | } |
| 313 | sap = llc_sap_find(addr->sllc_sap); |
| 314 | if (!sap) { |
| 315 | sap = llc_sap_open(addr->sllc_sap, NULL); |
| 316 | rc = -EBUSY; /* some other network layer is using the sap */ |
| 317 | if (!sap) |
| 318 | goto out; |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 319 | llc_sap_hold(sap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } else { |
| 321 | struct llc_addr laddr, daddr; |
| 322 | struct sock *ask; |
| 323 | |
| 324 | memset(&laddr, 0, sizeof(laddr)); |
| 325 | memset(&daddr, 0, sizeof(daddr)); |
| 326 | /* |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 327 | * FIXME: check if the address is multicast, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | * only SOCK_DGRAM can do this. |
| 329 | */ |
| 330 | memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN); |
| 331 | laddr.lsap = addr->sllc_sap; |
| 332 | rc = -EADDRINUSE; /* mac + sap clash. */ |
| 333 | ask = llc_lookup_established(sap, &daddr, &laddr); |
| 334 | if (ask) { |
| 335 | sock_put(ask); |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 336 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | llc->laddr.lsap = addr->sllc_sap; |
| 340 | memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN); |
| 341 | memcpy(&llc->addr, addr, sizeof(llc->addr)); |
| 342 | /* assign new connection to its SAP */ |
| 343 | llc_sap_add_socket(sap, sk); |
| 344 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 345 | rc = 0; |
Arnaldo Carvalho de Melo | 6e2144b | 2005-09-22 04:43:05 -0300 | [diff] [blame] | 346 | out_put: |
| 347 | llc_sap_put(sap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | out: |
| 349 | return rc; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * llc_ui_shutdown - shutdown a connect llc2 socket. |
| 354 | * @sock: Socket to shutdown. |
| 355 | * @how: What part of the socket to shutdown. |
| 356 | * |
| 357 | * Shutdown a connected llc2 socket. Currently this function only supports |
| 358 | * shutting down both sends and receives (2), we could probably make this |
| 359 | * function such that a user can shutdown only half the connection but not |
| 360 | * right now. |
| 361 | * Returns: 0 upon success, negative otherwise. |
| 362 | */ |
| 363 | static int llc_ui_shutdown(struct socket *sock, int how) |
| 364 | { |
| 365 | struct sock *sk = sock->sk; |
| 366 | int rc = -ENOTCONN; |
| 367 | |
| 368 | lock_sock(sk); |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 369 | if (unlikely(sk->sk_state != TCP_ESTABLISHED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | goto out; |
| 371 | rc = -EINVAL; |
| 372 | if (how != 2) |
| 373 | goto out; |
| 374 | rc = llc_send_disc(sk); |
| 375 | if (!rc) |
| 376 | rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo); |
| 377 | /* Wake up anyone sleeping in poll */ |
| 378 | sk->sk_state_change(sk); |
| 379 | out: |
| 380 | release_sock(sk); |
| 381 | return rc; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * llc_ui_connect - Connect to a remote llc2 mac + sap. |
| 386 | * @sock: Socket which will be connected to the remote destination. |
| 387 | * @uaddr: Remote and possibly the local address of the new connection. |
| 388 | * @addrlen: Size of uaddr structure. |
| 389 | * @flags: Operational flags specified by the user. |
| 390 | * |
| 391 | * Connect to a remote llc2 mac + sap. The caller must specify the |
| 392 | * destination mac and address to connect to. If the user hasn't previously |
| 393 | * called bind(2) with a smac the address of the first interface of the |
| 394 | * specified arp type will be used. |
| 395 | * This function will autobind if user did not previously call bind. |
| 396 | * Returns: 0 upon success, negative otherwise. |
| 397 | */ |
| 398 | static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr, |
| 399 | int addrlen, int flags) |
| 400 | { |
| 401 | struct sock *sk = sock->sk; |
| 402 | struct llc_sock *llc = llc_sk(sk); |
| 403 | struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | int rc = -EINVAL; |
| 405 | |
| 406 | lock_sock(sk); |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 407 | if (unlikely(addrlen != sizeof(*addr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | goto out; |
| 409 | rc = -EAFNOSUPPORT; |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 410 | if (unlikely(addr->sllc_family != AF_LLC)) |
| 411 | goto out; |
| 412 | if (unlikely(sk->sk_type != SOCK_STREAM)) |
| 413 | goto out; |
| 414 | rc = -EALREADY; |
| 415 | if (unlikely(sock->state == SS_CONNECTING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | goto out; |
| 417 | /* bind connection to sap if user hasn't done it. */ |
| 418 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 419 | /* bind to sap with null dev, exclusive */ |
| 420 | rc = llc_ui_autobind(sock, addr); |
| 421 | if (rc) |
| 422 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
Jochen Friedrich | 5564af2 | 2005-09-22 04:46:44 -0300 | [diff] [blame] | 424 | llc->daddr.lsap = addr->sllc_sap; |
| 425 | memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | sock->state = SS_CONNECTING; |
| 427 | sk->sk_state = TCP_SYN_SENT; |
| 428 | llc->link = llc_ui_next_link_no(llc->sap->laddr.lsap); |
Arnaldo Carvalho de Melo | 774ccb4 | 2005-09-22 03:53:35 -0300 | [diff] [blame] | 429 | rc = llc_establish_connection(sk, llc->dev->dev_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | addr->sllc_mac, addr->sllc_sap); |
| 431 | if (rc) { |
| 432 | dprintk("%s: llc_ui_send_conn failed :-(\n", __FUNCTION__); |
| 433 | sock->state = SS_UNCONNECTED; |
| 434 | sk->sk_state = TCP_CLOSE; |
| 435 | goto out; |
| 436 | } |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 437 | |
| 438 | if (sk->sk_state == TCP_SYN_SENT) { |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 439 | const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 440 | |
| 441 | if (!timeo || !llc_ui_wait_for_conn(sk, timeo)) |
| 442 | goto out; |
| 443 | |
| 444 | rc = sock_intr_errno(timeo); |
| 445 | if (signal_pending(current)) |
| 446 | goto out; |
| 447 | } |
| 448 | |
| 449 | if (sk->sk_state == TCP_CLOSE) |
| 450 | goto sock_error; |
| 451 | |
| 452 | sock->state = SS_CONNECTED; |
| 453 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | out: |
| 455 | release_sock(sk); |
| 456 | return rc; |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 457 | sock_error: |
| 458 | rc = sock_error(sk) ? : -ECONNABORTED; |
| 459 | sock->state = SS_UNCONNECTED; |
| 460 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /** |
| 464 | * llc_ui_listen - allow a normal socket to accept incoming connections |
| 465 | * @sock: Socket to allow incoming connections on. |
| 466 | * @backlog: Number of connections to queue. |
| 467 | * |
| 468 | * Allow a normal socket to accept incoming connections. |
| 469 | * Returns 0 upon success, negative otherwise. |
| 470 | */ |
| 471 | static int llc_ui_listen(struct socket *sock, int backlog) |
| 472 | { |
| 473 | struct sock *sk = sock->sk; |
| 474 | int rc = -EINVAL; |
| 475 | |
| 476 | lock_sock(sk); |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 477 | if (unlikely(sock->state != SS_UNCONNECTED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | goto out; |
| 479 | rc = -EOPNOTSUPP; |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 480 | if (unlikely(sk->sk_type != SOCK_STREAM)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | goto out; |
| 482 | rc = -EAGAIN; |
| 483 | if (sock_flag(sk, SOCK_ZAPPED)) |
| 484 | goto out; |
| 485 | rc = 0; |
| 486 | if (!(unsigned)backlog) /* BSDism */ |
| 487 | backlog = 1; |
| 488 | sk->sk_max_ack_backlog = backlog; |
| 489 | if (sk->sk_state != TCP_LISTEN) { |
| 490 | sk->sk_ack_backlog = 0; |
| 491 | sk->sk_state = TCP_LISTEN; |
| 492 | } |
| 493 | sk->sk_socket->flags |= __SO_ACCEPTCON; |
| 494 | out: |
| 495 | release_sock(sk); |
| 496 | return rc; |
| 497 | } |
| 498 | |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 499 | static int llc_ui_wait_for_disc(struct sock *sk, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 501 | DEFINE_WAIT(wait); |
| 502 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 504 | while (1) { |
| 505 | prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); |
| 506 | if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE)) |
| 507 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | rc = -ERESTARTSYS; |
| 509 | if (signal_pending(current)) |
| 510 | break; |
| 511 | rc = -EAGAIN; |
| 512 | if (!timeout) |
| 513 | break; |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 514 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 516 | finish_wait(sk->sk_sleep, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | return rc; |
| 518 | } |
| 519 | |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 520 | static int llc_ui_wait_for_conn(struct sock *sk, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | { |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 522 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 524 | while (1) { |
| 525 | prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); |
| 526 | if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT)) |
| 527 | break; |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 528 | if (signal_pending(current) || !timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | } |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 531 | finish_wait(sk->sk_sleep, &wait); |
| 532 | return timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 535 | static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 537 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | struct llc_sock *llc = llc_sk(sk); |
| 539 | int rc; |
| 540 | |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 541 | while (1) { |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 542 | prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | rc = 0; |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 544 | if (sk_wait_event(sk, &timeout, |
| 545 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
| 546 | (!llc_data_accept_state(llc->state) && |
Jochen Friedrich | 451677c | 2005-11-14 21:57:46 -0800 | [diff] [blame] | 547 | !llc->remote_busy_flag && |
Arnaldo Carvalho de Melo | 54fb7f2 | 2005-09-22 04:26:14 -0300 | [diff] [blame] | 548 | !llc->p_flag))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | break; |
| 550 | rc = -ERESTARTSYS; |
| 551 | if (signal_pending(current)) |
| 552 | break; |
| 553 | rc = -EAGAIN; |
| 554 | if (!timeout) |
| 555 | break; |
| 556 | } |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 557 | finish_wait(sk->sk_sleep, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return rc; |
| 559 | } |
| 560 | |
Arnaldo Carvalho de Melo | 2928c19 | 2005-09-22 05:14:33 -0300 | [diff] [blame] | 561 | static int llc_wait_data(struct sock *sk, long timeo) |
Arnaldo Carvalho de Melo | afdbe35 | 2005-09-22 04:37:07 -0300 | [diff] [blame] | 562 | { |
| 563 | int rc; |
| 564 | |
| 565 | while (1) { |
| 566 | /* |
| 567 | * POSIX 1003.1g mandates this order. |
| 568 | */ |
Benjamin LaHaise | c1cbe4b | 2005-12-13 23:22:19 -0800 | [diff] [blame] | 569 | rc = sock_error(sk); |
| 570 | if (rc) |
Arnaldo Carvalho de Melo | afdbe35 | 2005-09-22 04:37:07 -0300 | [diff] [blame] | 571 | break; |
Arnaldo Carvalho de Melo | afdbe35 | 2005-09-22 04:37:07 -0300 | [diff] [blame] | 572 | rc = 0; |
| 573 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 574 | break; |
| 575 | rc = -EAGAIN; |
| 576 | if (!timeo) |
| 577 | break; |
| 578 | rc = sock_intr_errno(timeo); |
| 579 | if (signal_pending(current)) |
| 580 | break; |
| 581 | rc = 0; |
| 582 | if (sk_wait_data(sk, &timeo)) |
| 583 | break; |
| 584 | } |
| 585 | return rc; |
| 586 | } |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | /** |
| 589 | * llc_ui_accept - accept a new incoming connection. |
| 590 | * @sock: Socket which connections arrive on. |
| 591 | * @newsock: Socket to move incoming connection to. |
| 592 | * @flags: User specified operational flags. |
| 593 | * |
| 594 | * Accept a new incoming connection. |
| 595 | * Returns 0 upon success, negative otherwise. |
| 596 | */ |
| 597 | static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags) |
| 598 | { |
| 599 | struct sock *sk = sock->sk, *newsk; |
| 600 | struct llc_sock *llc, *newllc; |
| 601 | struct sk_buff *skb; |
| 602 | int rc = -EOPNOTSUPP; |
| 603 | |
| 604 | dprintk("%s: accepting on %02X\n", __FUNCTION__, |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 605 | llc_sk(sk)->laddr.lsap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | lock_sock(sk); |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 607 | if (unlikely(sk->sk_type != SOCK_STREAM)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | goto out; |
| 609 | rc = -EINVAL; |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 610 | if (unlikely(sock->state != SS_UNCONNECTED || |
| 611 | sk->sk_state != TCP_LISTEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | goto out; |
| 613 | /* wait for a connection to arrive. */ |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 614 | if (skb_queue_empty(&sk->sk_receive_queue)) { |
Arnaldo Carvalho de Melo | afdbe35 | 2005-09-22 04:37:07 -0300 | [diff] [blame] | 615 | rc = llc_wait_data(sk, sk->sk_rcvtimeo); |
Arnaldo Carvalho de Melo | b35bd11 | 2005-09-22 04:22:39 -0300 | [diff] [blame] | 616 | if (rc) |
| 617 | goto out; |
| 618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | dprintk("%s: got a new connection on %02X\n", __FUNCTION__, |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 620 | llc_sk(sk)->laddr.lsap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 622 | rc = -EINVAL; |
| 623 | if (!skb->sk) |
| 624 | goto frees; |
| 625 | rc = 0; |
| 626 | newsk = skb->sk; |
| 627 | /* attach connection to a new socket. */ |
| 628 | llc_ui_sk_init(newsock, newsk); |
| 629 | sock_reset_flag(newsk, SOCK_ZAPPED); |
| 630 | newsk->sk_state = TCP_ESTABLISHED; |
| 631 | newsock->state = SS_CONNECTED; |
| 632 | llc = llc_sk(sk); |
| 633 | newllc = llc_sk(newsk); |
| 634 | memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr)); |
| 635 | newllc->link = llc_ui_next_link_no(newllc->laddr.lsap); |
| 636 | |
| 637 | /* put original socket back into a clean listen state. */ |
| 638 | sk->sk_state = TCP_LISTEN; |
| 639 | sk->sk_ack_backlog--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | dprintk("%s: ok success on %02X, client on %02X\n", __FUNCTION__, |
| 641 | llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap); |
| 642 | frees: |
| 643 | kfree_skb(skb); |
| 644 | out: |
| 645 | release_sock(sk); |
| 646 | return rc; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * llc_ui_recvmsg - copy received data to the socket user. |
| 651 | * @sock: Socket to copy data from. |
| 652 | * @msg: Various user space related information. |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 653 | * @len: Size of user buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | * @flags: User specified flags. |
| 655 | * |
| 656 | * Copy received data to the socket user. |
| 657 | * Returns non-negative upon success, negative otherwise. |
| 658 | */ |
| 659 | static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock, |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 660 | struct msghdr *msg, size_t len, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | struct sockaddr_llc *uaddr = (struct sockaddr_llc *)msg->msg_name; |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 663 | const int nonblock = flags & MSG_DONTWAIT; |
| 664 | struct sk_buff *skb = NULL; |
| 665 | struct sock *sk = sock->sk; |
| 666 | struct llc_sock *llc = llc_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | size_t copied = 0; |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 668 | u32 peek_seq = 0; |
| 669 | u32 *seq; |
| 670 | unsigned long used; |
| 671 | int target; /* Read at least this many bytes */ |
| 672 | long timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 675 | copied = -ENOTCONN; |
Stephen Hemminger | 29efcd2 | 2006-05-25 15:08:59 -0700 | [diff] [blame] | 676 | if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | goto out; |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 678 | |
| 679 | timeo = sock_rcvtimeo(sk, nonblock); |
| 680 | |
| 681 | seq = &llc->copied_seq; |
| 682 | if (flags & MSG_PEEK) { |
| 683 | peek_seq = llc->copied_seq; |
| 684 | seq = &peek_seq; |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 685 | } |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 686 | |
| 687 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); |
| 688 | copied = 0; |
| 689 | |
| 690 | do { |
| 691 | u32 offset; |
| 692 | |
| 693 | /* |
| 694 | * We need to check signals first, to get correct SIGURG |
| 695 | * handling. FIXME: Need to check this doesn't impact 1003.1g |
| 696 | * and move it down to the bottom of the loop |
| 697 | */ |
| 698 | if (signal_pending(current)) { |
| 699 | if (copied) |
| 700 | break; |
| 701 | copied = timeo ? sock_intr_errno(timeo) : -EAGAIN; |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | /* Next get a buffer. */ |
| 706 | |
| 707 | skb = skb_peek(&sk->sk_receive_queue); |
| 708 | if (skb) { |
| 709 | offset = *seq; |
| 710 | goto found_ok_skb; |
| 711 | } |
| 712 | /* Well, if we have backlog, try to process it now yet. */ |
| 713 | |
| 714 | if (copied >= target && !sk->sk_backlog.tail) |
| 715 | break; |
| 716 | |
| 717 | if (copied) { |
| 718 | if (sk->sk_err || |
| 719 | sk->sk_state == TCP_CLOSE || |
| 720 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
| 721 | !timeo || |
| 722 | (flags & MSG_PEEK)) |
| 723 | break; |
| 724 | } else { |
| 725 | if (sock_flag(sk, SOCK_DONE)) |
| 726 | break; |
| 727 | |
| 728 | if (sk->sk_err) { |
| 729 | copied = sock_error(sk); |
| 730 | break; |
| 731 | } |
| 732 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 733 | break; |
| 734 | |
Stephen Hemminger | 29efcd2 | 2006-05-25 15:08:59 -0700 | [diff] [blame] | 735 | if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSE) { |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 736 | if (!sock_flag(sk, SOCK_DONE)) { |
| 737 | /* |
| 738 | * This occurs when user tries to read |
| 739 | * from never connected socket. |
| 740 | */ |
| 741 | copied = -ENOTCONN; |
| 742 | break; |
| 743 | } |
| 744 | break; |
| 745 | } |
| 746 | if (!timeo) { |
| 747 | copied = -EAGAIN; |
| 748 | break; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if (copied >= target) { /* Do not sleep, just process backlog. */ |
| 753 | release_sock(sk); |
| 754 | lock_sock(sk); |
| 755 | } else |
| 756 | sk_wait_data(sk, &timeo); |
| 757 | |
| 758 | if ((flags & MSG_PEEK) && peek_seq != llc->copied_seq) { |
| 759 | if (net_ratelimit()) |
| 760 | printk(KERN_DEBUG "LLC(%s:%d): Application " |
| 761 | "bug, race in MSG_PEEK.\n", |
| 762 | current->comm, current->pid); |
| 763 | peek_seq = llc->copied_seq; |
| 764 | } |
| 765 | continue; |
| 766 | found_ok_skb: |
| 767 | /* Ok so how much can we use? */ |
| 768 | used = skb->len - offset; |
| 769 | if (len < used) |
| 770 | used = len; |
| 771 | |
| 772 | if (!(flags & MSG_TRUNC)) { |
| 773 | int rc = skb_copy_datagram_iovec(skb, offset, |
| 774 | msg->msg_iov, used); |
| 775 | if (rc) { |
| 776 | /* Exception. Bailout! */ |
| 777 | if (!copied) |
| 778 | copied = -EFAULT; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | *seq += used; |
| 784 | copied += used; |
| 785 | len -= used; |
| 786 | |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 787 | if (!(flags & MSG_PEEK)) { |
Chris Leech | 624d116 | 2006-05-23 18:01:28 -0700 | [diff] [blame] | 788 | sk_eat_skb(sk, skb, 0); |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 789 | *seq = 0; |
| 790 | } |
Stephen Hemminger | 30a584d | 2006-08-03 16:38:49 -0700 | [diff] [blame] | 791 | |
| 792 | /* For non stream protcols we get one packet per recvmsg call */ |
| 793 | if (sk->sk_type != SOCK_STREAM) |
| 794 | goto copy_uaddr; |
| 795 | |
| 796 | /* Partial read */ |
| 797 | if (used + offset < skb->len) |
| 798 | continue; |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 799 | } while (len > 0); |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | out: |
| 802 | release_sock(sk); |
Arnaldo Carvalho de Melo | 8420e1b | 2005-09-22 08:29:08 -0300 | [diff] [blame] | 803 | return copied; |
| 804 | copy_uaddr: |
| 805 | if (uaddr != NULL && skb != NULL) { |
| 806 | memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr)); |
| 807 | msg->msg_namelen = sizeof(*uaddr); |
| 808 | } |
| 809 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /** |
| 813 | * llc_ui_sendmsg - Transmit data provided by the socket user. |
| 814 | * @sock: Socket to transmit data from. |
| 815 | * @msg: Various user related information. |
| 816 | * @len: Length of data to transmit. |
| 817 | * |
| 818 | * Transmit data provided by the socket user. |
| 819 | * Returns non-negative upon success, negative otherwise. |
| 820 | */ |
| 821 | static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 822 | struct msghdr *msg, size_t len) |
| 823 | { |
| 824 | struct sock *sk = sock->sk; |
| 825 | struct llc_sock *llc = llc_sk(sk); |
| 826 | struct sockaddr_llc *addr = (struct sockaddr_llc *)msg->msg_name; |
| 827 | int flags = msg->msg_flags; |
| 828 | int noblock = flags & MSG_DONTWAIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | struct sk_buff *skb; |
| 830 | size_t size = 0; |
| 831 | int rc = -EINVAL, copied = 0, hdrlen; |
| 832 | |
| 833 | dprintk("%s: sending from %02X to %02X\n", __FUNCTION__, |
| 834 | llc->laddr.lsap, llc->daddr.lsap); |
| 835 | lock_sock(sk); |
| 836 | if (addr) { |
| 837 | if (msg->msg_namelen < sizeof(*addr)) |
| 838 | goto release; |
| 839 | } else { |
| 840 | if (llc_ui_addr_null(&llc->addr)) |
| 841 | goto release; |
| 842 | addr = &llc->addr; |
| 843 | } |
| 844 | /* must bind connection to sap if user hasn't done it. */ |
| 845 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 846 | /* bind to sap with null dev, exclusive. */ |
| 847 | rc = llc_ui_autobind(sock, addr); |
| 848 | if (rc) |
| 849 | goto release; |
| 850 | } |
Arnaldo Carvalho de Melo | 774ccb4 | 2005-09-22 03:53:35 -0300 | [diff] [blame] | 851 | hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | size = hdrlen + len; |
Arnaldo Carvalho de Melo | 774ccb4 | 2005-09-22 03:53:35 -0300 | [diff] [blame] | 853 | if (size > llc->dev->mtu) |
| 854 | size = llc->dev->mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | copied = size - hdrlen; |
| 856 | release_sock(sk); |
| 857 | skb = sock_alloc_send_skb(sk, size, noblock, &rc); |
| 858 | lock_sock(sk); |
| 859 | if (!skb) |
| 860 | goto release; |
Arnaldo Carvalho de Melo | 774ccb4 | 2005-09-22 03:53:35 -0300 | [diff] [blame] | 861 | skb->dev = llc->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | skb->protocol = llc_proto_type(addr->sllc_arphrd); |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 863 | skb_reserve(skb, hdrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | rc = memcpy_fromiovec(skb_put(skb, copied), msg->msg_iov, copied); |
| 865 | if (rc) |
| 866 | goto out; |
| 867 | if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) { |
| 868 | llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac, |
| 869 | addr->sllc_sap); |
| 870 | goto out; |
| 871 | } |
| 872 | if (addr->sllc_test) { |
| 873 | llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac, |
| 874 | addr->sllc_sap); |
| 875 | goto out; |
| 876 | } |
| 877 | if (addr->sllc_xid) { |
| 878 | llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac, |
| 879 | addr->sllc_sap); |
| 880 | goto out; |
| 881 | } |
| 882 | rc = -ENOPROTOOPT; |
| 883 | if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua)) |
| 884 | goto out; |
| 885 | rc = llc_ui_send_data(sk, skb, noblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | out: |
Arnaldo Carvalho de Melo | 249ff1c | 2005-09-22 04:32:10 -0300 | [diff] [blame] | 887 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | kfree_skb(skb); |
| 889 | release: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | dprintk("%s: failed sending from %02X to %02X: %d\n", |
| 891 | __FUNCTION__, llc->laddr.lsap, llc->daddr.lsap, rc); |
Arnaldo Carvalho de Melo | 249ff1c | 2005-09-22 04:32:10 -0300 | [diff] [blame] | 892 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | release_sock(sk); |
| 894 | return rc ? : copied; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * llc_ui_getname - return the address info of a socket |
| 899 | * @sock: Socket to get address of. |
| 900 | * @uaddr: Address structure to return information. |
| 901 | * @uaddrlen: Length of address structure. |
| 902 | * @peer: Does user want local or remote address information. |
| 903 | * |
| 904 | * Return the address information of a socket. |
| 905 | */ |
| 906 | static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr, |
| 907 | int *uaddrlen, int peer) |
| 908 | { |
| 909 | struct sockaddr_llc sllc; |
| 910 | struct sock *sk = sock->sk; |
| 911 | struct llc_sock *llc = llc_sk(sk); |
| 912 | int rc = 0; |
| 913 | |
| 914 | lock_sock(sk); |
| 915 | if (sock_flag(sk, SOCK_ZAPPED)) |
| 916 | goto out; |
| 917 | *uaddrlen = sizeof(sllc); |
| 918 | memset(uaddr, 0, *uaddrlen); |
| 919 | if (peer) { |
| 920 | rc = -ENOTCONN; |
| 921 | if (sk->sk_state != TCP_ESTABLISHED) |
| 922 | goto out; |
| 923 | if(llc->dev) |
| 924 | sllc.sllc_arphrd = llc->dev->type; |
| 925 | sllc.sllc_sap = llc->daddr.lsap; |
| 926 | memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN); |
| 927 | } else { |
| 928 | rc = -EINVAL; |
| 929 | if (!llc->sap) |
| 930 | goto out; |
| 931 | sllc.sllc_sap = llc->sap->laddr.lsap; |
| 932 | |
| 933 | if (llc->dev) { |
| 934 | sllc.sllc_arphrd = llc->dev->type; |
| 935 | memcpy(&sllc.sllc_mac, &llc->dev->dev_addr, |
| 936 | IFHWADDRLEN); |
| 937 | } |
| 938 | } |
| 939 | rc = 0; |
| 940 | sllc.sllc_family = AF_LLC; |
| 941 | memcpy(uaddr, &sllc, sizeof(sllc)); |
| 942 | out: |
| 943 | release_sock(sk); |
| 944 | return rc; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * llc_ui_ioctl - io controls for PF_LLC |
| 949 | * @sock: Socket to get/set info |
| 950 | * @cmd: command |
| 951 | * @arg: optional argument for cmd |
| 952 | * |
| 953 | * get/set info on llc sockets |
| 954 | */ |
| 955 | static int llc_ui_ioctl(struct socket *sock, unsigned int cmd, |
| 956 | unsigned long arg) |
| 957 | { |
Christoph Hellwig | b5e5fa5 | 2006-01-03 14:18:33 -0800 | [diff] [blame] | 958 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /** |
| 962 | * llc_ui_setsockopt - set various connection specific parameters. |
| 963 | * @sock: Socket to set options on. |
| 964 | * @level: Socket level user is requesting operations on. |
| 965 | * @optname: Operation name. |
| 966 | * @optval User provided operation data. |
| 967 | * @optlen: Length of optval. |
| 968 | * |
| 969 | * Set various connection specific parameters. |
| 970 | */ |
| 971 | static int llc_ui_setsockopt(struct socket *sock, int level, int optname, |
| 972 | char __user *optval, int optlen) |
| 973 | { |
| 974 | struct sock *sk = sock->sk; |
| 975 | struct llc_sock *llc = llc_sk(sk); |
| 976 | int rc = -EINVAL, opt; |
| 977 | |
| 978 | lock_sock(sk); |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 979 | if (unlikely(level != SOL_LLC || optlen != sizeof(int))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | goto out; |
| 981 | rc = get_user(opt, (int __user *)optval); |
| 982 | if (rc) |
| 983 | goto out; |
| 984 | rc = -EINVAL; |
| 985 | switch (optname) { |
| 986 | case LLC_OPT_RETRY: |
| 987 | if (opt > LLC_OPT_MAX_RETRY) |
| 988 | goto out; |
| 989 | llc->n2 = opt; |
| 990 | break; |
| 991 | case LLC_OPT_SIZE: |
| 992 | if (opt > LLC_OPT_MAX_SIZE) |
| 993 | goto out; |
| 994 | llc->n1 = opt; |
| 995 | break; |
| 996 | case LLC_OPT_ACK_TMR_EXP: |
| 997 | if (opt > LLC_OPT_MAX_ACK_TMR_EXP) |
| 998 | goto out; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 999 | llc->ack_timer.expire = opt * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | break; |
| 1001 | case LLC_OPT_P_TMR_EXP: |
| 1002 | if (opt > LLC_OPT_MAX_P_TMR_EXP) |
| 1003 | goto out; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1004 | llc->pf_cycle_timer.expire = opt * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | break; |
| 1006 | case LLC_OPT_REJ_TMR_EXP: |
| 1007 | if (opt > LLC_OPT_MAX_REJ_TMR_EXP) |
| 1008 | goto out; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1009 | llc->rej_sent_timer.expire = opt * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | break; |
| 1011 | case LLC_OPT_BUSY_TMR_EXP: |
| 1012 | if (opt > LLC_OPT_MAX_BUSY_TMR_EXP) |
| 1013 | goto out; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1014 | llc->busy_state_timer.expire = opt * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | break; |
| 1016 | case LLC_OPT_TX_WIN: |
| 1017 | if (opt > LLC_OPT_MAX_WIN) |
| 1018 | goto out; |
| 1019 | llc->k = opt; |
| 1020 | break; |
| 1021 | case LLC_OPT_RX_WIN: |
| 1022 | if (opt > LLC_OPT_MAX_WIN) |
| 1023 | goto out; |
| 1024 | llc->rw = opt; |
| 1025 | break; |
| 1026 | default: |
| 1027 | rc = -ENOPROTOOPT; |
| 1028 | goto out; |
| 1029 | } |
| 1030 | rc = 0; |
| 1031 | out: |
| 1032 | release_sock(sk); |
| 1033 | return rc; |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * llc_ui_getsockopt - get connection specific socket info |
| 1038 | * @sock: Socket to get information from. |
| 1039 | * @level: Socket level user is requesting operations on. |
| 1040 | * @optname: Operation name. |
| 1041 | * @optval: Variable to return operation data in. |
| 1042 | * @optlen: Length of optval. |
| 1043 | * |
| 1044 | * Get connection specific socket information. |
| 1045 | */ |
| 1046 | static int llc_ui_getsockopt(struct socket *sock, int level, int optname, |
| 1047 | char __user *optval, int __user *optlen) |
| 1048 | { |
| 1049 | struct sock *sk = sock->sk; |
| 1050 | struct llc_sock *llc = llc_sk(sk); |
| 1051 | int val = 0, len = 0, rc = -EINVAL; |
| 1052 | |
| 1053 | lock_sock(sk); |
Arnaldo Carvalho de Melo | af426d3 | 2005-09-22 03:59:22 -0300 | [diff] [blame] | 1054 | if (unlikely(level != SOL_LLC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | goto out; |
| 1056 | rc = get_user(len, optlen); |
| 1057 | if (rc) |
| 1058 | goto out; |
| 1059 | rc = -EINVAL; |
| 1060 | if (len != sizeof(int)) |
| 1061 | goto out; |
| 1062 | switch (optname) { |
| 1063 | case LLC_OPT_RETRY: |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1064 | val = llc->n2; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | case LLC_OPT_SIZE: |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1066 | val = llc->n1; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | case LLC_OPT_ACK_TMR_EXP: |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1068 | val = llc->ack_timer.expire / HZ; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | case LLC_OPT_P_TMR_EXP: |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1070 | val = llc->pf_cycle_timer.expire / HZ; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | case LLC_OPT_REJ_TMR_EXP: |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1072 | val = llc->rej_sent_timer.expire / HZ; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | case LLC_OPT_BUSY_TMR_EXP: |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1074 | val = llc->busy_state_timer.expire / HZ; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | case LLC_OPT_TX_WIN: |
| 1076 | val = llc->k; break; |
| 1077 | case LLC_OPT_RX_WIN: |
| 1078 | val = llc->rw; break; |
| 1079 | default: |
| 1080 | rc = -ENOPROTOOPT; |
| 1081 | goto out; |
| 1082 | } |
| 1083 | rc = 0; |
| 1084 | if (put_user(len, optlen) || copy_to_user(optval, &val, len)) |
| 1085 | rc = -EFAULT; |
| 1086 | out: |
| 1087 | release_sock(sk); |
| 1088 | return rc; |
| 1089 | } |
| 1090 | |
| 1091 | static struct net_proto_family llc_ui_family_ops = { |
| 1092 | .family = PF_LLC, |
| 1093 | .create = llc_ui_create, |
| 1094 | .owner = THIS_MODULE, |
| 1095 | }; |
| 1096 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1097 | static const struct proto_ops llc_ui_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | .family = PF_LLC, |
| 1099 | .owner = THIS_MODULE, |
| 1100 | .release = llc_ui_release, |
| 1101 | .bind = llc_ui_bind, |
| 1102 | .connect = llc_ui_connect, |
| 1103 | .socketpair = sock_no_socketpair, |
| 1104 | .accept = llc_ui_accept, |
| 1105 | .getname = llc_ui_getname, |
| 1106 | .poll = datagram_poll, |
| 1107 | .ioctl = llc_ui_ioctl, |
| 1108 | .listen = llc_ui_listen, |
| 1109 | .shutdown = llc_ui_shutdown, |
| 1110 | .setsockopt = llc_ui_setsockopt, |
| 1111 | .getsockopt = llc_ui_getsockopt, |
| 1112 | .sendmsg = llc_ui_sendmsg, |
| 1113 | .recvmsg = llc_ui_recvmsg, |
| 1114 | .mmap = sock_no_mmap, |
| 1115 | .sendpage = sock_no_sendpage, |
| 1116 | }; |
| 1117 | |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1118 | static char llc_proc_err_msg[] __initdata = |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 1119 | KERN_CRIT "LLC: Unable to register the proc_fs entries\n"; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1120 | static char llc_sysctl_err_msg[] __initdata = |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 1121 | KERN_CRIT "LLC: Unable to register the sysctl entries\n"; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1122 | static char llc_sock_err_msg[] __initdata = |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 1123 | KERN_CRIT "LLC: Unable to register the network family\n"; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | static int __init llc2_init(void) |
| 1126 | { |
| 1127 | int rc = proto_register(&llc_proto, 0); |
| 1128 | |
| 1129 | if (rc != 0) |
| 1130 | goto out; |
| 1131 | |
| 1132 | llc_build_offset_table(); |
| 1133 | llc_station_init(); |
| 1134 | llc_ui_sap_last_autoport = LLC_SAP_DYN_START; |
| 1135 | rc = llc_proc_init(); |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1136 | if (rc != 0) { |
| 1137 | printk(llc_proc_err_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | goto out_unregister_llc_proto; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1139 | } |
| 1140 | rc = llc_sysctl_init(); |
| 1141 | if (rc) { |
| 1142 | printk(llc_sysctl_err_msg); |
| 1143 | goto out_proc; |
| 1144 | } |
| 1145 | rc = sock_register(&llc_ui_family_ops); |
| 1146 | if (rc) { |
| 1147 | printk(llc_sock_err_msg); |
| 1148 | goto out_sysctl; |
| 1149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | llc_add_pack(LLC_DEST_SAP, llc_sap_handler); |
| 1151 | llc_add_pack(LLC_DEST_CONN, llc_conn_handler); |
| 1152 | out: |
| 1153 | return rc; |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1154 | out_sysctl: |
| 1155 | llc_sysctl_exit(); |
| 1156 | out_proc: |
| 1157 | llc_proc_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | out_unregister_llc_proto: |
| 1159 | proto_unregister(&llc_proto); |
| 1160 | goto out; |
| 1161 | } |
| 1162 | |
| 1163 | static void __exit llc2_exit(void) |
| 1164 | { |
| 1165 | llc_station_exit(); |
| 1166 | llc_remove_pack(LLC_DEST_SAP); |
| 1167 | llc_remove_pack(LLC_DEST_CONN); |
| 1168 | sock_unregister(PF_LLC); |
| 1169 | llc_proc_exit(); |
Arnaldo Carvalho de Melo | 590232a | 2005-09-22 04:30:44 -0300 | [diff] [blame] | 1170 | llc_sysctl_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | proto_unregister(&llc_proto); |
| 1172 | } |
| 1173 | |
| 1174 | module_init(llc2_init); |
| 1175 | module_exit(llc2_exit); |
| 1176 | |
| 1177 | MODULE_LICENSE("GPL"); |
| 1178 | MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003"); |
| 1179 | MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support"); |
| 1180 | MODULE_ALIAS_NETPROTO(PF_LLC); |