blob: 2922d4150d88e2cf63fa75a75f0718b19a558251 [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.
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700116 *
117 * This function always consumes a reference to the skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119static int llc_ui_send_data(struct sock* sk, struct sk_buff *skb, int noblock)
120{
121 struct llc_sock* llc = llc_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900123 if (unlikely(llc_data_accept_state(llc->state) ||
Jochen Friedrich451677c2005-11-14 21:57:46 -0800124 llc->remote_busy_flag ||
125 llc->p_flag)) {
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300126 long timeout = sock_sndtimeo(sk, noblock);
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700127 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 rc = llc_ui_wait_for_busy_core(sk, timeout);
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700130 if (rc) {
131 kfree_skb(skb);
132 return rc;
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700135 return llc_build_and_send_pkt(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138static void llc_ui_sk_init(struct socket *sock, struct sock *sk)
139{
David S. Milleree5850d2008-06-17 01:21:03 -0700140 sock_graft(sk, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 sk->sk_type = sock->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 sock->ops = &llc_ui_ops;
143}
144
145static struct proto llc_proto = {
Arnaldo Carvalho de Melo9c005e02006-03-20 21:27:23 -0800146 .name = "LLC",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .owner = THIS_MODULE,
148 .obj_size = sizeof(struct llc_sock),
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800149 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152/**
153 * llc_ui_create - alloc and init a new llc_ui socket
Eric Paris3f378b62009-11-05 22:18:14 -0800154 * @net: network namespace (must be default network)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * @sock: Socket to initialize and attach allocated sk to.
156 * @protocol: Unused.
Eric Paris3f378b62009-11-05 22:18:14 -0800157 * @kern: on behalf of kernel or userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 *
159 * Allocate and initialize a new llc_ui socket, validate the user wants a
160 * socket type we have available.
161 * Returns 0 upon success, negative upon failure.
162 */
Eric Paris3f378b62009-11-05 22:18:14 -0800163static int llc_ui_create(struct net *net, struct socket *sock, int protocol,
164 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct sock *sk;
167 int rc = -ESOCKTNOSUPPORT;
168
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000169 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Patrick McHardy3480c632008-03-27 20:28:10 -0700170 return -EPERM;
171
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800172 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700173 return -EAFNOSUPPORT;
174
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300175 if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 rc = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500177 sk = llc_sk_alloc(net, PF_LLC, GFP_KERNEL, &llc_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (sk) {
179 rc = 0;
180 llc_ui_sk_init(sock, sk);
181 }
182 }
183 return rc;
184}
185
186/**
187 * llc_ui_release - shutdown socket
188 * @sock: Socket to release.
189 *
190 * Shutdown and deallocate an existing socket.
191 */
192static int llc_ui_release(struct socket *sock)
193{
194 struct sock *sk = sock->sk;
195 struct llc_sock *llc;
196
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300197 if (unlikely(sk == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto out;
199 sock_hold(sk);
200 lock_sock(sk);
201 llc = llc_sk(sk);
Harvey Harrison0dc47872008-03-05 20:47:47 -0800202 dprintk("%s: closing local(%02X) remote(%02X)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 llc->laddr.lsap, llc->daddr.lsap);
204 if (!llc_send_disc(sk))
205 llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
Cong Wang3a04ce72018-04-19 21:54:34 -0700206 if (!sock_flag(sk, SOCK_ZAPPED)) {
207 struct llc_sap *sap = llc->sap;
208
209 /* Hold this for release_sock(), so that llc_backlog_rcv()
210 * could still use it.
211 */
212 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 llc_sap_remove_socket(llc->sap, sk);
Cong Wang3a04ce72018-04-19 21:54:34 -0700214 release_sock(sk);
215 llc_sap_put(sap);
216 } else {
217 release_sock(sk);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (llc->dev)
220 dev_put(llc->dev);
221 sock_put(sk);
222 llc_sk_free(sk);
223out:
224 return 0;
225}
226
227/**
228 * llc_ui_autoport - provide dynamically allocate SAP number
229 *
230 * Provide the caller with a dynamically allocated SAP number according
231 * to the rules that are set in this function. Returns: 0, upon failure,
232 * SAP number otherwise.
233 */
234static int llc_ui_autoport(void)
235{
236 struct llc_sap *sap;
237 int i, tries = 0;
238
239 while (tries < LLC_SAP_DYN_TRIES) {
240 for (i = llc_ui_sap_last_autoport;
241 i < LLC_SAP_DYN_STOP; i += 2) {
242 sap = llc_sap_find(i);
243 if (!sap) {
244 llc_ui_sap_last_autoport = i + 2;
245 goto out;
246 }
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300247 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
250 tries++;
251 }
252 i = 0;
253out:
254 return i;
255}
256
257/**
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300258 * llc_ui_autobind - automatically bind a socket to a sap
259 * @sock: socket to bind
260 * @addr: address to connect to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
Arnaldo Carvalho de Melo5a770c02005-09-22 03:56:26 -0300262 * Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't
263 * specifically used llc_ui_bind to bind to an specific address/sap
264 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * Returns: 0 upon success, negative otherwise.
266 */
267static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
268{
269 struct sock *sk = sock->sk;
270 struct llc_sock *llc = llc_sk(sk);
271 struct llc_sap *sap;
272 int rc = -EINVAL;
273
274 if (!sock_flag(sk, SOCK_ZAPPED))
275 goto out;
276 rc = -ENODEV;
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000277 if (sk->sk_bound_dev_if) {
278 llc->dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
279 if (llc->dev && addr->sllc_arphrd != llc->dev->type) {
280 dev_put(llc->dev);
281 llc->dev = NULL;
282 }
283 } else
284 llc->dev = dev_getfirstbyhwtype(&init_net, addr->sllc_arphrd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (!llc->dev)
286 goto out;
287 rc = -EUSERS;
288 llc->laddr.lsap = llc_ui_autoport();
289 if (!llc->laddr.lsap)
290 goto out;
291 rc = -EBUSY; /* some other network layer is using the sap */
292 sap = llc_sap_open(llc->laddr.lsap, NULL);
293 if (!sap)
294 goto out;
295 memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
296 memcpy(&llc->addr, addr, sizeof(llc->addr));
297 /* assign new connection to its SAP */
298 llc_sap_add_socket(sap, sk);
299 sock_reset_flag(sk, SOCK_ZAPPED);
300 rc = 0;
301out:
302 return rc;
303}
304
305/**
306 * llc_ui_bind - bind a socket to a specific address.
307 * @sock: Socket to bind an address to.
308 * @uaddr: Address the user wants the socket bound to.
309 * @addrlen: Length of the uaddr structure.
310 *
311 * 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 -0300312 * a specific sap only or mac + sap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * If the user desires to bind to a specific mac + sap, it is possible to
314 * have multiple sap connections via multiple macs.
315 * Bind and autobind for that matter must enforce the correct sap usage
316 * otherwise all hell will break loose.
317 * Returns: 0 upon success, negative otherwise.
318 */
319static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
320{
321 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
322 struct sock *sk = sock->sk;
323 struct llc_sock *llc = llc_sk(sk);
324 struct llc_sap *sap;
325 int rc = -EINVAL;
326
linzhang0908cf42017-05-25 14:07:18 +0800327 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300328 if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto out;
330 rc = -EAFNOSUPPORT;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300331 if (unlikely(addr->sllc_family != AF_LLC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 goto out;
Tetsuo Handac68e7472019-04-12 19:55:14 +0900333 dprintk("%s: binding %02X\n", __func__, addr->sllc_sap);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300334 rc = -ENODEV;
Eric Dumazet941666c2010-12-05 01:23:53 +0000335 rcu_read_lock();
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000336 if (sk->sk_bound_dev_if) {
Eric Dumazet941666c2010-12-05 01:23:53 +0000337 llc->dev = dev_get_by_index_rcu(&init_net, sk->sk_bound_dev_if);
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000338 if (llc->dev) {
339 if (!addr->sllc_arphrd)
340 addr->sllc_arphrd = llc->dev->type;
Joe Perches951fd872013-09-01 13:11:55 -0700341 if (is_zero_ether_addr(addr->sllc_mac))
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000342 memcpy(addr->sllc_mac, llc->dev->dev_addr,
343 IFHWADDRLEN);
344 if (addr->sllc_arphrd != llc->dev->type ||
Joe Perches951fd872013-09-01 13:11:55 -0700345 !ether_addr_equal(addr->sllc_mac,
346 llc->dev->dev_addr)) {
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000347 rc = -EINVAL;
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000348 llc->dev = NULL;
349 }
350 }
351 } else
Eric Dumazet941666c2010-12-05 01:23:53 +0000352 llc->dev = dev_getbyhwaddr_rcu(&init_net, addr->sllc_arphrd,
Octavian Purdilaabf9d532009-12-26 11:51:01 +0000353 addr->sllc_mac);
Eric Dumazet941666c2010-12-05 01:23:53 +0000354 if (llc->dev)
355 dev_hold(llc->dev);
356 rcu_read_unlock();
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300357 if (!llc->dev)
358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!addr->sllc_sap) {
360 rc = -EUSERS;
361 addr->sllc_sap = llc_ui_autoport();
362 if (!addr->sllc_sap)
363 goto out;
364 }
365 sap = llc_sap_find(addr->sllc_sap);
366 if (!sap) {
367 sap = llc_sap_open(addr->sllc_sap, NULL);
368 rc = -EBUSY; /* some other network layer is using the sap */
369 if (!sap)
370 goto out;
371 } else {
372 struct llc_addr laddr, daddr;
373 struct sock *ask;
374
375 memset(&laddr, 0, sizeof(laddr));
376 memset(&daddr, 0, sizeof(daddr));
377 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200378 * FIXME: check if the address is multicast,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * only SOCK_DGRAM can do this.
380 */
381 memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
382 laddr.lsap = addr->sllc_sap;
383 rc = -EADDRINUSE; /* mac + sap clash. */
384 ask = llc_lookup_established(sap, &daddr, &laddr);
385 if (ask) {
386 sock_put(ask);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300387 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 }
390 llc->laddr.lsap = addr->sllc_sap;
391 memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
392 memcpy(&llc->addr, addr, sizeof(llc->addr));
393 /* assign new connection to its SAP */
394 llc_sap_add_socket(sap, sk);
395 sock_reset_flag(sk, SOCK_ZAPPED);
396 rc = 0;
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300397out_put:
398 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399out:
linzhang0908cf42017-05-25 14:07:18 +0800400 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return rc;
402}
403
404/**
405 * llc_ui_shutdown - shutdown a connect llc2 socket.
406 * @sock: Socket to shutdown.
407 * @how: What part of the socket to shutdown.
408 *
409 * Shutdown a connected llc2 socket. Currently this function only supports
410 * shutting down both sends and receives (2), we could probably make this
411 * function such that a user can shutdown only half the connection but not
412 * right now.
413 * Returns: 0 upon success, negative otherwise.
414 */
415static int llc_ui_shutdown(struct socket *sock, int how)
416{
417 struct sock *sk = sock->sk;
418 int rc = -ENOTCONN;
419
420 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300421 if (unlikely(sk->sk_state != TCP_ESTABLISHED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 goto out;
423 rc = -EINVAL;
424 if (how != 2)
425 goto out;
426 rc = llc_send_disc(sk);
427 if (!rc)
428 rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
429 /* Wake up anyone sleeping in poll */
430 sk->sk_state_change(sk);
431out:
432 release_sock(sk);
433 return rc;
434}
435
436/**
437 * llc_ui_connect - Connect to a remote llc2 mac + sap.
438 * @sock: Socket which will be connected to the remote destination.
439 * @uaddr: Remote and possibly the local address of the new connection.
440 * @addrlen: Size of uaddr structure.
441 * @flags: Operational flags specified by the user.
442 *
443 * Connect to a remote llc2 mac + sap. The caller must specify the
444 * destination mac and address to connect to. If the user hasn't previously
445 * called bind(2) with a smac the address of the first interface of the
446 * specified arp type will be used.
447 * This function will autobind if user did not previously call bind.
448 * Returns: 0 upon success, negative otherwise.
449 */
450static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
451 int addrlen, int flags)
452{
453 struct sock *sk = sock->sk;
454 struct llc_sock *llc = llc_sk(sk);
455 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int rc = -EINVAL;
457
458 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300459 if (unlikely(addrlen != sizeof(*addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 goto out;
461 rc = -EAFNOSUPPORT;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300462 if (unlikely(addr->sllc_family != AF_LLC))
463 goto out;
464 if (unlikely(sk->sk_type != SOCK_STREAM))
465 goto out;
466 rc = -EALREADY;
467 if (unlikely(sock->state == SS_CONNECTING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto out;
469 /* bind connection to sap if user hasn't done it. */
470 if (sock_flag(sk, SOCK_ZAPPED)) {
471 /* bind to sap with null dev, exclusive */
472 rc = llc_ui_autobind(sock, addr);
473 if (rc)
474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Jochen Friedrich5564af22005-09-22 04:46:44 -0300476 llc->daddr.lsap = addr->sllc_sap;
477 memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sock->state = SS_CONNECTING;
479 sk->sk_state = TCP_SYN_SENT;
480 llc->link = llc_ui_next_link_no(llc->sap->laddr.lsap);
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300481 rc = llc_establish_connection(sk, llc->dev->dev_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 addr->sllc_mac, addr->sllc_sap);
483 if (rc) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800484 dprintk("%s: llc_ui_send_conn failed :-(\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 sock->state = SS_UNCONNECTED;
486 sk->sk_state = TCP_CLOSE;
487 goto out;
488 }
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300489
490 if (sk->sk_state == TCP_SYN_SENT) {
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300491 const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300492
493 if (!timeo || !llc_ui_wait_for_conn(sk, timeo))
494 goto out;
495
496 rc = sock_intr_errno(timeo);
497 if (signal_pending(current))
498 goto out;
499 }
500
501 if (sk->sk_state == TCP_CLOSE)
502 goto sock_error;
503
504 sock->state = SS_CONNECTED;
505 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506out:
507 release_sock(sk);
508 return rc;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300509sock_error:
510 rc = sock_error(sk) ? : -ECONNABORTED;
511 sock->state = SS_UNCONNECTED;
512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/**
516 * llc_ui_listen - allow a normal socket to accept incoming connections
517 * @sock: Socket to allow incoming connections on.
518 * @backlog: Number of connections to queue.
519 *
520 * Allow a normal socket to accept incoming connections.
521 * Returns 0 upon success, negative otherwise.
522 */
523static int llc_ui_listen(struct socket *sock, int backlog)
524{
525 struct sock *sk = sock->sk;
526 int rc = -EINVAL;
527
528 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300529 if (unlikely(sock->state != SS_UNCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto out;
531 rc = -EOPNOTSUPP;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300532 if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto out;
534 rc = -EAGAIN;
535 if (sock_flag(sk, SOCK_ZAPPED))
536 goto out;
537 rc = 0;
Eric Dumazet95c96172012-04-15 05:58:06 +0000538 if (!(unsigned int)backlog) /* BSDism */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 backlog = 1;
540 sk->sk_max_ack_backlog = backlog;
541 if (sk->sk_state != TCP_LISTEN) {
542 sk->sk_ack_backlog = 0;
543 sk->sk_state = TCP_LISTEN;
544 }
545 sk->sk_socket->flags |= __SO_ACCEPTCON;
546out:
547 release_sock(sk);
548 return rc;
549}
550
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300551static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
WANG Congd9dc8b02016-11-11 10:20:50 -0800553 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300554 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
WANG Congd9dc8b02016-11-11 10:20:50 -0800556 add_wait_queue(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300557 while (1) {
WANG Congd9dc8b02016-11-11 10:20:50 -0800558 if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE, &wait))
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 rc = -ERESTARTSYS;
561 if (signal_pending(current))
562 break;
563 rc = -EAGAIN;
564 if (!timeout)
565 break;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300566 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800568 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return rc;
570}
571
Alan Cox5ff904d2016-09-15 18:51:25 +0100572static bool llc_ui_wait_for_conn(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
WANG Congd9dc8b02016-11-11 10:20:50 -0800574 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
WANG Congd9dc8b02016-11-11 10:20:50 -0800576 add_wait_queue(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300577 while (1) {
WANG Congd9dc8b02016-11-11 10:20:50 -0800578 if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT, &wait))
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300579 break;
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300580 if (signal_pending(current) || !timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800583 remove_wait_queue(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300584 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300587static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
WANG Congd9dc8b02016-11-11 10:20:50 -0800589 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct llc_sock *llc = llc_sk(sk);
591 int rc;
592
WANG Congd9dc8b02016-11-11 10:20:50 -0800593 add_wait_queue(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300594 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 rc = 0;
Arnaldo Carvalho de Melo54fb7f22005-09-22 04:26:14 -0300596 if (sk_wait_event(sk, &timeout,
597 (sk->sk_shutdown & RCV_SHUTDOWN) ||
598 (!llc_data_accept_state(llc->state) &&
Jochen Friedrich451677c2005-11-14 21:57:46 -0800599 !llc->remote_busy_flag &&
WANG Congd9dc8b02016-11-11 10:20:50 -0800600 !llc->p_flag), &wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 break;
602 rc = -ERESTARTSYS;
603 if (signal_pending(current))
604 break;
605 rc = -EAGAIN;
606 if (!timeout)
607 break;
608 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800609 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return rc;
611}
612
Arnaldo Carvalho de Melo2928c192005-09-22 05:14:33 -0300613static int llc_wait_data(struct sock *sk, long timeo)
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300614{
615 int rc;
616
617 while (1) {
618 /*
619 * POSIX 1003.1g mandates this order.
620 */
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800621 rc = sock_error(sk);
622 if (rc)
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300623 break;
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300624 rc = 0;
625 if (sk->sk_shutdown & RCV_SHUTDOWN)
626 break;
627 rc = -EAGAIN;
628 if (!timeo)
629 break;
630 rc = sock_intr_errno(timeo);
631 if (signal_pending(current))
632 break;
633 rc = 0;
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +0200634 if (sk_wait_data(sk, &timeo, NULL))
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300635 break;
636 }
637 return rc;
638}
639
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +0000640static void llc_cmsg_rcv(struct msghdr *msg, struct sk_buff *skb)
641{
642 struct llc_sock *llc = llc_sk(skb->sk);
643
644 if (llc->cmsg_flags & LLC_CMSG_PKTINFO) {
645 struct llc_pktinfo info;
646
Kangjie Lub8670c02016-05-03 16:35:05 -0400647 memset(&info, 0, sizeof(info));
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +0000648 info.lpi_ifindex = llc_sk(skb->sk)->dev->ifindex;
649 llc_pdu_decode_dsap(skb, &info.lpi_sap);
650 llc_pdu_decode_da(skb, info.lpi_mac);
651 put_cmsg(msg, SOL_LLC, LLC_OPT_PKTINFO, sizeof(info), &info);
652 }
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/**
656 * llc_ui_accept - accept a new incoming connection.
657 * @sock: Socket which connections arrive on.
658 * @newsock: Socket to move incoming connection to.
659 * @flags: User specified operational flags.
David Howellscdfbabf2017-03-09 08:09:05 +0000660 * @kern: If the socket is kernel internal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 *
662 * Accept a new incoming connection.
663 * Returns 0 upon success, negative otherwise.
664 */
David Howellscdfbabf2017-03-09 08:09:05 +0000665static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags,
666 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct sock *sk = sock->sk, *newsk;
669 struct llc_sock *llc, *newllc;
670 struct sk_buff *skb;
671 int rc = -EOPNOTSUPP;
672
Harvey Harrison0dc47872008-03-05 20:47:47 -0800673 dprintk("%s: accepting on %02X\n", __func__,
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900674 llc_sk(sk)->laddr.lsap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300676 if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 goto out;
678 rc = -EINVAL;
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300679 if (unlikely(sock->state != SS_UNCONNECTED ||
680 sk->sk_state != TCP_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto out;
682 /* wait for a connection to arrive. */
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300683 if (skb_queue_empty(&sk->sk_receive_queue)) {
Arnaldo Carvalho de Meloafdbe352005-09-22 04:37:07 -0300684 rc = llc_wait_data(sk, sk->sk_rcvtimeo);
Arnaldo Carvalho de Melob35bd112005-09-22 04:22:39 -0300685 if (rc)
686 goto out;
687 }
Harvey Harrison0dc47872008-03-05 20:47:47 -0800688 dprintk("%s: got a new connection on %02X\n", __func__,
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900689 llc_sk(sk)->laddr.lsap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 skb = skb_dequeue(&sk->sk_receive_queue);
691 rc = -EINVAL;
692 if (!skb->sk)
693 goto frees;
694 rc = 0;
695 newsk = skb->sk;
696 /* attach connection to a new socket. */
697 llc_ui_sk_init(newsock, newsk);
698 sock_reset_flag(newsk, SOCK_ZAPPED);
699 newsk->sk_state = TCP_ESTABLISHED;
700 newsock->state = SS_CONNECTED;
701 llc = llc_sk(sk);
702 newllc = llc_sk(newsk);
703 memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
704 newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);
705
706 /* put original socket back into a clean listen state. */
707 sk->sk_state = TCP_LISTEN;
Eric Dumazet7976a112019-11-05 14:11:52 -0800708 sk_acceptq_removed(sk);
Harvey Harrison0dc47872008-03-05 20:47:47 -0800709 dprintk("%s: ok success on %02X, client on %02X\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
711frees:
712 kfree_skb(skb);
713out:
714 release_sock(sk);
715 return rc;
716}
717
718/**
719 * llc_ui_recvmsg - copy received data to the socket user.
720 * @sock: Socket to copy data from.
721 * @msg: Various user space related information.
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300722 * @len: Size of user buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * @flags: User specified flags.
724 *
725 * Copy received data to the socket user.
726 * Returns non-negative upon success, negative otherwise.
727 */
Ying Xue1b784142015-03-02 15:37:48 +0800728static int llc_ui_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
729 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100731 DECLARE_SOCKADDR(struct sockaddr_llc *, uaddr, msg->msg_name);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300732 const int nonblock = flags & MSG_DONTWAIT;
733 struct sk_buff *skb = NULL;
734 struct sock *sk = sock->sk;
735 struct llc_sock *llc = llc_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 size_t copied = 0;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300737 u32 peek_seq = 0;
Daniel Borkmann4d231b72013-12-30 23:40:50 +0100738 u32 *seq, skb_len;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300739 unsigned long used;
740 int target; /* Read at least this many bytes */
741 long timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 lock_sock(sk);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300744 copied = -ENOTCONN;
Stephen Hemminger29efcd22006-05-25 15:08:59 -0700745 if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto out;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300747
748 timeo = sock_rcvtimeo(sk, nonblock);
749
750 seq = &llc->copied_seq;
751 if (flags & MSG_PEEK) {
752 peek_seq = llc->copied_seq;
753 seq = &peek_seq;
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900754 }
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300755
756 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
757 copied = 0;
758
759 do {
760 u32 offset;
761
762 /*
763 * We need to check signals first, to get correct SIGURG
764 * handling. FIXME: Need to check this doesn't impact 1003.1g
765 * and move it down to the bottom of the loop
766 */
767 if (signal_pending(current)) {
768 if (copied)
769 break;
770 copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
771 break;
772 }
773
774 /* Next get a buffer. */
775
776 skb = skb_peek(&sk->sk_receive_queue);
777 if (skb) {
778 offset = *seq;
779 goto found_ok_skb;
780 }
781 /* Well, if we have backlog, try to process it now yet. */
782
Eric Dumazet9ed498c2019-11-06 10:04:11 -0800783 if (copied >= target && !READ_ONCE(sk->sk_backlog.tail))
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300784 break;
785
786 if (copied) {
787 if (sk->sk_err ||
788 sk->sk_state == TCP_CLOSE ||
789 (sk->sk_shutdown & RCV_SHUTDOWN) ||
790 !timeo ||
791 (flags & MSG_PEEK))
792 break;
793 } else {
794 if (sock_flag(sk, SOCK_DONE))
795 break;
796
797 if (sk->sk_err) {
798 copied = sock_error(sk);
799 break;
800 }
801 if (sk->sk_shutdown & RCV_SHUTDOWN)
802 break;
803
Stephen Hemminger29efcd22006-05-25 15:08:59 -0700804 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSE) {
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300805 if (!sock_flag(sk, SOCK_DONE)) {
806 /*
807 * This occurs when user tries to read
808 * from never connected socket.
809 */
810 copied = -ENOTCONN;
811 break;
812 }
813 break;
814 }
815 if (!timeo) {
816 copied = -EAGAIN;
817 break;
818 }
819 }
820
821 if (copied >= target) { /* Do not sleep, just process backlog. */
822 release_sock(sk);
823 lock_sock(sk);
824 } else
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +0200825 sk_wait_data(sk, &timeo, NULL);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300826
827 if ((flags & MSG_PEEK) && peek_seq != llc->copied_seq) {
Joe Perchese87cc472012-05-13 21:56:26 +0000828 net_dbg_ratelimited("LLC(%s:%d): Application bug, race in MSG_PEEK\n",
829 current->comm,
830 task_pid_nr(current));
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300831 peek_seq = llc->copied_seq;
832 }
833 continue;
834 found_ok_skb:
Daniel Borkmann4d231b72013-12-30 23:40:50 +0100835 skb_len = skb->len;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300836 /* Ok so how much can we use? */
837 used = skb->len - offset;
838 if (len < used)
839 used = len;
840
841 if (!(flags & MSG_TRUNC)) {
David S. Miller51f3d022014-11-05 16:46:40 -0500842 int rc = skb_copy_datagram_msg(skb, offset, msg, used);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300843 if (rc) {
844 /* Exception. Bailout! */
845 if (!copied)
846 copied = -EFAULT;
847 break;
848 }
849 }
850
851 *seq += used;
852 copied += used;
853 len -= used;
854
Alex Juncu9cef3102011-12-15 23:01:25 +0000855 /* For non stream protcols we get one packet per recvmsg call */
856 if (sk->sk_type != SOCK_STREAM)
857 goto copy_uaddr;
858
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300859 if (!(flags & MSG_PEEK)) {
Eric Dumazet604d4152018-10-22 09:24:27 -0700860 skb_unlink(skb, &sk->sk_receive_queue);
861 kfree_skb(skb);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300862 *seq = 0;
863 }
Stephen Hemminger30a584d2006-08-03 16:38:49 -0700864
Stephen Hemminger30a584d2006-08-03 16:38:49 -0700865 /* Partial read */
Daniel Borkmann4d231b72013-12-30 23:40:50 +0100866 if (used + offset < skb_len)
Stephen Hemminger30a584d2006-08-03 16:38:49 -0700867 continue;
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300868 } while (len > 0);
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870out:
871 release_sock(sk);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300872 return copied;
873copy_uaddr:
874 if (uaddr != NULL && skb != NULL) {
875 memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
876 msg->msg_namelen = sizeof(*uaddr);
877 }
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +0000878 if (llc_sk(sk)->cmsg_flags)
879 llc_cmsg_rcv(msg, skb);
Alex Juncu9cef3102011-12-15 23:01:25 +0000880
881 if (!(flags & MSG_PEEK)) {
Eric Dumazet604d4152018-10-22 09:24:27 -0700882 skb_unlink(skb, &sk->sk_receive_queue);
883 kfree_skb(skb);
Dan Williams7bced392013-12-30 12:37:29 -0800884 *seq = 0;
Alex Juncu9cef3102011-12-15 23:01:25 +0000885 }
886
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300887 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890/**
891 * llc_ui_sendmsg - Transmit data provided by the socket user.
892 * @sock: Socket to transmit data from.
893 * @msg: Various user related information.
894 * @len: Length of data to transmit.
895 *
896 * Transmit data provided by the socket user.
897 * Returns non-negative upon success, negative otherwise.
898 */
Ying Xue1b784142015-03-02 15:37:48 +0800899static int llc_ui_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct sock *sk = sock->sk;
902 struct llc_sock *llc = llc_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100903 DECLARE_SOCKADDR(struct sockaddr_llc *, addr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int flags = msg->msg_flags;
905 int noblock = flags & MSG_DONTWAIT;
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700906 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 size_t size = 0;
908 int rc = -EINVAL, copied = 0, hdrlen;
909
Harvey Harrison0dc47872008-03-05 20:47:47 -0800910 dprintk("%s: sending from %02X to %02X\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 llc->laddr.lsap, llc->daddr.lsap);
912 lock_sock(sk);
913 if (addr) {
914 if (msg->msg_namelen < sizeof(*addr))
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700915 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 } else {
917 if (llc_ui_addr_null(&llc->addr))
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 addr = &llc->addr;
920 }
921 /* must bind connection to sap if user hasn't done it. */
922 if (sock_flag(sk, SOCK_ZAPPED)) {
923 /* bind to sap with null dev, exclusive. */
924 rc = llc_ui_autobind(sock, addr);
925 if (rc)
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300928 hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 size = hdrlen + len;
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300930 if (size > llc->dev->mtu)
931 size = llc->dev->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 copied = size - hdrlen;
Eric Dumazet2c5d5b12018-05-07 09:02:25 -0700933 rc = -EINVAL;
934 if (copied < 0)
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700935 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 release_sock(sk);
937 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
938 lock_sock(sk);
939 if (!skb)
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700940 goto out;
Arnaldo Carvalho de Melo774ccb42005-09-22 03:53:35 -0300941 skb->dev = llc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 skb->protocol = llc_proto_type(addr->sllc_arphrd);
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900943 skb_reserve(skb, hdrlen);
Al Viro6ce8e9c2014-04-06 21:25:44 -0400944 rc = memcpy_from_msg(skb_put(skb, copied), msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (rc)
946 goto out;
947 if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
948 llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
949 addr->sllc_sap);
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700950 skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto out;
952 }
953 if (addr->sllc_test) {
954 llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
955 addr->sllc_sap);
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700956 skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 goto out;
958 }
959 if (addr->sllc_xid) {
960 llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
961 addr->sllc_sap);
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700962 skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 goto out;
964 }
965 rc = -ENOPROTOOPT;
966 if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
967 goto out;
968 rc = llc_ui_send_data(sk, skb, noblock);
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700969 skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970out:
Eric Biggersfc8d5db2019-10-06 14:24:26 -0700971 kfree_skb(skb);
972 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dprintk("%s: failed sending from %02X to %02X: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800974 __func__, llc->laddr.lsap, llc->daddr.lsap, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 release_sock(sk);
976 return rc ? : copied;
977}
978
979/**
980 * llc_ui_getname - return the address info of a socket
981 * @sock: Socket to get address of.
982 * @uaddr: Address structure to return information.
983 * @uaddrlen: Length of address structure.
984 * @peer: Does user want local or remote address information.
985 *
986 * Return the address information of a socket.
987 */
988static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100989 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 struct sockaddr_llc sllc;
992 struct sock *sk = sock->sk;
993 struct llc_sock *llc = llc_sk(sk);
Mathias Krause3592aae2012-08-15 11:31:53 +0000994 int rc = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Jiri Slaby28e9fc52009-08-23 22:55:51 -0700996 memset(&sllc, 0, sizeof(sllc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 lock_sock(sk);
998 if (sock_flag(sk, SOCK_ZAPPED))
999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (peer) {
1001 rc = -ENOTCONN;
1002 if (sk->sk_state != TCP_ESTABLISHED)
1003 goto out;
1004 if(llc->dev)
1005 sllc.sllc_arphrd = llc->dev->type;
1006 sllc.sllc_sap = llc->daddr.lsap;
1007 memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
1008 } else {
1009 rc = -EINVAL;
1010 if (!llc->sap)
1011 goto out;
1012 sllc.sllc_sap = llc->sap->laddr.lsap;
1013
1014 if (llc->dev) {
1015 sllc.sllc_arphrd = llc->dev->type;
Jiri Pirkoa186d2a2009-05-12 22:40:12 +00001016 memcpy(&sllc.sllc_mac, llc->dev->dev_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 IFHWADDRLEN);
1018 }
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 sllc.sllc_family = AF_LLC;
1021 memcpy(uaddr, &sllc, sizeof(sllc));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001022 rc = sizeof(sllc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023out:
1024 release_sock(sk);
1025 return rc;
1026}
1027
1028/**
1029 * llc_ui_ioctl - io controls for PF_LLC
1030 * @sock: Socket to get/set info
1031 * @cmd: command
1032 * @arg: optional argument for cmd
1033 *
1034 * get/set info on llc sockets
1035 */
1036static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
1037 unsigned long arg)
1038{
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001039 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042/**
1043 * llc_ui_setsockopt - set various connection specific parameters.
1044 * @sock: Socket to set options on.
1045 * @level: Socket level user is requesting operations on.
1046 * @optname: Operation name.
Ben Hutchings2c530402012-07-10 10:55:09 +00001047 * @optval: User provided operation data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * @optlen: Length of optval.
1049 *
1050 * Set various connection specific parameters.
1051 */
1052static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001053 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct sock *sk = sock->sk;
1056 struct llc_sock *llc = llc_sk(sk);
Dan Carpenter339db112010-09-10 01:56:16 +00001057 unsigned int opt;
1058 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -03001061 if (unlikely(level != SOL_LLC || optlen != sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto out;
1063 rc = get_user(opt, (int __user *)optval);
1064 if (rc)
1065 goto out;
1066 rc = -EINVAL;
1067 switch (optname) {
1068 case LLC_OPT_RETRY:
1069 if (opt > LLC_OPT_MAX_RETRY)
1070 goto out;
1071 llc->n2 = opt;
1072 break;
1073 case LLC_OPT_SIZE:
1074 if (opt > LLC_OPT_MAX_SIZE)
1075 goto out;
1076 llc->n1 = opt;
1077 break;
1078 case LLC_OPT_ACK_TMR_EXP:
1079 if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
1080 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001081 llc->ack_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 break;
1083 case LLC_OPT_P_TMR_EXP:
1084 if (opt > LLC_OPT_MAX_P_TMR_EXP)
1085 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001086 llc->pf_cycle_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 break;
1088 case LLC_OPT_REJ_TMR_EXP:
1089 if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
1090 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001091 llc->rej_sent_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
1093 case LLC_OPT_BUSY_TMR_EXP:
1094 if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
1095 goto out;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001096 llc->busy_state_timer.expire = opt * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 break;
1098 case LLC_OPT_TX_WIN:
1099 if (opt > LLC_OPT_MAX_WIN)
1100 goto out;
1101 llc->k = opt;
1102 break;
1103 case LLC_OPT_RX_WIN:
1104 if (opt > LLC_OPT_MAX_WIN)
1105 goto out;
1106 llc->rw = opt;
1107 break;
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +00001108 case LLC_OPT_PKTINFO:
1109 if (opt)
1110 llc->cmsg_flags |= LLC_CMSG_PKTINFO;
1111 else
1112 llc->cmsg_flags &= ~LLC_CMSG_PKTINFO;
1113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 default:
1115 rc = -ENOPROTOOPT;
1116 goto out;
1117 }
1118 rc = 0;
1119out:
1120 release_sock(sk);
1121 return rc;
1122}
1123
1124/**
1125 * llc_ui_getsockopt - get connection specific socket info
1126 * @sock: Socket to get information from.
1127 * @level: Socket level user is requesting operations on.
1128 * @optname: Operation name.
1129 * @optval: Variable to return operation data in.
1130 * @optlen: Length of optval.
1131 *
1132 * Get connection specific socket information.
1133 */
1134static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
1135 char __user *optval, int __user *optlen)
1136{
1137 struct sock *sk = sock->sk;
1138 struct llc_sock *llc = llc_sk(sk);
1139 int val = 0, len = 0, rc = -EINVAL;
1140
1141 lock_sock(sk);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -03001142 if (unlikely(level != SOL_LLC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto out;
1144 rc = get_user(len, optlen);
1145 if (rc)
1146 goto out;
1147 rc = -EINVAL;
1148 if (len != sizeof(int))
1149 goto out;
1150 switch (optname) {
1151 case LLC_OPT_RETRY:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001152 val = llc->n2; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 case LLC_OPT_SIZE:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001154 val = llc->n1; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 case LLC_OPT_ACK_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001156 val = llc->ack_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 case LLC_OPT_P_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001158 val = llc->pf_cycle_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 case LLC_OPT_REJ_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001160 val = llc->rej_sent_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 case LLC_OPT_BUSY_TMR_EXP:
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001162 val = llc->busy_state_timer.expire / HZ; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 case LLC_OPT_TX_WIN:
1164 val = llc->k; break;
1165 case LLC_OPT_RX_WIN:
1166 val = llc->rw; break;
Octavian Purdilae5cd6fe2009-12-26 11:51:00 +00001167 case LLC_OPT_PKTINFO:
1168 val = (llc->cmsg_flags & LLC_CMSG_PKTINFO) != 0;
1169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 default:
1171 rc = -ENOPROTOOPT;
1172 goto out;
1173 }
1174 rc = 0;
1175 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1176 rc = -EFAULT;
1177out:
1178 release_sock(sk);
1179 return rc;
1180}
1181
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001182static const struct net_proto_family llc_ui_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 .family = PF_LLC,
1184 .create = llc_ui_create,
1185 .owner = THIS_MODULE,
1186};
1187
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001188static const struct proto_ops llc_ui_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 .family = PF_LLC,
1190 .owner = THIS_MODULE,
1191 .release = llc_ui_release,
1192 .bind = llc_ui_bind,
1193 .connect = llc_ui_connect,
1194 .socketpair = sock_no_socketpair,
1195 .accept = llc_ui_accept,
1196 .getname = llc_ui_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001197 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 .ioctl = llc_ui_ioctl,
1199 .listen = llc_ui_listen,
1200 .shutdown = llc_ui_shutdown,
1201 .setsockopt = llc_ui_setsockopt,
1202 .getsockopt = llc_ui_getsockopt,
1203 .sendmsg = llc_ui_sendmsg,
1204 .recvmsg = llc_ui_recvmsg,
1205 .mmap = sock_no_mmap,
1206 .sendpage = sock_no_sendpage,
1207};
1208
Stephen Hemminger01af4a02009-02-22 00:02:44 -08001209static const char llc_proc_err_msg[] __initconst =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001210 KERN_CRIT "LLC: Unable to register the proc_fs entries\n";
Stephen Hemminger01af4a02009-02-22 00:02:44 -08001211static const char llc_sysctl_err_msg[] __initconst =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001212 KERN_CRIT "LLC: Unable to register the sysctl entries\n";
Stephen Hemminger01af4a02009-02-22 00:02:44 -08001213static const char llc_sock_err_msg[] __initconst =
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +09001214 KERN_CRIT "LLC: Unable to register the network family\n";
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216static int __init llc2_init(void)
1217{
1218 int rc = proto_register(&llc_proto, 0);
1219
1220 if (rc != 0)
1221 goto out;
1222
1223 llc_build_offset_table();
1224 llc_station_init();
1225 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
1226 rc = llc_proc_init();
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001227 if (rc != 0) {
1228 printk(llc_proc_err_msg);
Ben Hutchingsf4f87202012-08-13 02:50:43 +00001229 goto out_station;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001230 }
1231 rc = llc_sysctl_init();
1232 if (rc) {
1233 printk(llc_sysctl_err_msg);
1234 goto out_proc;
1235 }
1236 rc = sock_register(&llc_ui_family_ops);
1237 if (rc) {
1238 printk(llc_sock_err_msg);
1239 goto out_sysctl;
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
1242 llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
1243out:
1244 return rc;
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001245out_sysctl:
1246 llc_sysctl_exit();
1247out_proc:
1248 llc_proc_exit();
Ben Hutchingsf4f87202012-08-13 02:50:43 +00001249out_station:
1250 llc_station_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 proto_unregister(&llc_proto);
1252 goto out;
1253}
1254
1255static void __exit llc2_exit(void)
1256{
1257 llc_station_exit();
1258 llc_remove_pack(LLC_DEST_SAP);
1259 llc_remove_pack(LLC_DEST_CONN);
1260 sock_unregister(PF_LLC);
1261 llc_proc_exit();
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -03001262 llc_sysctl_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 proto_unregister(&llc_proto);
1264}
1265
1266module_init(llc2_init);
1267module_exit(llc2_exit);
1268
1269MODULE_LICENSE("GPL");
1270MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
1271MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
1272MODULE_ALIAS_NETPROTO(PF_LLC);