blob: 8364fe5b59e4ca01ef8d05b1038dbe96fedf657b [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010029#include <linux/sched/signal.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/llc.h>
32#include <net/llc_sap.h>
33#include <net/llc_pdu.h>
34#include <net/llc_conn.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070035#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* remember: uninitialized global data is zeroed because its in .bss */
38static u16 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
39static u16 llc_ui_sap_link_no_max[256];
40static struct sockaddr_llc llc_ui_addrnull;
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080041static const struct proto_ops llc_ui_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Alan Cox5ff904d2016-09-15 18:51:25 +010043static bool llc_ui_wait_for_conn(struct sock *sk, long timeout);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -030044static int llc_ui_wait_for_disc(struct sock *sk, long timeout);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -030045static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#if 0
48#define dprintk(args...) printk(KERN_DEBUG args)
49#else
50#define dprintk(args...)
51#endif
52
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +000053/* Maybe we'll add some more in the future. */
54#define LLC_CMSG_PKTINFO 1
55
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/**
58 * llc_ui_next_link_no - return the next unused link number for a sap
59 * @sap: Address of sap to get link number from.
60 *
61 * Return the next unused link number for a given sap.
62 */
Arnaldo Carvalho de Melo2342c992006-03-20 21:27:43 -080063static inline u16 llc_ui_next_link_no(int sap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 return llc_ui_sap_link_no_max[sap]++;
66}
67
68/**
69 * llc_proto_type - return eth protocol for ARP header type
70 * @arphrd: ARP header type.
71 *
72 * Given an ARP header type return the corresponding ethernet protocol.
73 */
Al Viro3fbd4182006-11-08 00:26:05 -080074static inline __be16 llc_proto_type(u16 arphrd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Paul Gortmaker211ed862012-05-10 17:14:35 -040076 return htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79/**
80 * llc_ui_addr_null - determines if a address structure is null
81 * @addr: Address to test if null.
82 */
Arnaldo Carvalho de Melo2342c992006-03-20 21:27:43 -080083static inline u8 llc_ui_addr_null(struct sockaddr_llc *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 return !memcmp(addr, &llc_ui_addrnull, sizeof(*addr));
86}
87
88/**
89 * llc_ui_header_len - return length of llc header based on operation
90 * @sk: Socket which contains a valid llc socket type.
91 * @addr: Complete sockaddr_llc structure received from the user.
92 *
93 * Provide the length of the llc header depending on what kind of
94 * operation the user would like to perform and the type of socket.
95 * Returns the correct llc header length.
96 */
Arnaldo Carvalho de Melo2342c992006-03-20 21:27:43 -080097static inline u8 llc_ui_header_len(struct sock *sk, struct sockaddr_llc *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 u8 rc = LLC_PDU_LEN_U;
100
101 if (addr->sllc_test || addr->sllc_xid)
102 rc = LLC_PDU_LEN_U;
103 else if (sk->sk_type == SOCK_STREAM)
104 rc = LLC_PDU_LEN_I;
105 return rc;
106}
107
108/**
109 * llc_ui_send_data - send data via reliable llc2 connection
110 * @sk: Connection the socket is using.
111 * @skb: Data the user wishes to send.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * @noblock: can we block waiting for data?
113 *
114 * Send data via reliable llc2 connection.
115 * Returns 0 upon success, non-zero if action did not succeed.
116 */
117static int llc_ui_send_data(struct sock* sk, struct sk_buff *skb, int noblock)
118{
119 struct llc_sock* llc = llc_sk(sk);
120 int rc = 0;
121
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900122 if (unlikely(llc_data_accept_state(llc->state) ||
Jochen Friedrich451677c2005-11-14 21:57:46 -0800123 llc->remote_busy_flag ||
124 llc->p_flag)) {
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300125 long timeout = sock_sndtimeo(sk, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 rc = llc_ui_wait_for_busy_core(sk, timeout);
128 }
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300129 if (unlikely(!rc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 rc = llc_build_and_send_pkt(sk, skb);
131 return rc;
132}
133
134static void llc_ui_sk_init(struct socket *sock, struct sock *sk)
135{
David S. Milleree5850d2008-06-17 01:21:03 -0700136 sock_graft(sk, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 sk->sk_type = sock->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 sock->ops = &llc_ui_ops;
139}
140
141static struct proto llc_proto = {
Arnaldo Carvalho de Melo9c005e02006-03-20 21:27:23 -0800142 .name = "LLC",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .owner = THIS_MODULE,
144 .obj_size = sizeof(struct llc_sock),
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800145 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148/**
149 * llc_ui_create - alloc and init a new llc_ui socket
Eric Paris3f378b62009-11-05 22:18:14 -0800150 * @net: network namespace (must be default network)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * @sock: Socket to initialize and attach allocated sk to.
152 * @protocol: Unused.
Eric Paris3f378b62009-11-05 22:18:14 -0800153 * @kern: on behalf of kernel or userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *
155 * Allocate and initialize a new llc_ui socket, validate the user wants a
156 * socket type we have available.
157 * Returns 0 upon success, negative upon failure.
158 */
Eric Paris3f378b62009-11-05 22:18:14 -0800159static int llc_ui_create(struct net *net, struct socket *sock, int protocol,
160 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct sock *sk;
163 int rc = -ESOCKTNOSUPPORT;
164
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000165 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Patrick McHardy3480c632008-03-27 20:28:10 -0700166 return -EPERM;
167
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800168 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700169 return -EAFNOSUPPORT;
170
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300171 if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 rc = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500173 sk = llc_sk_alloc(net, PF_LLC, GFP_KERNEL, &llc_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (sk) {
175 rc = 0;
176 llc_ui_sk_init(sock, sk);
177 }
178 }
179 return rc;
180}
181
182/**
183 * llc_ui_release - shutdown socket
184 * @sock: Socket to release.
185 *
186 * Shutdown and deallocate an existing socket.
187 */
188static int llc_ui_release(struct socket *sock)
189{
190 struct sock *sk = sock->sk;
191 struct llc_sock *llc;
192
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300193 if (unlikely(sk == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 goto out;
195 sock_hold(sk);
196 lock_sock(sk);
197 llc = llc_sk(sk);
Harvey Harrison0dc47872008-03-05 20:47:47 -0800198 dprintk("%s: closing local(%02X) remote(%02X)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 llc->laddr.lsap, llc->daddr.lsap);
200 if (!llc_send_disc(sk))
201 llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
Octavian Purdila3100aa92009-12-26 11:51:08 +0000202 if (!sock_flag(sk, SOCK_ZAPPED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 llc_sap_remove_socket(llc->sap, sk);
204 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (llc->dev)
206 dev_put(llc->dev);
207 sock_put(sk);
208 llc_sk_free(sk);
209out:
210 return 0;
211}
212
213/**
214 * llc_ui_autoport - provide dynamically allocate SAP number
215 *
216 * Provide the caller with a dynamically allocated SAP number according
217 * to the rules that are set in this function. Returns: 0, upon failure,
218 * SAP number otherwise.
219 */
220static int llc_ui_autoport(void)
221{
222 struct llc_sap *sap;
223 int i, tries = 0;
224
225 while (tries < LLC_SAP_DYN_TRIES) {
226 for (i = llc_ui_sap_last_autoport;
227 i < LLC_SAP_DYN_STOP; i += 2) {
228 sap = llc_sap_find(i);
229 if (!sap) {
230 llc_ui_sap_last_autoport = i + 2;
231 goto out;
232 }
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300233 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
236 tries++;
237 }
238 i = 0;
239out:
240 return i;
241}
242
243/**
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300244 * llc_ui_autobind - automatically bind a socket to a sap
245 * @sock: socket to bind
246 * @addr: address to connect to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300248 * Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't
249 * specifically used llc_ui_bind to bind to an specific address/sap
250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * Returns: 0 upon success, negative otherwise.
252 */
253static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
254{
255 struct sock *sk = sock->sk;
256 struct llc_sock *llc = llc_sk(sk);
257 struct llc_sap *sap;
258 int rc = -EINVAL;
259
260 if (!sock_flag(sk, SOCK_ZAPPED))
261 goto out;
262 rc = -ENODEV;
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000263 if (sk->sk_bound_dev_if) {
264 llc->dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
265 if (llc->dev && addr->sllc_arphrd != llc->dev->type) {
266 dev_put(llc->dev);
267 llc->dev = NULL;
268 }
269 } else
270 llc->dev = dev_getfirstbyhwtype(&init_net, addr->sllc_arphrd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (!llc->dev)
272 goto out;
273 rc = -EUSERS;
274 llc->laddr.lsap = llc_ui_autoport();
275 if (!llc->laddr.lsap)
276 goto out;
277 rc = -EBUSY; /* some other network layer is using the sap */
278 sap = llc_sap_open(llc->laddr.lsap, NULL);
279 if (!sap)
280 goto out;
281 memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
282 memcpy(&llc->addr, addr, sizeof(llc->addr));
283 /* assign new connection to its SAP */
284 llc_sap_add_socket(sap, sk);
285 sock_reset_flag(sk, SOCK_ZAPPED);
286 rc = 0;
287out:
288 return rc;
289}
290
291/**
292 * llc_ui_bind - bind a socket to a specific address.
293 * @sock: Socket to bind an address to.
294 * @uaddr: Address the user wants the socket bound to.
295 * @addrlen: Length of the uaddr structure.
296 *
297 * 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 -0300298 * a specific sap only or mac + sap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * If the user desires to bind to a specific mac + sap, it is possible to
300 * have multiple sap connections via multiple macs.
301 * Bind and autobind for that matter must enforce the correct sap usage
302 * otherwise all hell will break loose.
303 * Returns: 0 upon success, negative otherwise.
304 */
305static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
306{
307 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
308 struct sock *sk = sock->sk;
309 struct llc_sock *llc = llc_sk(sk);
310 struct llc_sap *sap;
311 int rc = -EINVAL;
312
Harvey Harrison0dc47872008-03-05 20:47:47 -0800313 dprintk("%s: binding %02X\n", __func__, addr->sllc_sap);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300314 if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto out;
316 rc = -EAFNOSUPPORT;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300317 if (unlikely(addr->sllc_family != AF_LLC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto out;
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300319 rc = -ENODEV;
Eric Dumazet941666c2010-12-05 01:23:53 +0000320 rcu_read_lock();
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000321 if (sk->sk_bound_dev_if) {
Eric Dumazet941666c2010-12-05 01:23:53 +0000322 llc->dev = dev_get_by_index_rcu(&init_net, sk->sk_bound_dev_if);
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000323 if (llc->dev) {
324 if (!addr->sllc_arphrd)
325 addr->sllc_arphrd = llc->dev->type;
Joe Perches951fd872013-09-01 13:11:55 -0700326 if (is_zero_ether_addr(addr->sllc_mac))
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000327 memcpy(addr->sllc_mac, llc->dev->dev_addr,
328 IFHWADDRLEN);
329 if (addr->sllc_arphrd != llc->dev->type ||
Joe Perches951fd872013-09-01 13:11:55 -0700330 !ether_addr_equal(addr->sllc_mac,
331 llc->dev->dev_addr)) {
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000332 rc = -EINVAL;
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000333 llc->dev = NULL;
334 }
335 }
336 } else
Eric Dumazet941666c2010-12-05 01:23:53 +0000337 llc->dev = dev_getbyhwaddr_rcu(&init_net, addr->sllc_arphrd,
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000338 addr->sllc_mac);
Eric Dumazet941666c2010-12-05 01:23:53 +0000339 if (llc->dev)
340 dev_hold(llc->dev);
341 rcu_read_unlock();
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300342 if (!llc->dev)
343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!addr->sllc_sap) {
345 rc = -EUSERS;
346 addr->sllc_sap = llc_ui_autoport();
347 if (!addr->sllc_sap)
348 goto out;
349 }
350 sap = llc_sap_find(addr->sllc_sap);
351 if (!sap) {
352 sap = llc_sap_open(addr->sllc_sap, NULL);
353 rc = -EBUSY; /* some other network layer is using the sap */
354 if (!sap)
355 goto out;
356 } else {
357 struct llc_addr laddr, daddr;
358 struct sock *ask;
359
360 memset(&laddr, 0, sizeof(laddr));
361 memset(&daddr, 0, sizeof(daddr));
362 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200363 * FIXME: check if the address is multicast,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * only SOCK_DGRAM can do this.
365 */
366 memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
367 laddr.lsap = addr->sllc_sap;
368 rc = -EADDRINUSE; /* mac + sap clash. */
369 ask = llc_lookup_established(sap, &daddr, &laddr);
370 if (ask) {
371 sock_put(ask);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300372 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 }
375 llc->laddr.lsap = addr->sllc_sap;
376 memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
377 memcpy(&llc->addr, addr, sizeof(llc->addr));
378 /* assign new connection to its SAP */
379 llc_sap_add_socket(sap, sk);
380 sock_reset_flag(sk, SOCK_ZAPPED);
381 rc = 0;
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300382out_put:
383 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384out:
385 return rc;
386}
387
388/**
389 * llc_ui_shutdown - shutdown a connect llc2 socket.
390 * @sock: Socket to shutdown.
391 * @how: What part of the socket to shutdown.
392 *
393 * Shutdown a connected llc2 socket. Currently this function only supports
394 * shutting down both sends and receives (2), we could probably make this
395 * function such that a user can shutdown only half the connection but not
396 * right now.
397 * Returns: 0 upon success, negative otherwise.
398 */
399static int llc_ui_shutdown(struct socket *sock, int how)
400{
401 struct sock *sk = sock->sk;
402 int rc = -ENOTCONN;
403
404 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300405 if (unlikely(sk->sk_state != TCP_ESTABLISHED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto out;
407 rc = -EINVAL;
408 if (how != 2)
409 goto out;
410 rc = llc_send_disc(sk);
411 if (!rc)
412 rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
413 /* Wake up anyone sleeping in poll */
414 sk->sk_state_change(sk);
415out:
416 release_sock(sk);
417 return rc;
418}
419
420/**
421 * llc_ui_connect - Connect to a remote llc2 mac + sap.
422 * @sock: Socket which will be connected to the remote destination.
423 * @uaddr: Remote and possibly the local address of the new connection.
424 * @addrlen: Size of uaddr structure.
425 * @flags: Operational flags specified by the user.
426 *
427 * Connect to a remote llc2 mac + sap. The caller must specify the
428 * destination mac and address to connect to. If the user hasn't previously
429 * called bind(2) with a smac the address of the first interface of the
430 * specified arp type will be used.
431 * This function will autobind if user did not previously call bind.
432 * Returns: 0 upon success, negative otherwise.
433 */
434static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
435 int addrlen, int flags)
436{
437 struct sock *sk = sock->sk;
438 struct llc_sock *llc = llc_sk(sk);
439 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int rc = -EINVAL;
441
442 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300443 if (unlikely(addrlen != sizeof(*addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 goto out;
445 rc = -EAFNOSUPPORT;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300446 if (unlikely(addr->sllc_family != AF_LLC))
447 goto out;
448 if (unlikely(sk->sk_type != SOCK_STREAM))
449 goto out;
450 rc = -EALREADY;
451 if (unlikely(sock->state == SS_CONNECTING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto out;
453 /* bind connection to sap if user hasn't done it. */
454 if (sock_flag(sk, SOCK_ZAPPED)) {
455 /* bind to sap with null dev, exclusive */
456 rc = llc_ui_autobind(sock, addr);
457 if (rc)
458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Jochen Friedrich5564af22005-09-22 04:46:44 -0300460 llc->daddr.lsap = addr->sllc_sap;
461 memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 sock->state = SS_CONNECTING;
463 sk->sk_state = TCP_SYN_SENT;
464 llc->link = llc_ui_next_link_no(llc->sap->laddr.lsap);
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300465 rc = llc_establish_connection(sk, llc->dev->dev_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 addr->sllc_mac, addr->sllc_sap);
467 if (rc) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800468 dprintk("%s: llc_ui_send_conn failed :-(\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 sock->state = SS_UNCONNECTED;
470 sk->sk_state = TCP_CLOSE;
471 goto out;
472 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300473
474 if (sk->sk_state == TCP_SYN_SENT) {
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300475 const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300476
477 if (!timeo || !llc_ui_wait_for_conn(sk, timeo))
478 goto out;
479
480 rc = sock_intr_errno(timeo);
481 if (signal_pending(current))
482 goto out;
483 }
484
485 if (sk->sk_state == TCP_CLOSE)
486 goto sock_error;
487
488 sock->state = SS_CONNECTED;
489 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490out:
491 release_sock(sk);
492 return rc;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300493sock_error:
494 rc = sock_error(sk) ? : -ECONNABORTED;
495 sock->state = SS_UNCONNECTED;
496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/**
500 * llc_ui_listen - allow a normal socket to accept incoming connections
501 * @sock: Socket to allow incoming connections on.
502 * @backlog: Number of connections to queue.
503 *
504 * Allow a normal socket to accept incoming connections.
505 * Returns 0 upon success, negative otherwise.
506 */
507static int llc_ui_listen(struct socket *sock, int backlog)
508{
509 struct sock *sk = sock->sk;
510 int rc = -EINVAL;
511
512 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300513 if (unlikely(sock->state != SS_UNCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto out;
515 rc = -EOPNOTSUPP;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300516 if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto out;
518 rc = -EAGAIN;
519 if (sock_flag(sk, SOCK_ZAPPED))
520 goto out;
521 rc = 0;
Eric Dumazet95c96172012-04-15 05:58:06 +0000522 if (!(unsigned int)backlog) /* BSDism */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 backlog = 1;
524 sk->sk_max_ack_backlog = backlog;
525 if (sk->sk_state != TCP_LISTEN) {
526 sk->sk_ack_backlog = 0;
527 sk->sk_state = TCP_LISTEN;
528 }
529 sk->sk_socket->flags |= __SO_ACCEPTCON;
530out:
531 release_sock(sk);
532 return rc;
533}
534
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300535static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
WANG Congd9dc8b02016-11-11 10:20:50 -0800537 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300538 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
WANG Congd9dc8b02016-11-11 10:20:50 -0800540 add_wait_queue(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300541 while (1) {
WANG Congd9dc8b02016-11-11 10:20:50 -0800542 if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE, &wait))
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 rc = -ERESTARTSYS;
545 if (signal_pending(current))
546 break;
547 rc = -EAGAIN;
548 if (!timeout)
549 break;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300550 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800552 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return rc;
554}
555
Alan Cox5ff904d2016-09-15 18:51:25 +0100556static bool llc_ui_wait_for_conn(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
WANG Congd9dc8b02016-11-11 10:20:50 -0800558 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
WANG Congd9dc8b02016-11-11 10:20:50 -0800560 add_wait_queue(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300561 while (1) {
WANG Congd9dc8b02016-11-11 10:20:50 -0800562 if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT, &wait))
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300563 break;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300564 if (signal_pending(current) || !timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800567 remove_wait_queue(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300568 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300571static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
WANG Congd9dc8b02016-11-11 10:20:50 -0800573 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct llc_sock *llc = llc_sk(sk);
575 int rc;
576
WANG Congd9dc8b02016-11-11 10:20:50 -0800577 add_wait_queue(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300578 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 rc = 0;
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300580 if (sk_wait_event(sk, &timeout,
581 (sk->sk_shutdown & RCV_SHUTDOWN) ||
582 (!llc_data_accept_state(llc->state) &&
Jochen Friedrich451677c2005-11-14 21:57:46 -0800583 !llc->remote_busy_flag &&
WANG Congd9dc8b02016-11-11 10:20:50 -0800584 !llc->p_flag), &wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586 rc = -ERESTARTSYS;
587 if (signal_pending(current))
588 break;
589 rc = -EAGAIN;
590 if (!timeout)
591 break;
592 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800593 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return rc;
595}
596
Arnaldo Carvalho de Melo2928c192005-09-22 05:14:33 -0300597static int llc_wait_data(struct sock *sk, long timeo)
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300598{
599 int rc;
600
601 while (1) {
602 /*
603 * POSIX 1003.1g mandates this order.
604 */
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800605 rc = sock_error(sk);
606 if (rc)
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300607 break;
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300608 rc = 0;
609 if (sk->sk_shutdown & RCV_SHUTDOWN)
610 break;
611 rc = -EAGAIN;
612 if (!timeo)
613 break;
614 rc = sock_intr_errno(timeo);
615 if (signal_pending(current))
616 break;
617 rc = 0;
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +0200618 if (sk_wait_data(sk, &timeo, NULL))
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300619 break;
620 }
621 return rc;
622}
623
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +0000624static void llc_cmsg_rcv(struct msghdr *msg, struct sk_buff *skb)
625{
626 struct llc_sock *llc = llc_sk(skb->sk);
627
628 if (llc->cmsg_flags & LLC_CMSG_PKTINFO) {
629 struct llc_pktinfo info;
630
Kangjie Lub8670c02016-05-03 16:35:05 -0400631 memset(&info, 0, sizeof(info));
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +0000632 info.lpi_ifindex = llc_sk(skb->sk)->dev->ifindex;
633 llc_pdu_decode_dsap(skb, &info.lpi_sap);
634 llc_pdu_decode_da(skb, info.lpi_mac);
635 put_cmsg(msg, SOL_LLC, LLC_OPT_PKTINFO, sizeof(info), &info);
636 }
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/**
640 * llc_ui_accept - accept a new incoming connection.
641 * @sock: Socket which connections arrive on.
642 * @newsock: Socket to move incoming connection to.
643 * @flags: User specified operational flags.
David Howellscdfbabf2017-03-09 08:09:05 +0000644 * @kern: If the socket is kernel internal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 *
646 * Accept a new incoming connection.
647 * Returns 0 upon success, negative otherwise.
648 */
David Howellscdfbabf2017-03-09 08:09:05 +0000649static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags,
650 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct sock *sk = sock->sk, *newsk;
653 struct llc_sock *llc, *newllc;
654 struct sk_buff *skb;
655 int rc = -EOPNOTSUPP;
656
Harvey Harrison0dc47872008-03-05 20:47:47 -0800657 dprintk("%s: accepting on %02X\n", __func__,
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900658 llc_sk(sk)->laddr.lsap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300660 if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 goto out;
662 rc = -EINVAL;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300663 if (unlikely(sock->state != SS_UNCONNECTED ||
664 sk->sk_state != TCP_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 goto out;
666 /* wait for a connection to arrive. */
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300667 if (skb_queue_empty(&sk->sk_receive_queue)) {
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300668 rc = llc_wait_data(sk, sk->sk_rcvtimeo);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300669 if (rc)
670 goto out;
671 }
Harvey Harrison0dc47872008-03-05 20:47:47 -0800672 dprintk("%s: got a new connection on %02X\n", __func__,
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900673 llc_sk(sk)->laddr.lsap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 skb = skb_dequeue(&sk->sk_receive_queue);
675 rc = -EINVAL;
676 if (!skb->sk)
677 goto frees;
678 rc = 0;
679 newsk = skb->sk;
680 /* attach connection to a new socket. */
681 llc_ui_sk_init(newsock, newsk);
682 sock_reset_flag(newsk, SOCK_ZAPPED);
683 newsk->sk_state = TCP_ESTABLISHED;
684 newsock->state = SS_CONNECTED;
685 llc = llc_sk(sk);
686 newllc = llc_sk(newsk);
687 memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
688 newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);
689
690 /* put original socket back into a clean listen state. */
691 sk->sk_state = TCP_LISTEN;
692 sk->sk_ack_backlog--;
Harvey Harrison0dc47872008-03-05 20:47:47 -0800693 dprintk("%s: ok success on %02X, client on %02X\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
695frees:
696 kfree_skb(skb);
697out:
698 release_sock(sk);
699 return rc;
700}
701
702/**
703 * llc_ui_recvmsg - copy received data to the socket user.
704 * @sock: Socket to copy data from.
705 * @msg: Various user space related information.
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300706 * @len: Size of user buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * @flags: User specified flags.
708 *
709 * Copy received data to the socket user.
710 * Returns non-negative upon success, negative otherwise.
711 */
Ying Xue1b784142015-03-02 15:37:48 +0800712static int llc_ui_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
713 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100715 DECLARE_SOCKADDR(struct sockaddr_llc *, uaddr, msg->msg_name);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300716 const int nonblock = flags & MSG_DONTWAIT;
717 struct sk_buff *skb = NULL;
718 struct sock *sk = sock->sk;
719 struct llc_sock *llc = llc_sk(sk);
Radu Iliescu56ac11c2012-01-19 03:57:57 +0000720 unsigned long cpu_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 size_t copied = 0;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300722 u32 peek_seq = 0;
Daniel Borkmann4d231b72013-12-30 23:40:50 +0100723 u32 *seq, skb_len;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300724 unsigned long used;
725 int target; /* Read at least this many bytes */
726 long timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 lock_sock(sk);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300729 copied = -ENOTCONN;
Stephen Hemminger29efcd22006-05-25 15:08:59 -0700730 if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto out;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300732
733 timeo = sock_rcvtimeo(sk, nonblock);
734
735 seq = &llc->copied_seq;
736 if (flags & MSG_PEEK) {
737 peek_seq = llc->copied_seq;
738 seq = &peek_seq;
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900739 }
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300740
741 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
742 copied = 0;
743
744 do {
745 u32 offset;
746
747 /*
748 * We need to check signals first, to get correct SIGURG
749 * handling. FIXME: Need to check this doesn't impact 1003.1g
750 * and move it down to the bottom of the loop
751 */
752 if (signal_pending(current)) {
753 if (copied)
754 break;
755 copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
756 break;
757 }
758
759 /* Next get a buffer. */
760
761 skb = skb_peek(&sk->sk_receive_queue);
762 if (skb) {
763 offset = *seq;
764 goto found_ok_skb;
765 }
766 /* Well, if we have backlog, try to process it now yet. */
767
768 if (copied >= target && !sk->sk_backlog.tail)
769 break;
770
771 if (copied) {
772 if (sk->sk_err ||
773 sk->sk_state == TCP_CLOSE ||
774 (sk->sk_shutdown & RCV_SHUTDOWN) ||
775 !timeo ||
776 (flags & MSG_PEEK))
777 break;
778 } else {
779 if (sock_flag(sk, SOCK_DONE))
780 break;
781
782 if (sk->sk_err) {
783 copied = sock_error(sk);
784 break;
785 }
786 if (sk->sk_shutdown & RCV_SHUTDOWN)
787 break;
788
Stephen Hemminger29efcd22006-05-25 15:08:59 -0700789 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSE) {
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300790 if (!sock_flag(sk, SOCK_DONE)) {
791 /*
792 * This occurs when user tries to read
793 * from never connected socket.
794 */
795 copied = -ENOTCONN;
796 break;
797 }
798 break;
799 }
800 if (!timeo) {
801 copied = -EAGAIN;
802 break;
803 }
804 }
805
806 if (copied >= target) { /* Do not sleep, just process backlog. */
807 release_sock(sk);
808 lock_sock(sk);
809 } else
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +0200810 sk_wait_data(sk, &timeo, NULL);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300811
812 if ((flags & MSG_PEEK) && peek_seq != llc->copied_seq) {
Joe Perchese87cc472012-05-13 21:56:26 +0000813 net_dbg_ratelimited("LLC(%s:%d): Application bug, race in MSG_PEEK\n",
814 current->comm,
815 task_pid_nr(current));
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300816 peek_seq = llc->copied_seq;
817 }
818 continue;
819 found_ok_skb:
Daniel Borkmann4d231b72013-12-30 23:40:50 +0100820 skb_len = skb->len;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300821 /* Ok so how much can we use? */
822 used = skb->len - offset;
823 if (len < used)
824 used = len;
825
826 if (!(flags & MSG_TRUNC)) {
David S. Miller51f3d022014-11-05 16:46:40 -0500827 int rc = skb_copy_datagram_msg(skb, offset, msg, used);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300828 if (rc) {
829 /* Exception. Bailout! */
830 if (!copied)
831 copied = -EFAULT;
832 break;
833 }
834 }
835
836 *seq += used;
837 copied += used;
838 len -= used;
839
Alex Juncu9cef3102011-12-15 23:01:25 +0000840 /* For non stream protcols we get one packet per recvmsg call */
841 if (sk->sk_type != SOCK_STREAM)
842 goto copy_uaddr;
843
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300844 if (!(flags & MSG_PEEK)) {
Radu Iliescu56ac11c2012-01-19 03:57:57 +0000845 spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
Dan Williams7bced392013-12-30 12:37:29 -0800846 sk_eat_skb(sk, skb);
Radu Iliescu56ac11c2012-01-19 03:57:57 +0000847 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300848 *seq = 0;
849 }
Stephen Hemminger30a584d2006-08-03 16:38:49 -0700850
Stephen Hemminger30a584d2006-08-03 16:38:49 -0700851 /* Partial read */
Daniel Borkmann4d231b72013-12-30 23:40:50 +0100852 if (used + offset < skb_len)
Stephen Hemminger30a584d2006-08-03 16:38:49 -0700853 continue;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300854 } while (len > 0);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856out:
857 release_sock(sk);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300858 return copied;
859copy_uaddr:
860 if (uaddr != NULL && skb != NULL) {
861 memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
862 msg->msg_namelen = sizeof(*uaddr);
863 }
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +0000864 if (llc_sk(sk)->cmsg_flags)
865 llc_cmsg_rcv(msg, skb);
Alex Juncu9cef3102011-12-15 23:01:25 +0000866
867 if (!(flags & MSG_PEEK)) {
Dan Williams7bced392013-12-30 12:37:29 -0800868 spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
869 sk_eat_skb(sk, skb);
870 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
871 *seq = 0;
Alex Juncu9cef3102011-12-15 23:01:25 +0000872 }
873
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300874 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877/**
878 * llc_ui_sendmsg - Transmit data provided by the socket user.
879 * @sock: Socket to transmit data from.
880 * @msg: Various user related information.
881 * @len: Length of data to transmit.
882 *
883 * Transmit data provided by the socket user.
884 * Returns non-negative upon success, negative otherwise.
885 */
Ying Xue1b784142015-03-02 15:37:48 +0800886static int llc_ui_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct sock *sk = sock->sk;
889 struct llc_sock *llc = llc_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100890 DECLARE_SOCKADDR(struct sockaddr_llc *, addr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int flags = msg->msg_flags;
892 int noblock = flags & MSG_DONTWAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct sk_buff *skb;
894 size_t size = 0;
895 int rc = -EINVAL, copied = 0, hdrlen;
896
Harvey Harrison0dc47872008-03-05 20:47:47 -0800897 dprintk("%s: sending from %02X to %02X\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 llc->laddr.lsap, llc->daddr.lsap);
899 lock_sock(sk);
900 if (addr) {
901 if (msg->msg_namelen < sizeof(*addr))
902 goto release;
903 } else {
904 if (llc_ui_addr_null(&llc->addr))
905 goto release;
906 addr = &llc->addr;
907 }
908 /* must bind connection to sap if user hasn't done it. */
909 if (sock_flag(sk, SOCK_ZAPPED)) {
910 /* bind to sap with null dev, exclusive. */
911 rc = llc_ui_autobind(sock, addr);
912 if (rc)
913 goto release;
914 }
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300915 hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 size = hdrlen + len;
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300917 if (size > llc->dev->mtu)
918 size = llc->dev->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 copied = size - hdrlen;
920 release_sock(sk);
921 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
922 lock_sock(sk);
923 if (!skb)
924 goto release;
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300925 skb->dev = llc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 skb->protocol = llc_proto_type(addr->sllc_arphrd);
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900927 skb_reserve(skb, hdrlen);
Al Viro6ce8e9c2014-04-06 21:25:44 -0400928 rc = memcpy_from_msg(skb_put(skb, copied), msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (rc)
930 goto out;
931 if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
932 llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
933 addr->sllc_sap);
934 goto out;
935 }
936 if (addr->sllc_test) {
937 llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
938 addr->sllc_sap);
939 goto out;
940 }
941 if (addr->sllc_xid) {
942 llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
943 addr->sllc_sap);
944 goto out;
945 }
946 rc = -ENOPROTOOPT;
947 if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
948 goto out;
949 rc = llc_ui_send_data(sk, skb, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950out:
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300951 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 kfree_skb(skb);
953release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 dprintk("%s: failed sending from %02X to %02X: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800955 __func__, llc->laddr.lsap, llc->daddr.lsap, rc);
Arnaldo Carvalho de Melo249ff1c2005-09-22 04:32:10 -0300956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 release_sock(sk);
958 return rc ? : copied;
959}
960
961/**
962 * llc_ui_getname - return the address info of a socket
963 * @sock: Socket to get address of.
964 * @uaddr: Address structure to return information.
965 * @uaddrlen: Length of address structure.
966 * @peer: Does user want local or remote address information.
967 *
968 * Return the address information of a socket.
969 */
970static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
971 int *uaddrlen, int peer)
972{
973 struct sockaddr_llc sllc;
974 struct sock *sk = sock->sk;
975 struct llc_sock *llc = llc_sk(sk);
Mathias Krause3592aae2012-08-15 11:31:53 +0000976 int rc = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Jiri Slaby28e9fc52009-08-23 22:55:51 -0700978 memset(&sllc, 0, sizeof(sllc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 lock_sock(sk);
980 if (sock_flag(sk, SOCK_ZAPPED))
981 goto out;
982 *uaddrlen = sizeof(sllc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (peer) {
984 rc = -ENOTCONN;
985 if (sk->sk_state != TCP_ESTABLISHED)
986 goto out;
987 if(llc->dev)
988 sllc.sllc_arphrd = llc->dev->type;
989 sllc.sllc_sap = llc->daddr.lsap;
990 memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
991 } else {
992 rc = -EINVAL;
993 if (!llc->sap)
994 goto out;
995 sllc.sllc_sap = llc->sap->laddr.lsap;
996
997 if (llc->dev) {
998 sllc.sllc_arphrd = llc->dev->type;
Jiri Pirkoa186d2a2009-05-12 22:40:12 +0000999 memcpy(&sllc.sllc_mac, llc->dev->dev_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 IFHWADDRLEN);
1001 }
1002 }
1003 rc = 0;
1004 sllc.sllc_family = AF_LLC;
1005 memcpy(uaddr, &sllc, sizeof(sllc));
1006out:
1007 release_sock(sk);
1008 return rc;
1009}
1010
1011/**
1012 * llc_ui_ioctl - io controls for PF_LLC
1013 * @sock: Socket to get/set info
1014 * @cmd: command
1015 * @arg: optional argument for cmd
1016 *
1017 * get/set info on llc sockets
1018 */
1019static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
1020 unsigned long arg)
1021{
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001022 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025/**
1026 * llc_ui_setsockopt - set various connection specific parameters.
1027 * @sock: Socket to set options on.
1028 * @level: Socket level user is requesting operations on.
1029 * @optname: Operation name.
Ben Hutchings2c530402012-07-10 10:55:09 +00001030 * @optval: User provided operation data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * @optlen: Length of optval.
1032 *
1033 * Set various connection specific parameters.
1034 */
1035static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001036 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 struct sock *sk = sock->sk;
1039 struct llc_sock *llc = llc_sk(sk);
Dan Carpenter339db112010-09-10 01:56:16 +00001040 unsigned int opt;
1041 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -03001044 if (unlikely(level != SOL_LLC || optlen != sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto out;
1046 rc = get_user(opt, (int __user *)optval);
1047 if (rc)
1048 goto out;
1049 rc = -EINVAL;
1050 switch (optname) {
1051 case LLC_OPT_RETRY:
1052 if (opt > LLC_OPT_MAX_RETRY)
1053 goto out;
1054 llc->n2 = opt;
1055 break;
1056 case LLC_OPT_SIZE:
1057 if (opt > LLC_OPT_MAX_SIZE)
1058 goto out;
1059 llc->n1 = opt;
1060 break;
1061 case LLC_OPT_ACK_TMR_EXP:
1062 if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
1063 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001064 llc->ack_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 break;
1066 case LLC_OPT_P_TMR_EXP:
1067 if (opt > LLC_OPT_MAX_P_TMR_EXP)
1068 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001069 llc->pf_cycle_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 break;
1071 case LLC_OPT_REJ_TMR_EXP:
1072 if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
1073 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001074 llc->rej_sent_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076 case LLC_OPT_BUSY_TMR_EXP:
1077 if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
1078 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001079 llc->busy_state_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
1081 case LLC_OPT_TX_WIN:
1082 if (opt > LLC_OPT_MAX_WIN)
1083 goto out;
1084 llc->k = opt;
1085 break;
1086 case LLC_OPT_RX_WIN:
1087 if (opt > LLC_OPT_MAX_WIN)
1088 goto out;
1089 llc->rw = opt;
1090 break;
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +00001091 case LLC_OPT_PKTINFO:
1092 if (opt)
1093 llc->cmsg_flags |= LLC_CMSG_PKTINFO;
1094 else
1095 llc->cmsg_flags &= ~LLC_CMSG_PKTINFO;
1096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 default:
1098 rc = -ENOPROTOOPT;
1099 goto out;
1100 }
1101 rc = 0;
1102out:
1103 release_sock(sk);
1104 return rc;
1105}
1106
1107/**
1108 * llc_ui_getsockopt - get connection specific socket info
1109 * @sock: Socket to get information from.
1110 * @level: Socket level user is requesting operations on.
1111 * @optname: Operation name.
1112 * @optval: Variable to return operation data in.
1113 * @optlen: Length of optval.
1114 *
1115 * Get connection specific socket information.
1116 */
1117static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
1118 char __user *optval, int __user *optlen)
1119{
1120 struct sock *sk = sock->sk;
1121 struct llc_sock *llc = llc_sk(sk);
1122 int val = 0, len = 0, rc = -EINVAL;
1123
1124 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -03001125 if (unlikely(level != SOL_LLC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto out;
1127 rc = get_user(len, optlen);
1128 if (rc)
1129 goto out;
1130 rc = -EINVAL;
1131 if (len != sizeof(int))
1132 goto out;
1133 switch (optname) {
1134 case LLC_OPT_RETRY:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001135 val = llc->n2; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 case LLC_OPT_SIZE:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001137 val = llc->n1; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 case LLC_OPT_ACK_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001139 val = llc->ack_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 case LLC_OPT_P_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001141 val = llc->pf_cycle_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 case LLC_OPT_REJ_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001143 val = llc->rej_sent_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 case LLC_OPT_BUSY_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001145 val = llc->busy_state_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 case LLC_OPT_TX_WIN:
1147 val = llc->k; break;
1148 case LLC_OPT_RX_WIN:
1149 val = llc->rw; break;
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +00001150 case LLC_OPT_PKTINFO:
1151 val = (llc->cmsg_flags & LLC_CMSG_PKTINFO) != 0;
1152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 default:
1154 rc = -ENOPROTOOPT;
1155 goto out;
1156 }
1157 rc = 0;
1158 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1159 rc = -EFAULT;
1160out:
1161 release_sock(sk);
1162 return rc;
1163}
1164
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001165static const struct net_proto_family llc_ui_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 .family = PF_LLC,
1167 .create = llc_ui_create,
1168 .owner = THIS_MODULE,
1169};
1170
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001171static const struct proto_ops llc_ui_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 .family = PF_LLC,
1173 .owner = THIS_MODULE,
1174 .release = llc_ui_release,
1175 .bind = llc_ui_bind,
1176 .connect = llc_ui_connect,
1177 .socketpair = sock_no_socketpair,
1178 .accept = llc_ui_accept,
1179 .getname = llc_ui_getname,
1180 .poll = datagram_poll,
1181 .ioctl = llc_ui_ioctl,
1182 .listen = llc_ui_listen,
1183 .shutdown = llc_ui_shutdown,
1184 .setsockopt = llc_ui_setsockopt,
1185 .getsockopt = llc_ui_getsockopt,
1186 .sendmsg = llc_ui_sendmsg,
1187 .recvmsg = llc_ui_recvmsg,
1188 .mmap = sock_no_mmap,
1189 .sendpage = sock_no_sendpage,
1190};
1191
Stephen Hemminger01af4a02009-02-22 00:02:44 -08001192static const char llc_proc_err_msg[] __initconst =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001193 KERN_CRIT "LLC: Unable to register the proc_fs entries\n";
Stephen Hemminger01af4a02009-02-22 00:02:44 -08001194static const char llc_sysctl_err_msg[] __initconst =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001195 KERN_CRIT "LLC: Unable to register the sysctl entries\n";
Stephen Hemminger01af4a02009-02-22 00:02:44 -08001196static const char llc_sock_err_msg[] __initconst =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001197 KERN_CRIT "LLC: Unable to register the network family\n";
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199static int __init llc2_init(void)
1200{
1201 int rc = proto_register(&llc_proto, 0);
1202
1203 if (rc != 0)
1204 goto out;
1205
1206 llc_build_offset_table();
1207 llc_station_init();
1208 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
1209 rc = llc_proc_init();
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001210 if (rc != 0) {
1211 printk(llc_proc_err_msg);
Ben Hutchingsf4f87202012-08-13 02:50:43 +00001212 goto out_station;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001213 }
1214 rc = llc_sysctl_init();
1215 if (rc) {
1216 printk(llc_sysctl_err_msg);
1217 goto out_proc;
1218 }
1219 rc = sock_register(&llc_ui_family_ops);
1220 if (rc) {
1221 printk(llc_sock_err_msg);
1222 goto out_sysctl;
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
1225 llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
1226out:
1227 return rc;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001228out_sysctl:
1229 llc_sysctl_exit();
1230out_proc:
1231 llc_proc_exit();
Ben Hutchingsf4f87202012-08-13 02:50:43 +00001232out_station:
1233 llc_station_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 proto_unregister(&llc_proto);
1235 goto out;
1236}
1237
1238static void __exit llc2_exit(void)
1239{
1240 llc_station_exit();
1241 llc_remove_pack(LLC_DEST_SAP);
1242 llc_remove_pack(LLC_DEST_CONN);
1243 sock_unregister(PF_LLC);
1244 llc_proc_exit();
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001245 llc_sysctl_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 proto_unregister(&llc_proto);
1247}
1248
1249module_init(llc2_init);
1250module_exit(llc2_exit);
1251
1252MODULE_LICENSE("GPL");
1253MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
1254MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
1255MODULE_ALIAS_NETPROTO(PF_LLC);