-
Notifications
You must be signed in to change notification settings - Fork 2
/
af_atn.c
486 lines (407 loc) · 10.8 KB
/
af_atn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* ATN An implementation of the ATN TP4/CLNP protocol suite for
* the GNU/Linux operating system. ATN is implemented using the
* BSD Socket interface as the means of communication with
* the user level.
*
* PF_ATN protocol family socket handler.
*
* Version:
*
* Authors: Husni Fahmi <[email protected]>
* Tadeus Prastowo <[email protected]>
*
* Changes (oldest at the top, newest at the bottom):
* Husni Fahmi: - 2007/08/04:
* * Register IP packet handler
* * Define IP packet type in af_inet.c
* * Call dev_add_pack() for registering IP packet
* handler (dev_add_pack() is defined in
* net/core/dev.c)
* Tadeus: - 2008/03/24:
* * Replace the use of dev_add_pack() with
* register_8022_client() to handle IEEE 802.3
* frame
* - 2008/04/07:
* * Create a new type of BSD socket whose
* communication domain is PF_ATN, communication
* semantic is SOCK_RAW, and communication protocol
* is zero to enable the delivery of the payload of
* a CLNP datagram to a user-space program
* - 2008/05/27:
* * Complete the initial version of the PF_ATN
* socket without CLNP support
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
#include <asm/errno.h>
#include <asm/types.h>
#include <linux/atn.h>
#include <linux/clnp.h>
#include <linux/ctype.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/byteorder/generic.h>
#include <net/clnp.h>
#include <net/datalink.h>
#include <net/p8022.h>
#include <net/sock.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Cast of dozens");
MODULE_DESCRIPTION("The ATN TP4/CLNP Networking Suite for GNU/Linux");
MODULE_SUPPORTED_DEVICE("netdevice");
/* Private Function Prototypes */
static __always_inline struct atn_sock *atn_sk(struct sock *sk)
{
return (struct atn_sock *)sk;
}
static int atn_rcv(struct sk_buff *skb, struct net_device *dev
, struct packet_type *pt, struct net_device *orig_dev);
static struct sock *lookup_clnp_sk_list(__u8 *nsap);
static int atn_create(struct socket *sock, int protocol);
static int atn_release(struct socket *sock);
static int atn_bind(struct socket *sock, struct sockaddr *saddr, int len);
static int atn_recvmsg(struct kiocb *iocb, struct socket *sock
, struct msghdr *msg, size_t size, int flags);
static int atn_sendmsg(struct kiocb *iocb, struct socket *sock
, struct msghdr *msg, size_t len);
static struct net_device *get_dev_out(char *ha);
static void compose_clnphdr(struct sk_buff *skb, __u8 *dst_addr, __u8 *src_addr);
/* Private Global Variables */
static struct proto atn_proto = {
.name = "ATN"
, .owner = THIS_MODULE
, .obj_size = sizeof(struct atn_sock)
};
static struct net_proto_family atn_family_ops = {
.family = PF_ATN
, .create = atn_create
, .owner = THIS_MODULE
};
static struct proto_ops atn_sockraw_ops = {
.family = PF_ATN
, .owner = THIS_MODULE
, .release = atn_release
, .recvmsg = atn_recvmsg
, .sendmsg = atn_sendmsg
, .bind = atn_bind
, .connect = sock_no_connect
, .getname = sock_no_getname
, .socketpair = sock_no_socketpair
, .accept = sock_no_accept
, .poll = sock_no_poll
, .ioctl = sock_no_ioctl
, .listen = sock_no_listen
, .shutdown = sock_no_shutdown
, .setsockopt = sock_no_setsockopt
, .getsockopt = sock_no_getsockopt
, .mmap = sock_no_mmap
, .sendpage = sock_no_sendpage
};
struct datalink_proto *p8022_datalink = NULL;
static unsigned char atn_8022_type = 0xFE; /* ISO Network Layer */
static char atn_llc_err_msg[] __initdata =
KERN_CRIT "ATN: Unable to register with 802.2\n";
static struct hlist_head clnp_sk_list;
DEFINE_RWLOCK(clnp_sk_list_lock);
static int __init init_atn(void)
{
rwlock_init(&clnp_sk_list_lock);
proto_register(&atn_proto, 1);
sock_register(&atn_family_ops);
p8022_datalink = register_8022_client(atn_8022_type, atn_rcv);
if (!p8022_datalink) {
printk(atn_llc_err_msg);
return -1;
}
return 0;
}
module_init(init_atn);
static void __exit cleanup_atn(void)
{
if (p8022_datalink) {
unregister_8022_client(p8022_datalink);
p8022_datalink = NULL;
}
sock_unregister(PF_ATN);
proto_unregister(&atn_proto);
}
module_exit(cleanup_atn);
static int atn_rcv(struct sk_buff *skb, struct net_device *dev
, struct packet_type *pt, struct net_device *orig_dev)
{
int rc = 0;
struct clnphdr *clnph = NULL;
struct sock *sk = NULL;
if (skb->pkt_type == PACKET_OTHERHOST) {
goto drop;
}
if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
goto out;
}
if (!pskb_may_pull(skb, sizeof(struct clnphdr))) {
goto drop;
}
clnph = clnp_hdr(skb);
if (!pskb_may_pull(skb, ntohs(clnph->seglen))) {
goto drop;
}
sk = lookup_clnp_sk_list(clnph->dest_addr);
if (!sk) {
goto drop;
}
rc = sock_queue_rcv_skb(sk, skb);
sock_put(sk);
if (rc < 0) {
goto drop;
}
goto out;
drop:
kfree_skb(skb);
out:
return rc;
}
static struct sock *lookup_clnp_sk_list(__u8 *nsap)
{
struct hlist_node *node = NULL;
struct sock *sk = NULL;
struct sock *result = NULL;
read_lock(&clnp_sk_list_lock);
sk_for_each(sk, node, &clnp_sk_list) {
struct atn_sock *atns = atn_sk(sk);
if (cmp_nsap(nsap, atns->nsap.s_addr)) {
result = sk;
sock_hold(result);
break;
}
}
read_unlock(&clnp_sk_list_lock);
return result;
}
static int atn_create(struct socket *sock, int protocol)
{
struct sock *sk = NULL;
if (sock->type != SOCK_RAW) {
return -ESOCKTNOSUPPORT;
}
if (protocol != 0) {
return -EPROTONOSUPPORT;
}
sk = sk_alloc(PF_ATN, GFP_KERNEL, &atn_proto, 1);
if (!sk) {
return -ENOMEM;
}
sock_init_data(sock, sk);
sock->ops = &atn_sockraw_ops;
write_lock_bh(&clnp_sk_list_lock);
sk_add_node(sk, &clnp_sk_list);
write_unlock_bh(&clnp_sk_list_lock);
return 0;
}
static int atn_release(struct socket *sock)
{
struct sock *sk_in_list = NULL;
struct hlist_node *node = NULL;
struct sock *sk = sock->sk;
if (!sk) {
return 0;
}
sock->sk = NULL;
write_lock_bh(&clnp_sk_list_lock);
sk_for_each(sk_in_list, node, &clnp_sk_list) {
if (sk_in_list == sk) {
sock_put(sk_in_list);
__sk_del_node(sk_in_list);
}
}
write_unlock_bh(&clnp_sk_list_lock);
sock_orphan(sk);
skb_queue_purge(&sk->sk_receive_queue);
sock_put(sk);
return 0;
}
static int atn_bind(struct socket *sock, struct sockaddr *saddr, int saddr_len)
{
struct sock *sk_in_list = NULL;
struct sock *sk = sock->sk;
struct atn_sock *atns = atn_sk(sk);
struct sockaddr_atn *addr = (struct sockaddr_atn *)saddr;
struct hlist_node *node = NULL;
int rc = -EINVAL;
if (!sock_flag(sk, SOCK_ZAPPED) || saddr_len != sizeof(
struct sockaddr_atn)) {
goto out;
}
rc = -EADDRINUSE;
write_lock_bh(&clnp_sk_list_lock);
sk_for_each(sk_in_list, node, &clnp_sk_list) {
struct atn_sock *atns_in_list = atn_sk(sk_in_list);
if (atns == atns_in_list) {
continue;
}
if (cmp_nsap(atns_in_list->nsap.s_addr, addr->satn_addr.s_addr))
{
write_unlock_bh(&clnp_sk_list_lock);
goto out;
}
}
memcpy(atns->nsap.s_addr, addr->satn_addr.s_addr, NSAP_ADDR_LEN);
memcpy(atns->snpa, addr->satn_mac_addr, ETH_ALEN);
write_unlock_bh(&clnp_sk_list_lock);
sock_reset_flag(sk, SOCK_ZAPPED);
rc = 0;
out:
return rc;
}
static int atn_recvmsg(struct kiocb *iocb, struct socket *sock
, struct msghdr *msg, size_t size, int flags)
{
struct sock *sk = sock->sk;
struct sockaddr_atn *satn = (struct sockaddr_atn *) msg->msg_name;
struct clnphdr *clnph = NULL;
struct sk_buff *skb;
int copied;
int rc;
rc = -ENOTCONN;
if (sock_flag(sk, SOCK_ZAPPED)) {
goto out;
}
skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT
, &rc);
if (!skb) {
goto out;
}
clnph = clnp_hdr(skb);
copied = ntohs(clnph->seglen) - clnph->hdrlen;
if (copied > size) {
copied = size;
msg->msg_flags |= MSG_TRUNC;
}
rc = skb_copy_datagram_iovec(skb, clnph->hdrlen, msg->msg_iov, copied);
if (rc) {
goto out_free;
}
if (skb->tstamp.off_sec) {
skb_get_timestamp(skb, &sk->sk_stamp);
}
msg->msg_namelen = sizeof(*satn);
if (satn) {
satn->satn_family = AF_ATN;
memcpy(&satn->satn_addr.s_addr, clnph->src_addr, clnph->src_len)
;
memcpy(satn->satn_mac_addr, eth_hdr(skb)->h_source, ETH_ALEN);
}
rc = copied;
out_free:
skb_free_datagram(sk, skb);
out:
return rc;
}
static int atn_sendmsg(struct kiocb *iocb, struct socket *sock
, struct msghdr *msg, size_t len)
{
struct sock *sk = sock->sk;
struct atn_sock *atns = atn_sk(sk);
struct sockaddr_atn *usatn = (struct sockaddr_atn *) msg->msg_name;
int rc = -ENOTCONN;
int flags = msg->msg_flags;
struct iovec *iov = msg->msg_iov;
struct sk_buff *skb;
struct net_device *dev;
int total_header_len = 0;
if (sock_flag(sk, SOCK_ZAPPED)) {
goto out;
}
rc = -EINVAL;
if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT)) {
goto out;
}
rc = -ENODEV;
dev = get_dev_out(atns->snpa);
if (!dev) {
goto out;
}
total_header_len = dev->hard_header_len + p8022_datalink->header_length
+ CLNP_FIX_LEN;
rc = -EMSGSIZE;
if (len > ETH_FRAME_LEN - total_header_len) {
goto out_dev;
}
rc = -ENOMEM;
skb = sock_alloc_send_skb(sk, NET_IP_ALIGN + total_header_len + len
, flags & MSG_DONTWAIT, &rc);
if (!skb) {
goto out_dev;
}
skb_reserve(skb, NET_IP_ALIGN + total_header_len);
skb->sk = sk;
rc = memcpy_fromiovec(skb_put(skb, len), iov, len);
if (rc) {
kfree_skb(skb);
goto out_dev;
}
skb->dev = dev;
skb->protocol = htons(ETH_P_802_2);
compose_clnphdr(skb, usatn->satn_addr.s_addr, atns->nsap.s_addr);
rc = p8022_datalink->request(p8022_datalink, skb, usatn->satn_mac_addr);
if (rc >= 0) {
rc = len;
}
out:
return rc;
out_dev:
dev_put(dev);
goto out;
}
static struct net_device *get_dev_out(char *ha)
{
struct net_device *dev;
int i;
for (i = 0; i < ETH_ALEN; i++) {
if (ha[i] != 0) {
break;
}
}
if (i == ETH_ALEN) {
rtnl_lock();
dev = &loopback_dev;
dev_hold(dev);
rtnl_unlock();
return dev;
}
rtnl_lock();
dev = dev_getbyhwaddr(ARPHRD_ETHER, ha);
if (dev) {
dev_hold(dev);
}
rtnl_unlock();
return dev;
}
static void compose_clnphdr(struct sk_buff *skb, __u8 *dst_addr, __u8 *src_addr)
{
struct clnphdr *clnph;
skb->h.raw = skb_push(skb, CLNP_FIX_LEN);
clnph = clnp_hdr(skb);
clnph->nlpid = CLNP_NLPID;
clnph->hdrlen = CLNP_FIX_LEN;
clnph->vers = CLNP_VERSION;
clnph->ttl = 40;
clnph->flag = set_clnp_flag(0, 0, 1, CLNP_DT);
clnph->seglen = htons(skb->len);
clnph->dest_len = clnph->src_len = NSAP_ADDR_LEN;
memcpy(clnph->dest_addr, dst_addr, NSAP_ADDR_LEN);
memcpy(clnph->src_addr, src_addr, NSAP_ADDR_LEN);
clnp_gen_csum(clnph);
}