blob: 233794435bb80d60837059c6db39ce87707d3c4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
23#include <linux/config.h>
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030024#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/rtnetlink.h>
28#include <linux/init.h>
29#include <net/llc.h>
30#include <net/llc_sap.h>
31#include <net/llc_pdu.h>
32#include <net/llc_conn.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070033#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* remember: uninitialized global data is zeroed because its in .bss */
36static u16 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
37static u16 llc_ui_sap_link_no_max[256];
38static struct sockaddr_llc llc_ui_addrnull;
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080039static const struct proto_ops llc_ui_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -030041static int llc_ui_wait_for_conn(struct sock *sk, long timeout);
42static int llc_ui_wait_for_disc(struct sock *sk, long timeout);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -030043static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#if 0
46#define dprintk(args...) printk(KERN_DEBUG args)
47#else
48#define dprintk(args...)
49#endif
50
51/**
52 * llc_ui_next_link_no - return the next unused link number for a sap
53 * @sap: Address of sap to get link number from.
54 *
55 * Return the next unused link number for a given sap.
56 */
Arnaldo Carvalho de Melo2342c992006-03-20 21:27:43 -080057static inline u16 llc_ui_next_link_no(int sap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 return llc_ui_sap_link_no_max[sap]++;
60}
61
62/**
63 * llc_proto_type - return eth protocol for ARP header type
64 * @arphrd: ARP header type.
65 *
66 * Given an ARP header type return the corresponding ethernet protocol.
67 */
Arnaldo Carvalho de Melo2342c992006-03-20 21:27:43 -080068static inline u16 llc_proto_type(u16 arphrd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 return arphrd == ARPHRD_IEEE802_TR ?
71 htons(ETH_P_TR_802_2) : htons(ETH_P_802_2);
72}
73
74/**
75 * llc_ui_addr_null - determines if a address structure is null
76 * @addr: Address to test if null.
77 */
Arnaldo Carvalho de Melo2342c992006-03-20 21:27:43 -080078static inline u8 llc_ui_addr_null(struct sockaddr_llc *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 return !memcmp(addr, &llc_ui_addrnull, sizeof(*addr));
81}
82
83/**
84 * llc_ui_header_len - return length of llc header based on operation
85 * @sk: Socket which contains a valid llc socket type.
86 * @addr: Complete sockaddr_llc structure received from the user.
87 *
88 * Provide the length of the llc header depending on what kind of
89 * operation the user would like to perform and the type of socket.
90 * Returns the correct llc header length.
91 */
Arnaldo Carvalho de Melo2342c992006-03-20 21:27:43 -080092static inline u8 llc_ui_header_len(struct sock *sk, struct sockaddr_llc *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 u8 rc = LLC_PDU_LEN_U;
95
96 if (addr->sllc_test || addr->sllc_xid)
97 rc = LLC_PDU_LEN_U;
98 else if (sk->sk_type == SOCK_STREAM)
99 rc = LLC_PDU_LEN_I;
100 return rc;
101}
102
103/**
104 * llc_ui_send_data - send data via reliable llc2 connection
105 * @sk: Connection the socket is using.
106 * @skb: Data the user wishes to send.
107 * @addr: Source and destination fields provided by the user.
108 * @noblock: can we block waiting for data?
109 *
110 * Send data via reliable llc2 connection.
111 * Returns 0 upon success, non-zero if action did not succeed.
112 */
113static int llc_ui_send_data(struct sock* sk, struct sk_buff *skb, int noblock)
114{
115 struct llc_sock* llc = llc_sk(sk);
116 int rc = 0;
117
Jochen Friedrich451677c2005-11-14 21:57:46 -0800118 if (unlikely(llc_data_accept_state(llc->state) ||
119 llc->remote_busy_flag ||
120 llc->p_flag)) {
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300121 long timeout = sock_sndtimeo(sk, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 rc = llc_ui_wait_for_busy_core(sk, timeout);
124 }
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300125 if (unlikely(!rc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 rc = llc_build_and_send_pkt(sk, skb);
127 return rc;
128}
129
130static void llc_ui_sk_init(struct socket *sock, struct sock *sk)
131{
132 sk->sk_type = sock->type;
133 sk->sk_sleep = &sock->wait;
134 sk->sk_socket = sock;
135 sock->sk = sk;
136 sock->ops = &llc_ui_ops;
137}
138
139static struct proto llc_proto = {
Arnaldo Carvalho de Melo9c005e02006-03-20 21:27:23 -0800140 .name = "LLC",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .owner = THIS_MODULE,
142 .obj_size = sizeof(struct llc_sock),
143};
144
145/**
146 * llc_ui_create - alloc and init a new llc_ui socket
147 * @sock: Socket to initialize and attach allocated sk to.
148 * @protocol: Unused.
149 *
150 * Allocate and initialize a new llc_ui socket, validate the user wants a
151 * socket type we have available.
152 * Returns 0 upon success, negative upon failure.
153 */
154static int llc_ui_create(struct socket *sock, int protocol)
155{
156 struct sock *sk;
157 int rc = -ESOCKTNOSUPPORT;
158
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300159 if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 rc = -ENOMEM;
161 sk = llc_sk_alloc(PF_LLC, GFP_KERNEL, &llc_proto);
162 if (sk) {
163 rc = 0;
164 llc_ui_sk_init(sock, sk);
165 }
166 }
167 return rc;
168}
169
170/**
171 * llc_ui_release - shutdown socket
172 * @sock: Socket to release.
173 *
174 * Shutdown and deallocate an existing socket.
175 */
176static int llc_ui_release(struct socket *sock)
177{
178 struct sock *sk = sock->sk;
179 struct llc_sock *llc;
180
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300181 if (unlikely(sk == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out;
183 sock_hold(sk);
184 lock_sock(sk);
185 llc = llc_sk(sk);
186 dprintk("%s: closing local(%02X) remote(%02X)\n", __FUNCTION__,
187 llc->laddr.lsap, llc->daddr.lsap);
188 if (!llc_send_disc(sk))
189 llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
190 if (!sock_flag(sk, SOCK_ZAPPED))
191 llc_sap_remove_socket(llc->sap, sk);
192 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (llc->dev)
194 dev_put(llc->dev);
195 sock_put(sk);
196 llc_sk_free(sk);
197out:
198 return 0;
199}
200
201/**
202 * llc_ui_autoport - provide dynamically allocate SAP number
203 *
204 * Provide the caller with a dynamically allocated SAP number according
205 * to the rules that are set in this function. Returns: 0, upon failure,
206 * SAP number otherwise.
207 */
208static int llc_ui_autoport(void)
209{
210 struct llc_sap *sap;
211 int i, tries = 0;
212
213 while (tries < LLC_SAP_DYN_TRIES) {
214 for (i = llc_ui_sap_last_autoport;
215 i < LLC_SAP_DYN_STOP; i += 2) {
216 sap = llc_sap_find(i);
217 if (!sap) {
218 llc_ui_sap_last_autoport = i + 2;
219 goto out;
220 }
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300221 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
224 tries++;
225 }
226 i = 0;
227out:
228 return i;
229}
230
231/**
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300232 * llc_ui_autobind - automatically bind a socket to a sap
233 * @sock: socket to bind
234 * @addr: address to connect to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 *
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300236 * Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't
237 * specifically used llc_ui_bind to bind to an specific address/sap
238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * Returns: 0 upon success, negative otherwise.
240 */
241static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
242{
243 struct sock *sk = sock->sk;
244 struct llc_sock *llc = llc_sk(sk);
245 struct llc_sap *sap;
246 int rc = -EINVAL;
247
248 if (!sock_flag(sk, SOCK_ZAPPED))
249 goto out;
250 rc = -ENODEV;
251 llc->dev = dev_getfirstbyhwtype(addr->sllc_arphrd);
252 if (!llc->dev)
253 goto out;
254 rc = -EUSERS;
255 llc->laddr.lsap = llc_ui_autoport();
256 if (!llc->laddr.lsap)
257 goto out;
258 rc = -EBUSY; /* some other network layer is using the sap */
259 sap = llc_sap_open(llc->laddr.lsap, NULL);
260 if (!sap)
261 goto out;
262 memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
263 memcpy(&llc->addr, addr, sizeof(llc->addr));
264 /* assign new connection to its SAP */
265 llc_sap_add_socket(sap, sk);
266 sock_reset_flag(sk, SOCK_ZAPPED);
267 rc = 0;
268out:
269 return rc;
270}
271
272/**
273 * llc_ui_bind - bind a socket to a specific address.
274 * @sock: Socket to bind an address to.
275 * @uaddr: Address the user wants the socket bound to.
276 * @addrlen: Length of the uaddr structure.
277 *
278 * Bind a socket to a specific address. For llc a user is able to bind to
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300279 * a specific sap only or mac + sap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * If the user desires to bind to a specific mac + sap, it is possible to
281 * have multiple sap connections via multiple macs.
282 * Bind and autobind for that matter must enforce the correct sap usage
283 * otherwise all hell will break loose.
284 * Returns: 0 upon success, negative otherwise.
285 */
286static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
287{
288 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
289 struct sock *sk = sock->sk;
290 struct llc_sock *llc = llc_sk(sk);
291 struct llc_sap *sap;
292 int rc = -EINVAL;
293
294 dprintk("%s: binding %02X\n", __FUNCTION__, addr->sllc_sap);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300295 if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto out;
297 rc = -EAFNOSUPPORT;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300298 if (unlikely(addr->sllc_family != AF_LLC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto out;
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300300 rc = -ENODEV;
301 rtnl_lock();
302 llc->dev = dev_getbyhwaddr(addr->sllc_arphrd, addr->sllc_mac);
303 rtnl_unlock();
304 if (!llc->dev)
305 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!addr->sllc_sap) {
307 rc = -EUSERS;
308 addr->sllc_sap = llc_ui_autoport();
309 if (!addr->sllc_sap)
310 goto out;
311 }
312 sap = llc_sap_find(addr->sllc_sap);
313 if (!sap) {
314 sap = llc_sap_open(addr->sllc_sap, NULL);
315 rc = -EBUSY; /* some other network layer is using the sap */
316 if (!sap)
317 goto out;
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300318 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else {
320 struct llc_addr laddr, daddr;
321 struct sock *ask;
322
323 memset(&laddr, 0, sizeof(laddr));
324 memset(&daddr, 0, sizeof(daddr));
325 /*
326 * FIXME: check if the the address is multicast,
327 * only SOCK_DGRAM can do this.
328 */
329 memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
330 laddr.lsap = addr->sllc_sap;
331 rc = -EADDRINUSE; /* mac + sap clash. */
332 ask = llc_lookup_established(sap, &daddr, &laddr);
333 if (ask) {
334 sock_put(ask);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300335 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 }
338 llc->laddr.lsap = addr->sllc_sap;
339 memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
340 memcpy(&llc->addr, addr, sizeof(llc->addr));
341 /* assign new connection to its SAP */
342 llc_sap_add_socket(sap, sk);
343 sock_reset_flag(sk, SOCK_ZAPPED);
344 rc = 0;
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300345out_put:
346 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347out:
348 return rc;
349}
350
351/**
352 * llc_ui_shutdown - shutdown a connect llc2 socket.
353 * @sock: Socket to shutdown.
354 * @how: What part of the socket to shutdown.
355 *
356 * Shutdown a connected llc2 socket. Currently this function only supports
357 * shutting down both sends and receives (2), we could probably make this
358 * function such that a user can shutdown only half the connection but not
359 * right now.
360 * Returns: 0 upon success, negative otherwise.
361 */
362static int llc_ui_shutdown(struct socket *sock, int how)
363{
364 struct sock *sk = sock->sk;
365 int rc = -ENOTCONN;
366
367 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300368 if (unlikely(sk->sk_state != TCP_ESTABLISHED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto out;
370 rc = -EINVAL;
371 if (how != 2)
372 goto out;
373 rc = llc_send_disc(sk);
374 if (!rc)
375 rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
376 /* Wake up anyone sleeping in poll */
377 sk->sk_state_change(sk);
378out:
379 release_sock(sk);
380 return rc;
381}
382
383/**
384 * llc_ui_connect - Connect to a remote llc2 mac + sap.
385 * @sock: Socket which will be connected to the remote destination.
386 * @uaddr: Remote and possibly the local address of the new connection.
387 * @addrlen: Size of uaddr structure.
388 * @flags: Operational flags specified by the user.
389 *
390 * Connect to a remote llc2 mac + sap. The caller must specify the
391 * destination mac and address to connect to. If the user hasn't previously
392 * called bind(2) with a smac the address of the first interface of the
393 * specified arp type will be used.
394 * This function will autobind if user did not previously call bind.
395 * Returns: 0 upon success, negative otherwise.
396 */
397static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
398 int addrlen, int flags)
399{
400 struct sock *sk = sock->sk;
401 struct llc_sock *llc = llc_sk(sk);
402 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int rc = -EINVAL;
404
405 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300406 if (unlikely(addrlen != sizeof(*addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 goto out;
408 rc = -EAFNOSUPPORT;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300409 if (unlikely(addr->sllc_family != AF_LLC))
410 goto out;
411 if (unlikely(sk->sk_type != SOCK_STREAM))
412 goto out;
413 rc = -EALREADY;
414 if (unlikely(sock->state == SS_CONNECTING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto out;
416 /* bind connection to sap if user hasn't done it. */
417 if (sock_flag(sk, SOCK_ZAPPED)) {
418 /* bind to sap with null dev, exclusive */
419 rc = llc_ui_autobind(sock, addr);
420 if (rc)
421 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Jochen Friedrich5564af22005-09-22 04:46:44 -0300423 llc->daddr.lsap = addr->sllc_sap;
424 memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 sock->state = SS_CONNECTING;
426 sk->sk_state = TCP_SYN_SENT;
427 llc->link = llc_ui_next_link_no(llc->sap->laddr.lsap);
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300428 rc = llc_establish_connection(sk, llc->dev->dev_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 addr->sllc_mac, addr->sllc_sap);
430 if (rc) {
431 dprintk("%s: llc_ui_send_conn failed :-(\n", __FUNCTION__);
432 sock->state = SS_UNCONNECTED;
433 sk->sk_state = TCP_CLOSE;
434 goto out;
435 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300436
437 if (sk->sk_state == TCP_SYN_SENT) {
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300438 const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300439
440 if (!timeo || !llc_ui_wait_for_conn(sk, timeo))
441 goto out;
442
443 rc = sock_intr_errno(timeo);
444 if (signal_pending(current))
445 goto out;
446 }
447
448 if (sk->sk_state == TCP_CLOSE)
449 goto sock_error;
450
451 sock->state = SS_CONNECTED;
452 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453out:
454 release_sock(sk);
455 return rc;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300456sock_error:
457 rc = sock_error(sk) ? : -ECONNABORTED;
458 sock->state = SS_UNCONNECTED;
459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/**
463 * llc_ui_listen - allow a normal socket to accept incoming connections
464 * @sock: Socket to allow incoming connections on.
465 * @backlog: Number of connections to queue.
466 *
467 * Allow a normal socket to accept incoming connections.
468 * Returns 0 upon success, negative otherwise.
469 */
470static int llc_ui_listen(struct socket *sock, int backlog)
471{
472 struct sock *sk = sock->sk;
473 int rc = -EINVAL;
474
475 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300476 if (unlikely(sock->state != SS_UNCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto out;
478 rc = -EOPNOTSUPP;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300479 if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto out;
481 rc = -EAGAIN;
482 if (sock_flag(sk, SOCK_ZAPPED))
483 goto out;
484 rc = 0;
485 if (!(unsigned)backlog) /* BSDism */
486 backlog = 1;
487 sk->sk_max_ack_backlog = backlog;
488 if (sk->sk_state != TCP_LISTEN) {
489 sk->sk_ack_backlog = 0;
490 sk->sk_state = TCP_LISTEN;
491 }
492 sk->sk_socket->flags |= __SO_ACCEPTCON;
493out:
494 release_sock(sk);
495 return rc;
496}
497
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300498static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300500 DEFINE_WAIT(wait);
501 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300503 while (1) {
504 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
505 if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE))
506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 rc = -ERESTARTSYS;
508 if (signal_pending(current))
509 break;
510 rc = -EAGAIN;
511 if (!timeout)
512 break;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300513 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300515 finish_wait(sk->sk_sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return rc;
517}
518
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300519static int llc_ui_wait_for_conn(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300521 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300523 while (1) {
524 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
525 if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT))
526 break;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300527 if (signal_pending(current) || !timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300530 finish_wait(sk->sk_sleep, &wait);
531 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300534static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300536 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct llc_sock *llc = llc_sk(sk);
538 int rc;
539
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300540 while (1) {
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300541 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 rc = 0;
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300543 if (sk_wait_event(sk, &timeout,
544 (sk->sk_shutdown & RCV_SHUTDOWN) ||
545 (!llc_data_accept_state(llc->state) &&
Jochen Friedrich451677c2005-11-14 21:57:46 -0800546 !llc->remote_busy_flag &&
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300547 !llc->p_flag)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 rc = -ERESTARTSYS;
550 if (signal_pending(current))
551 break;
552 rc = -EAGAIN;
553 if (!timeout)
554 break;
555 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300556 finish_wait(sk->sk_sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return rc;
558}
559
Arnaldo Carvalho de Melo2928c192005-09-22 05:14:33 -0300560static int llc_wait_data(struct sock *sk, long timeo)
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300561{
562 int rc;
563
564 while (1) {
565 /*
566 * POSIX 1003.1g mandates this order.
567 */
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800568 rc = sock_error(sk);
569 if (rc)
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300570 break;
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300571 rc = 0;
572 if (sk->sk_shutdown & RCV_SHUTDOWN)
573 break;
574 rc = -EAGAIN;
575 if (!timeo)
576 break;
577 rc = sock_intr_errno(timeo);
578 if (signal_pending(current))
579 break;
580 rc = 0;
581 if (sk_wait_data(sk, &timeo))
582 break;
583 }
584 return rc;
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/**
588 * llc_ui_accept - accept a new incoming connection.
589 * @sock: Socket which connections arrive on.
590 * @newsock: Socket to move incoming connection to.
591 * @flags: User specified operational flags.
592 *
593 * Accept a new incoming connection.
594 * Returns 0 upon success, negative otherwise.
595 */
596static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags)
597{
598 struct sock *sk = sock->sk, *newsk;
599 struct llc_sock *llc, *newllc;
600 struct sk_buff *skb;
601 int rc = -EOPNOTSUPP;
602
603 dprintk("%s: accepting on %02X\n", __FUNCTION__,
604 llc_sk(sk)->laddr.lsap);
605 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300606 if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goto out;
608 rc = -EINVAL;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300609 if (unlikely(sock->state != SS_UNCONNECTED ||
610 sk->sk_state != TCP_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto out;
612 /* wait for a connection to arrive. */
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300613 if (skb_queue_empty(&sk->sk_receive_queue)) {
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300614 rc = llc_wait_data(sk, sk->sk_rcvtimeo);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300615 if (rc)
616 goto out;
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 dprintk("%s: got a new connection on %02X\n", __FUNCTION__,
619 llc_sk(sk)->laddr.lsap);
620 skb = skb_dequeue(&sk->sk_receive_queue);
621 rc = -EINVAL;
622 if (!skb->sk)
623 goto frees;
624 rc = 0;
625 newsk = skb->sk;
626 /* attach connection to a new socket. */
627 llc_ui_sk_init(newsock, newsk);
628 sock_reset_flag(newsk, SOCK_ZAPPED);
629 newsk->sk_state = TCP_ESTABLISHED;
630 newsock->state = SS_CONNECTED;
631 llc = llc_sk(sk);
632 newllc = llc_sk(newsk);
633 memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
634 newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);
635
636 /* put original socket back into a clean listen state. */
637 sk->sk_state = TCP_LISTEN;
638 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 dprintk("%s: ok success on %02X, client on %02X\n", __FUNCTION__,
640 llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
641frees:
642 kfree_skb(skb);
643out:
644 release_sock(sk);
645 return rc;
646}
647
648/**
649 * llc_ui_recvmsg - copy received data to the socket user.
650 * @sock: Socket to copy data from.
651 * @msg: Various user space related information.
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300652 * @len: Size of user buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * @flags: User specified flags.
654 *
655 * Copy received data to the socket user.
656 * Returns non-negative upon success, negative otherwise.
657 */
658static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300659 struct msghdr *msg, size_t len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct sockaddr_llc *uaddr = (struct sockaddr_llc *)msg->msg_name;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300662 const int nonblock = flags & MSG_DONTWAIT;
663 struct sk_buff *skb = NULL;
664 struct sock *sk = sock->sk;
665 struct llc_sock *llc = llc_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 size_t copied = 0;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300667 u32 peek_seq = 0;
668 u32 *seq;
669 unsigned long used;
670 int target; /* Read at least this many bytes */
671 long timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 lock_sock(sk);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300674 copied = -ENOTCONN;
675 if (sk->sk_state == TCP_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto out;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300677
678 timeo = sock_rcvtimeo(sk, nonblock);
679
680 seq = &llc->copied_seq;
681 if (flags & MSG_PEEK) {
682 peek_seq = llc->copied_seq;
683 seq = &peek_seq;
684 }
685
686 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
687 copied = 0;
688
689 do {
690 u32 offset;
691
692 /*
693 * We need to check signals first, to get correct SIGURG
694 * handling. FIXME: Need to check this doesn't impact 1003.1g
695 * and move it down to the bottom of the loop
696 */
697 if (signal_pending(current)) {
698 if (copied)
699 break;
700 copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
701 break;
702 }
703
704 /* Next get a buffer. */
705
706 skb = skb_peek(&sk->sk_receive_queue);
707 if (skb) {
708 offset = *seq;
709 goto found_ok_skb;
710 }
711 /* Well, if we have backlog, try to process it now yet. */
712
713 if (copied >= target && !sk->sk_backlog.tail)
714 break;
715
716 if (copied) {
717 if (sk->sk_err ||
718 sk->sk_state == TCP_CLOSE ||
719 (sk->sk_shutdown & RCV_SHUTDOWN) ||
720 !timeo ||
721 (flags & MSG_PEEK))
722 break;
723 } else {
724 if (sock_flag(sk, SOCK_DONE))
725 break;
726
727 if (sk->sk_err) {
728 copied = sock_error(sk);
729 break;
730 }
731 if (sk->sk_shutdown & RCV_SHUTDOWN)
732 break;
733
734 if (sk->sk_state == TCP_CLOSE) {
735 if (!sock_flag(sk, SOCK_DONE)) {
736 /*
737 * This occurs when user tries to read
738 * from never connected socket.
739 */
740 copied = -ENOTCONN;
741 break;
742 }
743 break;
744 }
745 if (!timeo) {
746 copied = -EAGAIN;
747 break;
748 }
749 }
750
751 if (copied >= target) { /* Do not sleep, just process backlog. */
752 release_sock(sk);
753 lock_sock(sk);
754 } else
755 sk_wait_data(sk, &timeo);
756
757 if ((flags & MSG_PEEK) && peek_seq != llc->copied_seq) {
758 if (net_ratelimit())
759 printk(KERN_DEBUG "LLC(%s:%d): Application "
760 "bug, race in MSG_PEEK.\n",
761 current->comm, current->pid);
762 peek_seq = llc->copied_seq;
763 }
764 continue;
765 found_ok_skb:
766 /* Ok so how much can we use? */
767 used = skb->len - offset;
768 if (len < used)
769 used = len;
770
771 if (!(flags & MSG_TRUNC)) {
772 int rc = skb_copy_datagram_iovec(skb, offset,
773 msg->msg_iov, used);
774 if (rc) {
775 /* Exception. Bailout! */
776 if (!copied)
777 copied = -EFAULT;
778 break;
779 }
780 }
781
782 *seq += used;
783 copied += used;
784 len -= used;
785
786 if (used + offset < skb->len)
787 continue;
788
789 if (!(flags & MSG_PEEK)) {
790 sk_eat_skb(sk, skb);
791 *seq = 0;
792 }
793 } while (len > 0);
794
795 /*
796 * According to UNIX98, msg_name/msg_namelen are ignored
797 * on connected socket. -ANK
798 * But... af_llc still doesn't have separate sets of methods for
799 * SOCK_DGRAM and SOCK_STREAM :-( So we have to do this test, will
800 * eventually fix this tho :-) -acme
801 */
802 if (sk->sk_type == SOCK_DGRAM)
803 goto copy_uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804out:
805 release_sock(sk);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300806 return copied;
807copy_uaddr:
808 if (uaddr != NULL && skb != NULL) {
809 memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
810 msg->msg_namelen = sizeof(*uaddr);
811 }
812 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815/**
816 * llc_ui_sendmsg - Transmit data provided by the socket user.
817 * @sock: Socket to transmit data from.
818 * @msg: Various user related information.
819 * @len: Length of data to transmit.
820 *
821 * Transmit data provided by the socket user.
822 * Returns non-negative upon success, negative otherwise.
823 */
824static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock,
825 struct msghdr *msg, size_t len)
826{
827 struct sock *sk = sock->sk;
828 struct llc_sock *llc = llc_sk(sk);
829 struct sockaddr_llc *addr = (struct sockaddr_llc *)msg->msg_name;
830 int flags = msg->msg_flags;
831 int noblock = flags & MSG_DONTWAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct sk_buff *skb;
833 size_t size = 0;
834 int rc = -EINVAL, copied = 0, hdrlen;
835
836 dprintk("%s: sending from %02X to %02X\n", __FUNCTION__,
837 llc->laddr.lsap, llc->daddr.lsap);
838 lock_sock(sk);
839 if (addr) {
840 if (msg->msg_namelen < sizeof(*addr))
841 goto release;
842 } else {
843 if (llc_ui_addr_null(&llc->addr))
844 goto release;
845 addr = &llc->addr;
846 }
847 /* must bind connection to sap if user hasn't done it. */
848 if (sock_flag(sk, SOCK_ZAPPED)) {
849 /* bind to sap with null dev, exclusive. */
850 rc = llc_ui_autobind(sock, addr);
851 if (rc)
852 goto release;
853 }
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300854 hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 size = hdrlen + len;
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300856 if (size > llc->dev->mtu)
857 size = llc->dev->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 copied = size - hdrlen;
859 release_sock(sk);
860 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
861 lock_sock(sk);
862 if (!skb)
863 goto release;
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300864 skb->dev = llc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 skb->protocol = llc_proto_type(addr->sllc_arphrd);
866 skb_reserve(skb, hdrlen);
867 rc = memcpy_fromiovec(skb_put(skb, copied), msg->msg_iov, copied);
868 if (rc)
869 goto out;
870 if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
871 llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
872 addr->sllc_sap);
873 goto out;
874 }
875 if (addr->sllc_test) {
876 llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
877 addr->sllc_sap);
878 goto out;
879 }
880 if (addr->sllc_xid) {
881 llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
882 addr->sllc_sap);
883 goto out;
884 }
885 rc = -ENOPROTOOPT;
886 if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
887 goto out;
888 rc = llc_ui_send_data(sk, skb, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889out:
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300890 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 kfree_skb(skb);
892release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 dprintk("%s: failed sending from %02X to %02X: %d\n",
894 __FUNCTION__, llc->laddr.lsap, llc->daddr.lsap, rc);
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 release_sock(sk);
897 return rc ? : copied;
898}
899
900/**
901 * llc_ui_getname - return the address info of a socket
902 * @sock: Socket to get address of.
903 * @uaddr: Address structure to return information.
904 * @uaddrlen: Length of address structure.
905 * @peer: Does user want local or remote address information.
906 *
907 * Return the address information of a socket.
908 */
909static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
910 int *uaddrlen, int peer)
911{
912 struct sockaddr_llc sllc;
913 struct sock *sk = sock->sk;
914 struct llc_sock *llc = llc_sk(sk);
915 int rc = 0;
916
917 lock_sock(sk);
918 if (sock_flag(sk, SOCK_ZAPPED))
919 goto out;
920 *uaddrlen = sizeof(sllc);
921 memset(uaddr, 0, *uaddrlen);
922 if (peer) {
923 rc = -ENOTCONN;
924 if (sk->sk_state != TCP_ESTABLISHED)
925 goto out;
926 if(llc->dev)
927 sllc.sllc_arphrd = llc->dev->type;
928 sllc.sllc_sap = llc->daddr.lsap;
929 memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
930 } else {
931 rc = -EINVAL;
932 if (!llc->sap)
933 goto out;
934 sllc.sllc_sap = llc->sap->laddr.lsap;
935
936 if (llc->dev) {
937 sllc.sllc_arphrd = llc->dev->type;
938 memcpy(&sllc.sllc_mac, &llc->dev->dev_addr,
939 IFHWADDRLEN);
940 }
941 }
942 rc = 0;
943 sllc.sllc_family = AF_LLC;
944 memcpy(uaddr, &sllc, sizeof(sllc));
945out:
946 release_sock(sk);
947 return rc;
948}
949
950/**
951 * llc_ui_ioctl - io controls for PF_LLC
952 * @sock: Socket to get/set info
953 * @cmd: command
954 * @arg: optional argument for cmd
955 *
956 * get/set info on llc sockets
957 */
958static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
959 unsigned long arg)
960{
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800961 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/**
965 * llc_ui_setsockopt - set various connection specific parameters.
966 * @sock: Socket to set options on.
967 * @level: Socket level user is requesting operations on.
968 * @optname: Operation name.
969 * @optval User provided operation data.
970 * @optlen: Length of optval.
971 *
972 * Set various connection specific parameters.
973 */
974static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
975 char __user *optval, int optlen)
976{
977 struct sock *sk = sock->sk;
978 struct llc_sock *llc = llc_sk(sk);
979 int rc = -EINVAL, opt;
980
981 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300982 if (unlikely(level != SOL_LLC || optlen != sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto out;
984 rc = get_user(opt, (int __user *)optval);
985 if (rc)
986 goto out;
987 rc = -EINVAL;
988 switch (optname) {
989 case LLC_OPT_RETRY:
990 if (opt > LLC_OPT_MAX_RETRY)
991 goto out;
992 llc->n2 = opt;
993 break;
994 case LLC_OPT_SIZE:
995 if (opt > LLC_OPT_MAX_SIZE)
996 goto out;
997 llc->n1 = opt;
998 break;
999 case LLC_OPT_ACK_TMR_EXP:
1000 if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
1001 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001002 llc->ack_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004 case LLC_OPT_P_TMR_EXP:
1005 if (opt > LLC_OPT_MAX_P_TMR_EXP)
1006 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001007 llc->pf_cycle_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 break;
1009 case LLC_OPT_REJ_TMR_EXP:
1010 if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
1011 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001012 llc->rej_sent_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 case LLC_OPT_BUSY_TMR_EXP:
1015 if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
1016 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001017 llc->busy_state_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
1019 case LLC_OPT_TX_WIN:
1020 if (opt > LLC_OPT_MAX_WIN)
1021 goto out;
1022 llc->k = opt;
1023 break;
1024 case LLC_OPT_RX_WIN:
1025 if (opt > LLC_OPT_MAX_WIN)
1026 goto out;
1027 llc->rw = opt;
1028 break;
1029 default:
1030 rc = -ENOPROTOOPT;
1031 goto out;
1032 }
1033 rc = 0;
1034out:
1035 release_sock(sk);
1036 return rc;
1037}
1038
1039/**
1040 * llc_ui_getsockopt - get connection specific socket info
1041 * @sock: Socket to get information from.
1042 * @level: Socket level user is requesting operations on.
1043 * @optname: Operation name.
1044 * @optval: Variable to return operation data in.
1045 * @optlen: Length of optval.
1046 *
1047 * Get connection specific socket information.
1048 */
1049static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
1050 char __user *optval, int __user *optlen)
1051{
1052 struct sock *sk = sock->sk;
1053 struct llc_sock *llc = llc_sk(sk);
1054 int val = 0, len = 0, rc = -EINVAL;
1055
1056 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -03001057 if (unlikely(level != SOL_LLC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto out;
1059 rc = get_user(len, optlen);
1060 if (rc)
1061 goto out;
1062 rc = -EINVAL;
1063 if (len != sizeof(int))
1064 goto out;
1065 switch (optname) {
1066 case LLC_OPT_RETRY:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001067 val = llc->n2; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 case LLC_OPT_SIZE:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001069 val = llc->n1; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 case LLC_OPT_ACK_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001071 val = llc->ack_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 case LLC_OPT_P_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001073 val = llc->pf_cycle_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 case LLC_OPT_REJ_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001075 val = llc->rej_sent_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 case LLC_OPT_BUSY_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001077 val = llc->busy_state_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 case LLC_OPT_TX_WIN:
1079 val = llc->k; break;
1080 case LLC_OPT_RX_WIN:
1081 val = llc->rw; break;
1082 default:
1083 rc = -ENOPROTOOPT;
1084 goto out;
1085 }
1086 rc = 0;
1087 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1088 rc = -EFAULT;
1089out:
1090 release_sock(sk);
1091 return rc;
1092}
1093
1094static struct net_proto_family llc_ui_family_ops = {
1095 .family = PF_LLC,
1096 .create = llc_ui_create,
1097 .owner = THIS_MODULE,
1098};
1099
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001100static const struct proto_ops llc_ui_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 .family = PF_LLC,
1102 .owner = THIS_MODULE,
1103 .release = llc_ui_release,
1104 .bind = llc_ui_bind,
1105 .connect = llc_ui_connect,
1106 .socketpair = sock_no_socketpair,
1107 .accept = llc_ui_accept,
1108 .getname = llc_ui_getname,
1109 .poll = datagram_poll,
1110 .ioctl = llc_ui_ioctl,
1111 .listen = llc_ui_listen,
1112 .shutdown = llc_ui_shutdown,
1113 .setsockopt = llc_ui_setsockopt,
1114 .getsockopt = llc_ui_getsockopt,
1115 .sendmsg = llc_ui_sendmsg,
1116 .recvmsg = llc_ui_recvmsg,
1117 .mmap = sock_no_mmap,
1118 .sendpage = sock_no_sendpage,
1119};
1120
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001121static char llc_proc_err_msg[] __initdata =
1122 KERN_CRIT "LLC: Unable to register the proc_fs entries\n";
1123static char llc_sysctl_err_msg[] __initdata =
1124 KERN_CRIT "LLC: Unable to register the sysctl entries\n";
1125static char llc_sock_err_msg[] __initdata =
1126 KERN_CRIT "LLC: Unable to register the network family\n";
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128static int __init llc2_init(void)
1129{
1130 int rc = proto_register(&llc_proto, 0);
1131
1132 if (rc != 0)
1133 goto out;
1134
1135 llc_build_offset_table();
1136 llc_station_init();
1137 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
1138 rc = llc_proc_init();
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001139 if (rc != 0) {
1140 printk(llc_proc_err_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 goto out_unregister_llc_proto;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001142 }
1143 rc = llc_sysctl_init();
1144 if (rc) {
1145 printk(llc_sysctl_err_msg);
1146 goto out_proc;
1147 }
1148 rc = sock_register(&llc_ui_family_ops);
1149 if (rc) {
1150 printk(llc_sock_err_msg);
1151 goto out_sysctl;
1152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
1154 llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
1155out:
1156 return rc;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001157out_sysctl:
1158 llc_sysctl_exit();
1159out_proc:
1160 llc_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161out_unregister_llc_proto:
1162 proto_unregister(&llc_proto);
1163 goto out;
1164}
1165
1166static void __exit llc2_exit(void)
1167{
1168 llc_station_exit();
1169 llc_remove_pack(LLC_DEST_SAP);
1170 llc_remove_pack(LLC_DEST_CONN);
1171 sock_unregister(PF_LLC);
1172 llc_proc_exit();
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001173 llc_sysctl_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 proto_unregister(&llc_proto);
1175}
1176
1177module_init(llc2_init);
1178module_exit(llc2_exit);
1179
1180MODULE_LICENSE("GPL");
1181MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
1182MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
1183MODULE_ALIAS_NETPROTO(PF_LLC);