blob: 5bcc452a247fb54338dc1fac77563df7d6c49f4a [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 */
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -030023#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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 Meloc752f072005-08-09 20:08:28 -070032#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* remember: uninitialized global data is zeroed because its in .bss */
35static u16 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
36static u16 llc_ui_sap_link_no_max[256];
37static struct sockaddr_llc llc_ui_addrnull;
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080038static const struct proto_ops llc_ui_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -030040static int llc_ui_wait_for_conn(struct sock *sk, long timeout);
41static int llc_ui_wait_for_disc(struct sock *sk, long timeout);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -030042static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
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 Melo2342c992006-03-20 21:27:43 -080056static inline u16 llc_ui_next_link_no(int sap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
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 Viro3fbd4182006-11-08 00:26:05 -080067static inline __be16 llc_proto_type(u16 arphrd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 return arphrd == ARPHRD_IEEE802_TR ?
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +090070 htons(ETH_P_TR_802_2) : htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
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 Melo2342c992006-03-20 21:27:43 -080077static inline u8 llc_ui_addr_null(struct sockaddr_llc *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
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 Melo2342c992006-03-20 21:27:43 -080091static inline u8 llc_ui_header_len(struct sock *sk, struct sockaddr_llc *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
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 */
112static 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 Hideakid57b1862007-02-09 23:25:01 +0900117 if (unlikely(llc_data_accept_state(llc->state) ||
Jochen Friedrich451677c2005-11-14 21:57:46 -0800118 llc->remote_busy_flag ||
119 llc->p_flag)) {
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300120 long timeout = sock_sndtimeo(sk, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 rc = llc_ui_wait_for_busy_core(sk, timeout);
123 }
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300124 if (unlikely(!rc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 rc = llc_build_and_send_pkt(sk, skb);
126 return rc;
127}
128
129static void llc_ui_sk_init(struct socket *sock, struct sock *sk)
130{
David S. Milleree5850d2008-06-17 01:21:03 -0700131 sock_graft(sk, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 sk->sk_type = sock->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 sock->ops = &llc_ui_ops;
134}
135
136static struct proto llc_proto = {
Arnaldo Carvalho de Melo9c005e02006-03-20 21:27:23 -0800137 .name = "LLC",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .owner = THIS_MODULE,
139 .obj_size = sizeof(struct llc_sock),
140};
141
142/**
143 * llc_ui_create - alloc and init a new llc_ui socket
144 * @sock: Socket to initialize and attach allocated sk to.
145 * @protocol: Unused.
146 *
147 * Allocate and initialize a new llc_ui socket, validate the user wants a
148 * socket type we have available.
149 * Returns 0 upon success, negative upon failure.
150 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700151static int llc_ui_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct sock *sk;
154 int rc = -ESOCKTNOSUPPORT;
155
Patrick McHardy3480c632008-03-27 20:28:10 -0700156 if (!capable(CAP_NET_RAW))
157 return -EPERM;
158
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700159 if (net != &init_net)
160 return -EAFNOSUPPORT;
161
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300162 if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 rc = -ENOMEM;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700164 sk = llc_sk_alloc(net, PF_LLC, GFP_KERNEL, &llc_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (sk) {
166 rc = 0;
167 llc_ui_sk_init(sock, sk);
168 }
169 }
170 return rc;
171}
172
173/**
174 * llc_ui_release - shutdown socket
175 * @sock: Socket to release.
176 *
177 * Shutdown and deallocate an existing socket.
178 */
179static int llc_ui_release(struct socket *sock)
180{
181 struct sock *sk = sock->sk;
182 struct llc_sock *llc;
183
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300184 if (unlikely(sk == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto out;
186 sock_hold(sk);
187 lock_sock(sk);
188 llc = llc_sk(sk);
Harvey Harrison0dc47872008-03-05 20:47:47 -0800189 dprintk("%s: closing local(%02X) remote(%02X)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 llc->laddr.lsap, llc->daddr.lsap);
191 if (!llc_send_disc(sk))
192 llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
Arnaldo Carvalho de Melo2e1f47c2006-03-20 21:28:11 -0800193 if (!sock_flag(sk, SOCK_ZAPPED)) {
194 llc_sap_put(llc->sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 llc_sap_remove_socket(llc->sap, sk);
Arnaldo Carvalho de Melo2e1f47c2006-03-20 21:28:11 -0800196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (llc->dev)
199 dev_put(llc->dev);
200 sock_put(sk);
201 llc_sk_free(sk);
202out:
203 return 0;
204}
205
206/**
207 * llc_ui_autoport - provide dynamically allocate SAP number
208 *
209 * Provide the caller with a dynamically allocated SAP number according
210 * to the rules that are set in this function. Returns: 0, upon failure,
211 * SAP number otherwise.
212 */
213static int llc_ui_autoport(void)
214{
215 struct llc_sap *sap;
216 int i, tries = 0;
217
218 while (tries < LLC_SAP_DYN_TRIES) {
219 for (i = llc_ui_sap_last_autoport;
220 i < LLC_SAP_DYN_STOP; i += 2) {
221 sap = llc_sap_find(i);
222 if (!sap) {
223 llc_ui_sap_last_autoport = i + 2;
224 goto out;
225 }
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300226 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
229 tries++;
230 }
231 i = 0;
232out:
233 return i;
234}
235
236/**
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300237 * llc_ui_autobind - automatically bind a socket to a sap
238 * @sock: socket to bind
239 * @addr: address to connect to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300241 * Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't
242 * specifically used llc_ui_bind to bind to an specific address/sap
243 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * Returns: 0 upon success, negative otherwise.
245 */
246static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
247{
248 struct sock *sk = sock->sk;
249 struct llc_sock *llc = llc_sk(sk);
250 struct llc_sap *sap;
251 int rc = -EINVAL;
252
253 if (!sock_flag(sk, SOCK_ZAPPED))
254 goto out;
255 rc = -ENODEV;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700256 llc->dev = dev_getfirstbyhwtype(&init_net, addr->sllc_arphrd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!llc->dev)
258 goto out;
259 rc = -EUSERS;
260 llc->laddr.lsap = llc_ui_autoport();
261 if (!llc->laddr.lsap)
262 goto out;
263 rc = -EBUSY; /* some other network layer is using the sap */
264 sap = llc_sap_open(llc->laddr.lsap, NULL);
265 if (!sap)
266 goto out;
267 memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
268 memcpy(&llc->addr, addr, sizeof(llc->addr));
269 /* assign new connection to its SAP */
270 llc_sap_add_socket(sap, sk);
271 sock_reset_flag(sk, SOCK_ZAPPED);
272 rc = 0;
273out:
274 return rc;
275}
276
277/**
278 * llc_ui_bind - bind a socket to a specific address.
279 * @sock: Socket to bind an address to.
280 * @uaddr: Address the user wants the socket bound to.
281 * @addrlen: Length of the uaddr structure.
282 *
283 * 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 -0300284 * a specific sap only or mac + sap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * If the user desires to bind to a specific mac + sap, it is possible to
286 * have multiple sap connections via multiple macs.
287 * Bind and autobind for that matter must enforce the correct sap usage
288 * otherwise all hell will break loose.
289 * Returns: 0 upon success, negative otherwise.
290 */
291static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
292{
293 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
294 struct sock *sk = sock->sk;
295 struct llc_sock *llc = llc_sk(sk);
296 struct llc_sap *sap;
297 int rc = -EINVAL;
298
Harvey Harrison0dc47872008-03-05 20:47:47 -0800299 dprintk("%s: binding %02X\n", __func__, addr->sllc_sap);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300300 if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto out;
302 rc = -EAFNOSUPPORT;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300303 if (unlikely(addr->sllc_family != AF_LLC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 goto out;
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300305 rc = -ENODEV;
306 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700307 llc->dev = dev_getbyhwaddr(&init_net, addr->sllc_arphrd, addr->sllc_mac);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300308 rtnl_unlock();
309 if (!llc->dev)
310 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!addr->sllc_sap) {
312 rc = -EUSERS;
313 addr->sllc_sap = llc_ui_autoport();
314 if (!addr->sllc_sap)
315 goto out;
316 }
317 sap = llc_sap_find(addr->sllc_sap);
318 if (!sap) {
319 sap = llc_sap_open(addr->sllc_sap, NULL);
320 rc = -EBUSY; /* some other network layer is using the sap */
321 if (!sap)
322 goto out;
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300323 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 } else {
325 struct llc_addr laddr, daddr;
326 struct sock *ask;
327
328 memset(&laddr, 0, sizeof(laddr));
329 memset(&daddr, 0, sizeof(daddr));
330 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200331 * FIXME: check if the address is multicast,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * only SOCK_DGRAM can do this.
333 */
334 memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
335 laddr.lsap = addr->sllc_sap;
336 rc = -EADDRINUSE; /* mac + sap clash. */
337 ask = llc_lookup_established(sap, &daddr, &laddr);
338 if (ask) {
339 sock_put(ask);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300340 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
343 llc->laddr.lsap = addr->sllc_sap;
344 memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
345 memcpy(&llc->addr, addr, sizeof(llc->addr));
346 /* assign new connection to its SAP */
347 llc_sap_add_socket(sap, sk);
348 sock_reset_flag(sk, SOCK_ZAPPED);
349 rc = 0;
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300350out_put:
351 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352out:
353 return rc;
354}
355
356/**
357 * llc_ui_shutdown - shutdown a connect llc2 socket.
358 * @sock: Socket to shutdown.
359 * @how: What part of the socket to shutdown.
360 *
361 * Shutdown a connected llc2 socket. Currently this function only supports
362 * shutting down both sends and receives (2), we could probably make this
363 * function such that a user can shutdown only half the connection but not
364 * right now.
365 * Returns: 0 upon success, negative otherwise.
366 */
367static int llc_ui_shutdown(struct socket *sock, int how)
368{
369 struct sock *sk = sock->sk;
370 int rc = -ENOTCONN;
371
372 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300373 if (unlikely(sk->sk_state != TCP_ESTABLISHED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto out;
375 rc = -EINVAL;
376 if (how != 2)
377 goto out;
378 rc = llc_send_disc(sk);
379 if (!rc)
380 rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
381 /* Wake up anyone sleeping in poll */
382 sk->sk_state_change(sk);
383out:
384 release_sock(sk);
385 return rc;
386}
387
388/**
389 * llc_ui_connect - Connect to a remote llc2 mac + sap.
390 * @sock: Socket which will be connected to the remote destination.
391 * @uaddr: Remote and possibly the local address of the new connection.
392 * @addrlen: Size of uaddr structure.
393 * @flags: Operational flags specified by the user.
394 *
395 * Connect to a remote llc2 mac + sap. The caller must specify the
396 * destination mac and address to connect to. If the user hasn't previously
397 * called bind(2) with a smac the address of the first interface of the
398 * specified arp type will be used.
399 * This function will autobind if user did not previously call bind.
400 * Returns: 0 upon success, negative otherwise.
401 */
402static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
403 int addrlen, int flags)
404{
405 struct sock *sk = sock->sk;
406 struct llc_sock *llc = llc_sk(sk);
407 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int rc = -EINVAL;
409
410 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300411 if (unlikely(addrlen != sizeof(*addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto out;
413 rc = -EAFNOSUPPORT;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300414 if (unlikely(addr->sllc_family != AF_LLC))
415 goto out;
416 if (unlikely(sk->sk_type != SOCK_STREAM))
417 goto out;
418 rc = -EALREADY;
419 if (unlikely(sock->state == SS_CONNECTING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto out;
421 /* bind connection to sap if user hasn't done it. */
422 if (sock_flag(sk, SOCK_ZAPPED)) {
423 /* bind to sap with null dev, exclusive */
424 rc = llc_ui_autobind(sock, addr);
425 if (rc)
426 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Jochen Friedrich5564af22005-09-22 04:46:44 -0300428 llc->daddr.lsap = addr->sllc_sap;
429 memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 sock->state = SS_CONNECTING;
431 sk->sk_state = TCP_SYN_SENT;
432 llc->link = llc_ui_next_link_no(llc->sap->laddr.lsap);
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300433 rc = llc_establish_connection(sk, llc->dev->dev_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 addr->sllc_mac, addr->sllc_sap);
435 if (rc) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800436 dprintk("%s: llc_ui_send_conn failed :-(\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 sock->state = SS_UNCONNECTED;
438 sk->sk_state = TCP_CLOSE;
439 goto out;
440 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300441
442 if (sk->sk_state == TCP_SYN_SENT) {
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300443 const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300444
445 if (!timeo || !llc_ui_wait_for_conn(sk, timeo))
446 goto out;
447
448 rc = sock_intr_errno(timeo);
449 if (signal_pending(current))
450 goto out;
451 }
452
453 if (sk->sk_state == TCP_CLOSE)
454 goto sock_error;
455
456 sock->state = SS_CONNECTED;
457 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458out:
459 release_sock(sk);
460 return rc;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300461sock_error:
462 rc = sock_error(sk) ? : -ECONNABORTED;
463 sock->state = SS_UNCONNECTED;
464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467/**
468 * llc_ui_listen - allow a normal socket to accept incoming connections
469 * @sock: Socket to allow incoming connections on.
470 * @backlog: Number of connections to queue.
471 *
472 * Allow a normal socket to accept incoming connections.
473 * Returns 0 upon success, negative otherwise.
474 */
475static int llc_ui_listen(struct socket *sock, int backlog)
476{
477 struct sock *sk = sock->sk;
478 int rc = -EINVAL;
479
480 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300481 if (unlikely(sock->state != SS_UNCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto out;
483 rc = -EOPNOTSUPP;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300484 if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto out;
486 rc = -EAGAIN;
487 if (sock_flag(sk, SOCK_ZAPPED))
488 goto out;
489 rc = 0;
490 if (!(unsigned)backlog) /* BSDism */
491 backlog = 1;
492 sk->sk_max_ack_backlog = backlog;
493 if (sk->sk_state != TCP_LISTEN) {
494 sk->sk_ack_backlog = 0;
495 sk->sk_state = TCP_LISTEN;
496 }
497 sk->sk_socket->flags |= __SO_ACCEPTCON;
498out:
499 release_sock(sk);
500 return rc;
501}
502
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300503static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300505 DEFINE_WAIT(wait);
506 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300508 while (1) {
509 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
510 if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE))
511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 rc = -ERESTARTSYS;
513 if (signal_pending(current))
514 break;
515 rc = -EAGAIN;
516 if (!timeout)
517 break;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300518 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300520 finish_wait(sk->sk_sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return rc;
522}
523
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300524static int llc_ui_wait_for_conn(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300526 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300528 while (1) {
529 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
530 if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT))
531 break;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300532 if (signal_pending(current) || !timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300535 finish_wait(sk->sk_sleep, &wait);
536 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300539static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300541 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct llc_sock *llc = llc_sk(sk);
543 int rc;
544
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300545 while (1) {
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300546 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 rc = 0;
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300548 if (sk_wait_event(sk, &timeout,
549 (sk->sk_shutdown & RCV_SHUTDOWN) ||
550 (!llc_data_accept_state(llc->state) &&
Jochen Friedrich451677c2005-11-14 21:57:46 -0800551 !llc->remote_busy_flag &&
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300552 !llc->p_flag)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 rc = -ERESTARTSYS;
555 if (signal_pending(current))
556 break;
557 rc = -EAGAIN;
558 if (!timeout)
559 break;
560 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300561 finish_wait(sk->sk_sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return rc;
563}
564
Arnaldo Carvalho de Melo2928c192005-09-22 05:14:33 -0300565static int llc_wait_data(struct sock *sk, long timeo)
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300566{
567 int rc;
568
569 while (1) {
570 /*
571 * POSIX 1003.1g mandates this order.
572 */
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800573 rc = sock_error(sk);
574 if (rc)
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300575 break;
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300576 rc = 0;
577 if (sk->sk_shutdown & RCV_SHUTDOWN)
578 break;
579 rc = -EAGAIN;
580 if (!timeo)
581 break;
582 rc = sock_intr_errno(timeo);
583 if (signal_pending(current))
584 break;
585 rc = 0;
586 if (sk_wait_data(sk, &timeo))
587 break;
588 }
589 return rc;
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/**
593 * llc_ui_accept - accept a new incoming connection.
594 * @sock: Socket which connections arrive on.
595 * @newsock: Socket to move incoming connection to.
596 * @flags: User specified operational flags.
597 *
598 * Accept a new incoming connection.
599 * Returns 0 upon success, negative otherwise.
600 */
601static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags)
602{
603 struct sock *sk = sock->sk, *newsk;
604 struct llc_sock *llc, *newllc;
605 struct sk_buff *skb;
606 int rc = -EOPNOTSUPP;
607
Harvey Harrison0dc47872008-03-05 20:47:47 -0800608 dprintk("%s: accepting on %02X\n", __func__,
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900609 llc_sk(sk)->laddr.lsap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300611 if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto out;
613 rc = -EINVAL;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300614 if (unlikely(sock->state != SS_UNCONNECTED ||
615 sk->sk_state != TCP_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto out;
617 /* wait for a connection to arrive. */
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300618 if (skb_queue_empty(&sk->sk_receive_queue)) {
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300619 rc = llc_wait_data(sk, sk->sk_rcvtimeo);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300620 if (rc)
621 goto out;
622 }
Harvey Harrison0dc47872008-03-05 20:47:47 -0800623 dprintk("%s: got a new connection on %02X\n", __func__,
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900624 llc_sk(sk)->laddr.lsap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 skb = skb_dequeue(&sk->sk_receive_queue);
626 rc = -EINVAL;
627 if (!skb->sk)
628 goto frees;
629 rc = 0;
630 newsk = skb->sk;
631 /* attach connection to a new socket. */
632 llc_ui_sk_init(newsock, newsk);
633 sock_reset_flag(newsk, SOCK_ZAPPED);
634 newsk->sk_state = TCP_ESTABLISHED;
635 newsock->state = SS_CONNECTED;
636 llc = llc_sk(sk);
637 newllc = llc_sk(newsk);
638 memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
639 newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);
640
641 /* put original socket back into a clean listen state. */
642 sk->sk_state = TCP_LISTEN;
643 sk->sk_ack_backlog--;
Harvey Harrison0dc47872008-03-05 20:47:47 -0800644 dprintk("%s: ok success on %02X, client on %02X\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
646frees:
647 kfree_skb(skb);
648out:
649 release_sock(sk);
650 return rc;
651}
652
653/**
654 * llc_ui_recvmsg - copy received data to the socket user.
655 * @sock: Socket to copy data from.
656 * @msg: Various user space related information.
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300657 * @len: Size of user buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * @flags: User specified flags.
659 *
660 * Copy received data to the socket user.
661 * Returns non-negative upon success, negative otherwise.
662 */
663static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300664 struct msghdr *msg, size_t len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct sockaddr_llc *uaddr = (struct sockaddr_llc *)msg->msg_name;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300667 const int nonblock = flags & MSG_DONTWAIT;
668 struct sk_buff *skb = NULL;
669 struct sock *sk = sock->sk;
670 struct llc_sock *llc = llc_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 size_t copied = 0;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300672 u32 peek_seq = 0;
673 u32 *seq;
674 unsigned long used;
675 int target; /* Read at least this many bytes */
676 long timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 lock_sock(sk);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300679 copied = -ENOTCONN;
Stephen Hemminger29efcd22006-05-25 15:08:59 -0700680 if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto out;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300682
683 timeo = sock_rcvtimeo(sk, nonblock);
684
685 seq = &llc->copied_seq;
686 if (flags & MSG_PEEK) {
687 peek_seq = llc->copied_seq;
688 seq = &peek_seq;
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900689 }
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300690
691 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
692 copied = 0;
693
694 do {
695 u32 offset;
696
697 /*
698 * We need to check signals first, to get correct SIGURG
699 * handling. FIXME: Need to check this doesn't impact 1003.1g
700 * and move it down to the bottom of the loop
701 */
702 if (signal_pending(current)) {
703 if (copied)
704 break;
705 copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
706 break;
707 }
708
709 /* Next get a buffer. */
710
711 skb = skb_peek(&sk->sk_receive_queue);
712 if (skb) {
713 offset = *seq;
714 goto found_ok_skb;
715 }
716 /* Well, if we have backlog, try to process it now yet. */
717
718 if (copied >= target && !sk->sk_backlog.tail)
719 break;
720
721 if (copied) {
722 if (sk->sk_err ||
723 sk->sk_state == TCP_CLOSE ||
724 (sk->sk_shutdown & RCV_SHUTDOWN) ||
725 !timeo ||
726 (flags & MSG_PEEK))
727 break;
728 } else {
729 if (sock_flag(sk, SOCK_DONE))
730 break;
731
732 if (sk->sk_err) {
733 copied = sock_error(sk);
734 break;
735 }
736 if (sk->sk_shutdown & RCV_SHUTDOWN)
737 break;
738
Stephen Hemminger29efcd22006-05-25 15:08:59 -0700739 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSE) {
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300740 if (!sock_flag(sk, SOCK_DONE)) {
741 /*
742 * This occurs when user tries to read
743 * from never connected socket.
744 */
745 copied = -ENOTCONN;
746 break;
747 }
748 break;
749 }
750 if (!timeo) {
751 copied = -EAGAIN;
752 break;
753 }
754 }
755
756 if (copied >= target) { /* Do not sleep, just process backlog. */
757 release_sock(sk);
758 lock_sock(sk);
759 } else
760 sk_wait_data(sk, &timeo);
761
762 if ((flags & MSG_PEEK) && peek_seq != llc->copied_seq) {
763 if (net_ratelimit())
764 printk(KERN_DEBUG "LLC(%s:%d): Application "
765 "bug, race in MSG_PEEK.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700766 current->comm, task_pid_nr(current));
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300767 peek_seq = llc->copied_seq;
768 }
769 continue;
770 found_ok_skb:
771 /* Ok so how much can we use? */
772 used = skb->len - offset;
773 if (len < used)
774 used = len;
775
776 if (!(flags & MSG_TRUNC)) {
777 int rc = skb_copy_datagram_iovec(skb, offset,
778 msg->msg_iov, used);
779 if (rc) {
780 /* Exception. Bailout! */
781 if (!copied)
782 copied = -EFAULT;
783 break;
784 }
785 }
786
787 *seq += used;
788 copied += used;
789 len -= used;
790
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300791 if (!(flags & MSG_PEEK)) {
Chris Leech624d1162006-05-23 18:01:28 -0700792 sk_eat_skb(sk, skb, 0);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300793 *seq = 0;
794 }
Stephen Hemminger30a584d2006-08-03 16:38:49 -0700795
796 /* For non stream protcols we get one packet per recvmsg call */
797 if (sk->sk_type != SOCK_STREAM)
798 goto copy_uaddr;
799
800 /* Partial read */
801 if (used + offset < skb->len)
802 continue;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300803 } while (len > 0);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805out:
806 release_sock(sk);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300807 return copied;
808copy_uaddr:
809 if (uaddr != NULL && skb != NULL) {
810 memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
811 msg->msg_namelen = sizeof(*uaddr);
812 }
813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816/**
817 * llc_ui_sendmsg - Transmit data provided by the socket user.
818 * @sock: Socket to transmit data from.
819 * @msg: Various user related information.
820 * @len: Length of data to transmit.
821 *
822 * Transmit data provided by the socket user.
823 * Returns non-negative upon success, negative otherwise.
824 */
825static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock,
826 struct msghdr *msg, size_t len)
827{
828 struct sock *sk = sock->sk;
829 struct llc_sock *llc = llc_sk(sk);
830 struct sockaddr_llc *addr = (struct sockaddr_llc *)msg->msg_name;
831 int flags = msg->msg_flags;
832 int noblock = flags & MSG_DONTWAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct sk_buff *skb;
834 size_t size = 0;
835 int rc = -EINVAL, copied = 0, hdrlen;
836
Harvey Harrison0dc47872008-03-05 20:47:47 -0800837 dprintk("%s: sending from %02X to %02X\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 llc->laddr.lsap, llc->daddr.lsap);
839 lock_sock(sk);
840 if (addr) {
841 if (msg->msg_namelen < sizeof(*addr))
842 goto release;
843 } else {
844 if (llc_ui_addr_null(&llc->addr))
845 goto release;
846 addr = &llc->addr;
847 }
848 /* must bind connection to sap if user hasn't done it. */
849 if (sock_flag(sk, SOCK_ZAPPED)) {
850 /* bind to sap with null dev, exclusive. */
851 rc = llc_ui_autobind(sock, addr);
852 if (rc)
853 goto release;
854 }
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300855 hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 size = hdrlen + len;
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300857 if (size > llc->dev->mtu)
858 size = llc->dev->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 copied = size - hdrlen;
860 release_sock(sk);
861 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
862 lock_sock(sk);
863 if (!skb)
864 goto release;
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300865 skb->dev = llc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 skb->protocol = llc_proto_type(addr->sllc_arphrd);
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900867 skb_reserve(skb, hdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 rc = memcpy_fromiovec(skb_put(skb, copied), msg->msg_iov, copied);
869 if (rc)
870 goto out;
871 if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
872 llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
873 addr->sllc_sap);
874 goto out;
875 }
876 if (addr->sllc_test) {
877 llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
878 addr->sllc_sap);
879 goto out;
880 }
881 if (addr->sllc_xid) {
882 llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
883 addr->sllc_sap);
884 goto out;
885 }
886 rc = -ENOPROTOOPT;
887 if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
888 goto out;
889 rc = llc_ui_send_data(sk, skb, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890out:
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300891 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 kfree_skb(skb);
893release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 dprintk("%s: failed sending from %02X to %02X: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800895 __func__, llc->laddr.lsap, llc->daddr.lsap, rc);
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 release_sock(sk);
898 return rc ? : copied;
899}
900
901/**
902 * llc_ui_getname - return the address info of a socket
903 * @sock: Socket to get address of.
904 * @uaddr: Address structure to return information.
905 * @uaddrlen: Length of address structure.
906 * @peer: Does user want local or remote address information.
907 *
908 * Return the address information of a socket.
909 */
910static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
911 int *uaddrlen, int peer)
912{
913 struct sockaddr_llc sllc;
914 struct sock *sk = sock->sk;
915 struct llc_sock *llc = llc_sk(sk);
916 int rc = 0;
917
918 lock_sock(sk);
919 if (sock_flag(sk, SOCK_ZAPPED))
920 goto out;
921 *uaddrlen = sizeof(sllc);
922 memset(uaddr, 0, *uaddrlen);
923 if (peer) {
924 rc = -ENOTCONN;
925 if (sk->sk_state != TCP_ESTABLISHED)
926 goto out;
927 if(llc->dev)
928 sllc.sllc_arphrd = llc->dev->type;
929 sllc.sllc_sap = llc->daddr.lsap;
930 memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
931 } else {
932 rc = -EINVAL;
933 if (!llc->sap)
934 goto out;
935 sllc.sllc_sap = llc->sap->laddr.lsap;
936
937 if (llc->dev) {
938 sllc.sllc_arphrd = llc->dev->type;
939 memcpy(&sllc.sllc_mac, &llc->dev->dev_addr,
940 IFHWADDRLEN);
941 }
942 }
943 rc = 0;
944 sllc.sllc_family = AF_LLC;
945 memcpy(uaddr, &sllc, sizeof(sllc));
946out:
947 release_sock(sk);
948 return rc;
949}
950
951/**
952 * llc_ui_ioctl - io controls for PF_LLC
953 * @sock: Socket to get/set info
954 * @cmd: command
955 * @arg: optional argument for cmd
956 *
957 * get/set info on llc sockets
958 */
959static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
960 unsigned long arg)
961{
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800962 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965/**
966 * llc_ui_setsockopt - set various connection specific parameters.
967 * @sock: Socket to set options on.
968 * @level: Socket level user is requesting operations on.
969 * @optname: Operation name.
970 * @optval User provided operation data.
971 * @optlen: Length of optval.
972 *
973 * Set various connection specific parameters.
974 */
975static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
976 char __user *optval, int optlen)
977{
978 struct sock *sk = sock->sk;
979 struct llc_sock *llc = llc_sk(sk);
980 int rc = -EINVAL, opt;
981
982 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300983 if (unlikely(level != SOL_LLC || optlen != sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto out;
985 rc = get_user(opt, (int __user *)optval);
986 if (rc)
987 goto out;
988 rc = -EINVAL;
989 switch (optname) {
990 case LLC_OPT_RETRY:
991 if (opt > LLC_OPT_MAX_RETRY)
992 goto out;
993 llc->n2 = opt;
994 break;
995 case LLC_OPT_SIZE:
996 if (opt > LLC_OPT_MAX_SIZE)
997 goto out;
998 llc->n1 = opt;
999 break;
1000 case LLC_OPT_ACK_TMR_EXP:
1001 if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
1002 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001003 llc->ack_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
1005 case LLC_OPT_P_TMR_EXP:
1006 if (opt > LLC_OPT_MAX_P_TMR_EXP)
1007 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001008 llc->pf_cycle_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 break;
1010 case LLC_OPT_REJ_TMR_EXP:
1011 if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
1012 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001013 llc->rej_sent_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 break;
1015 case LLC_OPT_BUSY_TMR_EXP:
1016 if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
1017 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001018 llc->busy_state_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 break;
1020 case LLC_OPT_TX_WIN:
1021 if (opt > LLC_OPT_MAX_WIN)
1022 goto out;
1023 llc->k = opt;
1024 break;
1025 case LLC_OPT_RX_WIN:
1026 if (opt > LLC_OPT_MAX_WIN)
1027 goto out;
1028 llc->rw = opt;
1029 break;
1030 default:
1031 rc = -ENOPROTOOPT;
1032 goto out;
1033 }
1034 rc = 0;
1035out:
1036 release_sock(sk);
1037 return rc;
1038}
1039
1040/**
1041 * llc_ui_getsockopt - get connection specific socket info
1042 * @sock: Socket to get information from.
1043 * @level: Socket level user is requesting operations on.
1044 * @optname: Operation name.
1045 * @optval: Variable to return operation data in.
1046 * @optlen: Length of optval.
1047 *
1048 * Get connection specific socket information.
1049 */
1050static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
1051 char __user *optval, int __user *optlen)
1052{
1053 struct sock *sk = sock->sk;
1054 struct llc_sock *llc = llc_sk(sk);
1055 int val = 0, len = 0, rc = -EINVAL;
1056
1057 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -03001058 if (unlikely(level != SOL_LLC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 goto out;
1060 rc = get_user(len, optlen);
1061 if (rc)
1062 goto out;
1063 rc = -EINVAL;
1064 if (len != sizeof(int))
1065 goto out;
1066 switch (optname) {
1067 case LLC_OPT_RETRY:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001068 val = llc->n2; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 case LLC_OPT_SIZE:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001070 val = llc->n1; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 case LLC_OPT_ACK_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001072 val = llc->ack_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 case LLC_OPT_P_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001074 val = llc->pf_cycle_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 case LLC_OPT_REJ_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001076 val = llc->rej_sent_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 case LLC_OPT_BUSY_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001078 val = llc->busy_state_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 case LLC_OPT_TX_WIN:
1080 val = llc->k; break;
1081 case LLC_OPT_RX_WIN:
1082 val = llc->rw; break;
1083 default:
1084 rc = -ENOPROTOOPT;
1085 goto out;
1086 }
1087 rc = 0;
1088 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1089 rc = -EFAULT;
1090out:
1091 release_sock(sk);
1092 return rc;
1093}
1094
1095static struct net_proto_family llc_ui_family_ops = {
1096 .family = PF_LLC,
1097 .create = llc_ui_create,
1098 .owner = THIS_MODULE,
1099};
1100
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001101static const struct proto_ops llc_ui_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 .family = PF_LLC,
1103 .owner = THIS_MODULE,
1104 .release = llc_ui_release,
1105 .bind = llc_ui_bind,
1106 .connect = llc_ui_connect,
1107 .socketpair = sock_no_socketpair,
1108 .accept = llc_ui_accept,
1109 .getname = llc_ui_getname,
1110 .poll = datagram_poll,
1111 .ioctl = llc_ui_ioctl,
1112 .listen = llc_ui_listen,
1113 .shutdown = llc_ui_shutdown,
1114 .setsockopt = llc_ui_setsockopt,
1115 .getsockopt = llc_ui_getsockopt,
1116 .sendmsg = llc_ui_sendmsg,
1117 .recvmsg = llc_ui_recvmsg,
1118 .mmap = sock_no_mmap,
1119 .sendpage = sock_no_sendpage,
1120};
1121
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001122static char llc_proc_err_msg[] __initdata =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001123 KERN_CRIT "LLC: Unable to register the proc_fs entries\n";
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001124static char llc_sysctl_err_msg[] __initdata =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001125 KERN_CRIT "LLC: Unable to register the sysctl entries\n";
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001126static char llc_sock_err_msg[] __initdata =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001127 KERN_CRIT "LLC: Unable to register the network family\n";
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129static int __init llc2_init(void)
1130{
1131 int rc = proto_register(&llc_proto, 0);
1132
1133 if (rc != 0)
1134 goto out;
1135
1136 llc_build_offset_table();
1137 llc_station_init();
1138 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
1139 rc = llc_proc_init();
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001140 if (rc != 0) {
1141 printk(llc_proc_err_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 goto out_unregister_llc_proto;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001143 }
1144 rc = llc_sysctl_init();
1145 if (rc) {
1146 printk(llc_sysctl_err_msg);
1147 goto out_proc;
1148 }
1149 rc = sock_register(&llc_ui_family_ops);
1150 if (rc) {
1151 printk(llc_sock_err_msg);
1152 goto out_sysctl;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
1155 llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
1156out:
1157 return rc;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001158out_sysctl:
1159 llc_sysctl_exit();
1160out_proc:
1161 llc_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162out_unregister_llc_proto:
1163 proto_unregister(&llc_proto);
1164 goto out;
1165}
1166
1167static void __exit llc2_exit(void)
1168{
1169 llc_station_exit();
1170 llc_remove_pack(LLC_DEST_SAP);
1171 llc_remove_pack(LLC_DEST_CONN);
1172 sock_unregister(PF_LLC);
1173 llc_proc_exit();
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001174 llc_sysctl_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 proto_unregister(&llc_proto);
1176}
1177
1178module_init(llc2_init);
1179module_exit(llc2_exit);
1180
1181MODULE_LICENSE("GPL");
1182MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
1183MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
1184MODULE_ALIAS_NETPROTO(PF_LLC);