1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Based on linux/net/ipv6/af_inet6.c
4 * Authors:
5 * Pedro Roque <roque@di.fc.ul.pt>
6 *
7 * Fixes:
8 * piggy, Karl Knutson : Socket protocol table
9 * Hideaki YOSHIFUJI : sin6_scope_id support
10 * Arnaldo Melo : check proc_net_create return, cleanups
11 *
12 * Based on linux/net/socket.c
13 * Authors: Orest Zborowski, <obz@Kodak.COM>
14 * Ross Biro
15 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
16 *
17 * Fixes:
18 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
19 * shutdown()
20 * Alan Cox : verify_area() fixes
21 * Alan Cox : Removed DDI
22 * Jonathan Kamens : SOCK_DGRAM reconnect bug
23 * Alan Cox : Moved a load of checks to the very
24 * top level.
25 * Alan Cox : Move address structures to/from user
26 * mode above the protocol layers.
27 * Rob Janssen : Allow 0 length sends.
28 * Alan Cox : Asynchronous I/O support (cribbed from the
29 * tty drivers).
30 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
31 * Jeff Uphoff : Made max number of sockets command-line
32 * configurable.
33 * Matti Aarnio : Made the number of sockets dynamic,
34 * to be allocated when needed, and mr.
35 * Uphoff's max is used as max to be
36 * allowed to allocate.
37 * Linus : Argh. removed all the socket allocation
38 * altogether: it's in the inode now.
39 * Alan Cox : Made sock_alloc()/sock_release() public
40 * for NetROM and future kernel nfsd type
41 * stuff.
42 * Alan Cox : sendmsg/recvmsg basics.
43 * Tom Dyas : Export net symbols.
44 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
45 * Alan Cox : Added thread locking to sys_* calls
46 * for sockets. May have errors at the
47 * moment.
48 * Kevin Buhr : Fixed the dumb errors in the above.
49 * Andi Kleen : Some small cleanups, optimizations,
50 * and fixed a copy_from_user() bug.
51 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
52 * Tigran Aivazian : Made listen(2) backlog sanity checks
53 * protocol-independent
54 *
55 * NewIP INET socket protocol family
56 * Linux NewIP INET implementation
57 */
58 #define pr_fmt(fmt) KBUILD_MODNAME ": [%s:%d] " fmt, __func__, __LINE__
59
60 #include <linux/module.h>
61 #include <linux/capability.h>
62 #include <linux/errno.h>
63 #include <linux/types.h>
64 #include <linux/socket.h>
65 #include <linux/kernel.h>
66 #include <linux/timer.h>
67 #include <linux/string.h>
68 #include <linux/net.h>
69 #include <linux/interrupt.h>
70 #include <linux/proc_fs.h>
71 #include <linux/stat.h>
72 #include <linux/init.h>
73 #include <linux/sched/signal.h> /* for signal_pending() */
74 #include <linux/netdevice.h>
75 #include <linux/uaccess.h>
76 #include <linux/rtnetlink.h>
77
78 #include <net/nip.h>
79 #include <net/udp.h>
80 #include <net/tcp.h>
81 #include <net/protocol.h>
82 #include <net/inet_common.h>
83 #include <net/route.h>
84 #include <net/nndisc.h>
85 #include <linux/inet.h>
86 #include <linux/netdevice.h>
87
88 #include <net/transp_nip.h>
89 #include <net/nip_fib.h>
90 #include <net/nip_route.h>
91 #include <net/nip_addrconf.h>
92 #include <net/tcp_nip.h>
93 #include <linux/nip.h>
94 #include <linux/newip_route.h>
95
96 #include <net/netlink.h>
97 #include <net/net_namespace.h>
98 #include <linux/netlink.h>
99
100 #ifdef CONFIG_NEWIP_HOOKS
101 #include "nip_hooks_register.h"
102 #endif
103 #include "tcp_nip_parameter.h"
104
105 #define NINET_IOCTL_FLAG_LEN 8
106 #define NINET_IOCTL_HEAD_LEN 12
107 #define NINET_IOCTL_FLAG_VALUE {0xea, 0xdd, 0xea, 0xdd, 0xea, 0xdd, 0xea, 0xdd}
108
109 MODULE_DESCRIPTION("NewIP protocol stack");
110
111 /* The inetsw_nip table contains everything that ninet_create needs to
112 * build a new socket
113 */
114 static struct list_head inetsw_nip[SOCK_MAX];
115 static DEFINE_SPINLOCK(inetsw_nip_lock);
116 /* count the socket number */
117 atomic_t g_nip_socket_number = ATOMIC_INIT(0);
118
119 static int disable_nip_mod;
120 module_param_named(disable, disable_nip_mod, int, 0444);
121 MODULE_PARM_DESC(disable, "Disable NewIP module such that it is non_functional");
122
newip_mod_enabled(void)123 bool newip_mod_enabled(void)
124 {
125 return disable_nip_mod == 0;
126 }
127 EXPORT_SYMBOL_GPL(newip_mod_enabled);
128
ninet_create(struct net * net,struct socket * sock,int protocol,int kern)129 static int ninet_create(struct net *net, struct socket *sock, int protocol,
130 int kern)
131 {
132 struct inet_sock *inet;
133 struct sock *sk;
134 struct inet_protosw *answer;
135 struct proto *answer_prot;
136 unsigned char answer_flags;
137 int err;
138 int num;
139
140 if (protocol < 0 ||
141 protocol >= IPPROTO_MAX ||
142 sock->type >= SOCK_MAX)
143 return -EINVAL;
144
145 num = atomic_add_return(1, &g_nip_socket_number);
146 if (num > NIP_MAX_SOCKET_NUM) {
147 nip_dbg("The number of socket is biger than %u", NIP_MAX_SOCKET_NUM);
148 err = -EPERM;
149 goto number_sub;
150 }
151
152 sock->state = SS_UNCONNECTED;
153 /* look for the requested type/protocol pair. */
154 err = -ESOCKTNOSUPPORT;
155 rcu_read_lock();
156 list_for_each_entry_rcu(answer, &inetsw_nip[sock->type], list) {
157 err = 0;
158 /* Check the non-wild matcg */
159 if (protocol == answer->protocol) {
160 if (protocol != IPPROTO_IP)
161 break;
162 } else {
163 /* check for the two wild case. */
164 if (protocol == IPPROTO_IP) {
165 protocol = answer->protocol;
166 break;
167 }
168 if (answer->protocol == IPPROTO_IP)
169 break;
170 }
171 err = -EPROTONOSUPPORT;
172 }
173
174 if (err)
175 goto out_rcu_unlock;
176
177 sock->ops = answer->ops;
178 answer_prot = answer->prot;
179 answer_flags = answer->flags;
180 rcu_read_unlock();
181
182 WARN_ON(!answer_prot->slab);
183
184 err = -ENOBUFS;
185 sk = sk_alloc(net, PF_NINET, GFP_KERNEL, answer_prot, kern);
186 if (!sk)
187 goto number_sub;
188
189 sock_init_data(sock, sk);
190
191 err = 0;
192 if (answer_flags & INET_PROTOSW_REUSE)
193 sk->sk_reuse = SK_CAN_REUSE;
194 inet = inet_sk(sk);
195 inet->is_icsk = (answer_flags & INET_PROTOSW_ICSK) != 0;
196 inet->nodefrag = 0;
197
198 if (sock->type == SOCK_RAW) {
199 inet->inet_num = protocol;
200 if (protocol == IPPROTO_RAW)
201 inet->hdrincl = 1;
202 }
203
204 sk->sk_destruct = inet_sock_destruct;
205 sk->sk_family = PF_NINET;
206 sk->sk_protocol = protocol;
207 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
208 sk->SK_NIP_DADDR = nip_any_addr;
209 sk->SK_NIP_RCV_SADDR = nip_any_addr;
210
211 inet->uc_ttl = -1;
212 inet->mc_loop = 1;
213 inet->mc_ttl = 1;
214 inet->mc_all = 1;
215 inet->mc_index = 0;
216 inet->mc_list = NULL;
217 inet->rcv_tos = 0;
218 sk_refcnt_debug_inc(sk);
219
220 if (inet->inet_num) {
221 inet->inet_sport = htons(inet->inet_num);
222 err = sk->sk_prot->hash(sk);
223 if (err) {
224 sk_common_release(sk);
225 goto number_sub;
226 }
227 }
228 if (sk->sk_prot->init) {
229 err = sk->sk_prot->init(sk);
230 if (err) {
231 sk_common_release(sk);
232 goto number_sub;
233 }
234 }
235 out:
236 nip_dbg("The final number of socket is: %d", num);
237 return err;
238 out_rcu_unlock:
239 rcu_read_unlock();
240 number_sub:
241 atomic_dec_if_positive(&g_nip_socket_number);
242 num = atomic_read(&g_nip_socket_number);
243 nip_dbg("[error] The final number of socket is: %d (after dec)", num);
244 goto out;
245 }
246
ninet_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)247 int ninet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
248 {
249 struct sockaddr_nin *addr = (struct sockaddr_nin *)uaddr;
250 struct sock *sk = sock->sk;
251 struct inet_sock *inet = inet_sk(sk);
252 struct net *net = sock_net(sk);
253 u_short snum;
254 int err = 0;
255
256 /* If the socket has its own bind function then use it */
257 if (sk->sk_prot->bind)
258 return sk->sk_prot->bind(sk, uaddr, addr_len);
259
260 if (addr_len < sizeof(struct sockaddr_nin))
261 return -EINVAL;
262
263 snum = ntohs(addr->sin_port);
264 if (snum && snum < PROT_SOCK)
265 return -EACCES;
266
267 if (nip_bind_addr_check(net, &addr->sin_addr) == false) {
268 nip_dbg("binding-addr invalid, bitlen=%u", addr->sin_addr.bitlen);
269 return -EADDRNOTAVAIL;
270 }
271 lock_sock(sk);
272
273 /* check these errors (active socket, double bind) */
274 if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
275 err = -EINVAL;
276 goto out;
277 }
278
279 sk->SK_NIP_RCV_SADDR = addr->sin_addr;
280
281 /* make sure we are allowed to bind here */
282 if ((snum || !inet->bind_address_no_port) &&
283 sk->sk_prot->get_port(sk, snum)) {
284 inet->inet_saddr = 0;
285 err = -EADDRINUSE;
286 goto out;
287 }
288 inet->inet_sport = htons(inet->inet_num);
289 inet->inet_daddr = 0;
290 inet->inet_dport = 0;
291 sk_dst_reset(sk);
292
293 out:
294 release_sock(sk);
295 return err;
296 }
297
298 /* Function
299 * Move a socket into listening state.
300 * Parameter
301 * sock: The socket
302 * backlog: Specifies the number of clients that use a three-way handshake
303 * to establish a TCP connection
304 */
ninet_listen(struct socket * sock,int backlog)305 int ninet_listen(struct socket *sock, int backlog)
306 {
307 struct sock *sk = sock->sk;
308 unsigned char old_state;
309 int err;
310
311 lock_sock(sk);
312
313 err = -EINVAL;
314 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
315 goto out;
316
317 old_state = sk->sk_state;
318 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
319 goto out;
320
321 WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
322 /* Really, if the socket is already in listen state
323 * we can only allow the backlog to be adjusted.
324 */
325 if (old_state != TCP_LISTEN) {
326 err = inet_csk_listen_start(sk, backlog);
327 if (err)
328 goto out;
329 }
330 err = 0;
331
332 out:
333 release_sock(sk);
334 return err;
335 }
336
ninet_release(struct socket * sock)337 int ninet_release(struct socket *sock)
338 {
339 struct sock *sk = sock->sk;
340 int err;
341 int num;
342
343 if (!sk)
344 return -EINVAL;
345
346 atomic_dec_if_positive(&g_nip_socket_number);
347 err = inet_release(sock);
348 num = atomic_read(&g_nip_socket_number);
349 nip_dbg("%s, The final number of socket is: %d",
350 err ? "failed" : "success", num);
351 return err;
352 }
353
ninet_destroy_sock(struct sock * sk)354 void ninet_destroy_sock(struct sock *sk)
355 {
356 ;
357 }
358
ninet_getname(struct socket * sock,struct sockaddr * uaddr,int peer)359 int ninet_getname(struct socket *sock, struct sockaddr *uaddr,
360 int peer)
361 {
362 struct sock *sk = sock->sk;
363 struct inet_sock *inet = inet_sk(sk);
364 DECLARE_SOCKADDR(struct sockaddr_nin *, sin, uaddr);
365
366 sin->sin_family = AF_NINET;
367 if (peer) {
368 if (!inet->inet_dport)
369 return -ENOTCONN;
370 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
371 peer == 1)
372 return -ENOTCONN;
373 sin->sin_port = inet->inet_dport;
374 sin->sin_addr = sk->SK_NIP_DADDR;
375 } else {
376 sin->sin_port = inet->inet_sport;
377 sin->sin_addr = sk->SK_NIP_RCV_SADDR;
378 }
379 return sizeof(*sin);
380 }
381
ninet_wait_for_connect(struct sock * sk,long timeo,int writebias)382 static long ninet_wait_for_connect(struct sock *sk, long timeo, int writebias)
383 {
384 DEFINE_WAIT_FUNC(wait, woken_wake_function);
385
386 add_wait_queue(sk_sleep(sk), &wait);
387 sk->sk_write_pending += writebias;
388
389 /* Basic assumption: if someone sets sk->sk_err, he _must_
390 * change state of the socket from TCP_SYN_*.
391 * Connect() does not allow to get error notifications
392 * without closing the socket.
393 */
394 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
395 release_sock(sk);
396 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
397 lock_sock(sk);
398 if (signal_pending(current) || !timeo)
399 break;
400 }
401 remove_wait_queue(sk_sleep(sk), &wait);
402 sk->sk_write_pending -= writebias;
403 return timeo;
404 }
405
406 /* Function
407 * The client socket layer is used to establish connection requests
408 * Parameter
409 * sock: The socket
410 * uaddr:The destination address
411 */
__ninet_stream_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)412 int __ninet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
413 int addr_len, int flags)
414 {
415 struct sock *sk = sock->sk;
416 int err;
417 long timeo;
418
419 if (uaddr) {
420 if (addr_len < sizeof(uaddr->sa_family))
421 return -EINVAL;
422 }
423
424 switch (sock->state) {
425 default:
426 err = -EINVAL;
427 goto out;
428 case SS_CONNECTED:
429 err = -EISCONN;
430 goto out;
431 case SS_CONNECTING:
432 err = -EALREADY;
433 break;
434 case SS_UNCONNECTED:
435 err = -EISCONN;
436 if (sk->sk_state != TCP_CLOSE)
437 goto out;
438 /* Call the tcp_nip_connect function */
439 err = sk->sk_prot->connect(sk, uaddr, addr_len);
440 if (err < 0)
441 goto out;
442 /* Switch to connecting, and then perform subsequent operations */
443 sock->state = SS_CONNECTING;
444 err = -EINPROGRESS;
445 break;
446 }
447
448 /* Get blocking time */
449 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
450 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
451 int writebias = 0;
452 /* Error code is set above */
453 if (!timeo || !ninet_wait_for_connect(sk, timeo, writebias))
454 goto out;
455
456 err = sock_intr_errno(timeo);
457 if (signal_pending(current))
458 goto out;
459 }
460
461 if (sk->sk_state == TCP_CLOSE)
462 goto sock_error;
463 sock->state = SS_CONNECTED;
464 err = 0;
465
466 out:
467 return err;
468 sock_error:
469 err = sock_error(sk) ? : -ECONNABORTED;
470 sock->state = SS_DISCONNECTING;
471 goto out;
472 }
473
ninet_stream_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)474 int ninet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
475 int addr_len, int flags)
476 {
477 int err;
478
479 lock_sock(sock->sk);
480 err = __ninet_stream_connect(sock, uaddr, addr_len, flags);
481 release_sock(sock->sk);
482 return err;
483 }
484
ninet_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)485 int ninet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
486 {
487 struct sock *sk = sock->sk;
488 struct net *net = sock_net(sk);
489
490 nip_dbg("cmd=0x%x", cmd);
491 switch (cmd) {
492 case SIOCADDRT:
493 case SIOCDELRT: {
494 struct nip_rtmsg rtmsg;
495
496 if (copy_from_user(&rtmsg, (void __user *)arg, sizeof(rtmsg))) {
497 nip_dbg("fail to copy route cfg data");
498 return -EFAULT;
499 }
500 return nip_route_ioctl(net, cmd, &rtmsg);
501 }
502 case SIOCSIFADDR:
503 return nip_addrconf_add_ifaddr(net, (void __user *)arg);
504 case SIOCDIFADDR:
505 return nip_addrconf_del_ifaddr(net, (void __user *)arg);
506 case SIOCGIFADDR:
507 return nip_addrconf_get_ifaddr(net, cmd, (void __user *)arg);
508 case SIOCGIFCONF: {
509 struct ifconf ifc;
510 int err;
511
512 if (copy_from_user(&ifc, (void __user *)arg, sizeof(struct ifconf)))
513 return -EFAULT;
514 rtnl_lock();
515 err = nip_dev_ifconf(net, &ifc, sizeof(struct ifreq));
516 rtnl_unlock();
517 if (!err && copy_to_user((void __user *)arg, &ifc, sizeof(struct ifconf)))
518 err = -EFAULT;
519 return err;
520 }
521 default:
522 if (!sk->sk_prot->ioctl) {
523 nip_dbg("sock sk_prot ioctl is null, cmd=0x%x", cmd);
524 return -ENOIOCTLCMD;
525 }
526 return sk->sk_prot->ioctl(sk, cmd, arg);
527 }
528 }
529
530 #ifdef CONFIG_COMPAT
531 struct compat_nip_rtmsg {
532 struct nip_addr rtmsg_dst;
533 struct nip_addr rtmsg_src;
534 struct nip_addr rtmsg_gateway;
535 char dev_name[10];
536 unsigned int rtmsg_type;
537 int rtmsg_ifindex;
538 unsigned int rtmsg_metric;
539 unsigned int rtmsg_info; /* long convert to int */
540 unsigned int rtmsg_flags;
541 };
542
ninet_compat_routing_ioctl(struct sock * sk,unsigned int cmd,struct compat_nip_rtmsg __user * ur)543 static int ninet_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
544 struct compat_nip_rtmsg __user *ur)
545 {
546 struct nip_rtmsg rt;
547
548 if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst, INDEX_3 * sizeof(struct nip_addr)) ||
549 copy_from_user(&rt.dev_name, &ur->dev_name, sizeof(rt.dev_name)) ||
550 get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
551 get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex) ||
552 get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
553 get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
554 get_user(rt.rtmsg_flags, &ur->rtmsg_flags)) {
555 nip_dbg("fail to convert input para, cmd=0x%x", cmd);
556 return -EFAULT;
557 }
558
559 nip_dbg("cmd=0x%x", cmd);
560 return nip_route_ioctl(sock_net(sk), cmd, &rt);
561 }
562
compat_nip_dev_ifconf(struct net * net,struct compat_ifconf __user * uifc32)563 static int compat_nip_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
564 {
565 struct compat_ifconf ifc32;
566 struct ifconf ifc;
567 int err;
568
569 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
570 return -EFAULT;
571
572 ifc.ifc_len = ifc32.ifc_len;
573 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
574
575 rtnl_lock();
576 err = nip_dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
577 rtnl_unlock();
578 if (err)
579 return err;
580
581 ifc32.ifc_len = ifc.ifc_len;
582 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
583 return -EFAULT;
584
585 return 0;
586 }
587
ninet_compat_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)588 int ninet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
589 {
590 void __user *argp = compat_ptr(arg);
591 struct sock *sk = sock->sk;
592 struct net *net = sock_net(sk);
593
594 switch (cmd) {
595 case SIOCADDRT:
596 case SIOCDELRT:
597 return ninet_compat_routing_ioctl(sk, cmd, argp);
598 case SIOCGIFCONF:
599 return compat_nip_dev_ifconf(net, argp);
600 default:
601 return -ENOIOCTLCMD;
602 }
603 }
604 EXPORT_SYMBOL_GPL(ninet_compat_ioctl);
605
compat_select_ninet_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg,int arglen)606 static int compat_select_ninet_ioctl(struct socket *sock, unsigned int cmd,
607 unsigned long arg, int arglen)
608 {
609 switch (cmd) {
610 case SIOCADDRT:
611 case SIOCDELRT:
612 if (sizeof(struct nip_rtmsg) != arglen) {
613 void __user *argp = compat_ptr(arg);
614 struct sock *sk = sock->sk;
615
616 return ninet_compat_routing_ioctl(sk, cmd, argp);
617 }
618 return ninet_ioctl(sock, cmd, arg);
619 default:
620 return ninet_ioctl(sock, cmd, arg);
621 }
622 }
623 #endif /* CONFIG_COMPAT */
624
__ninet_ioctl_cmd(struct socket * sock,unsigned int cmd,void __user * iov_base,__kernel_size_t iov_len)625 static int __ninet_ioctl_cmd(struct socket *sock, unsigned int cmd,
626 void __user *iov_base, __kernel_size_t iov_len)
627 {
628 unsigned long arg = (unsigned long)((char *)iov_base + NINET_IOCTL_HEAD_LEN);
629 #ifdef CONFIG_COMPAT
630 int arglen = iov_len - NINET_IOCTL_HEAD_LEN;
631
632 return compat_select_ninet_ioctl(sock, cmd, arg, arglen);
633 #else
634 return ninet_ioctl(sock, cmd, arg);
635 #endif
636 }
637
ninet_ioctl_cmd(struct socket * sock,const struct iovec * iov)638 int ninet_ioctl_cmd(struct socket *sock, const struct iovec *iov)
639 {
640 const char ioctl_flag[NINET_IOCTL_FLAG_LEN] = NINET_IOCTL_FLAG_VALUE;
641 char ioctl_head[NINET_IOCTL_HEAD_LEN];
642 int i;
643 unsigned int cmd;
644
645 if (!iov || !iov->iov_base || !sock ||
646 iov->iov_len < NINET_IOCTL_HEAD_LEN) {
647 nip_dbg("invalid parameter");
648 return -NIP_IOCTL_FLAG_INVALID;
649 }
650
651 if (copy_from_user(ioctl_head, (void __user *)iov->iov_base, NINET_IOCTL_HEAD_LEN)) {
652 nip_dbg("fail to copy ioctl head");
653 return -NIP_IOCTL_FLAG_INVALID;
654 }
655
656 for (i = 0; i < NINET_IOCTL_FLAG_LEN; i++) {
657 if (ioctl_head[i] != ioctl_flag[i]) {
658 nip_dbg("not ninet ioctl cmd");
659 return -NIP_IOCTL_FLAG_INVALID;
660 }
661 }
662 cmd = *(unsigned int *)(ioctl_head + NINET_IOCTL_FLAG_LEN);
663 return __ninet_ioctl_cmd(sock, cmd, iov->iov_base, iov->iov_len);
664 }
665
666 /* register new IP socket */
667 const struct proto_ops ninet_dgram_ops = {
668 .family = PF_NINET,
669 .owner = THIS_MODULE,
670 .release = ninet_release,
671 .bind = ninet_bind,
672 .connect = inet_dgram_connect,
673 .socketpair = sock_no_socketpair,
674 .accept = sock_no_accept,
675 .getname = ninet_getname,
676 .poll = datagram_poll,
677 .ioctl = ninet_ioctl,
678 .gettstamp = sock_gettstamp,
679 .listen = sock_no_listen,
680 .shutdown = inet_shutdown,
681 .setsockopt = sock_common_setsockopt,
682 .getsockopt = sock_common_getsockopt,
683 .sendmsg = inet_sendmsg,
684 .recvmsg = inet_recvmsg,
685 .mmap = sock_no_mmap,
686 .sendpage = sock_no_sendpage,
687 .set_peek_off = sk_set_peek_off,
688 #ifdef CONFIG_COMPAT
689 .compat_ioctl = ninet_compat_ioctl,
690 #endif
691 };
692
693 const struct proto_ops ninet_stream_ops = {
694 .family = PF_NINET,
695 .owner = THIS_MODULE,
696 .release = ninet_release,
697 .bind = ninet_bind,
698 .connect = ninet_stream_connect,
699 .socketpair = sock_no_socketpair,
700 .accept = inet_accept,
701 .getname = ninet_getname,
702 .poll = tcp_poll,
703 .ioctl = ninet_ioctl,
704 .listen = ninet_listen,
705 .shutdown = inet_shutdown,
706 .setsockopt = sock_common_setsockopt,
707 .getsockopt = sock_common_getsockopt,
708 .sendmsg = inet_sendmsg,
709 .recvmsg = inet_recvmsg,
710 .mmap = sock_no_mmap,
711 .sendpage = inet_sendpage,
712 #ifdef CONFIG_COMPAT
713 .compat_ioctl = ninet_compat_ioctl,
714 #endif
715 };
716
717 static const struct net_proto_family ninet_family_ops = {
718 .family = PF_NINET,
719 .create = ninet_create,
720 .owner = THIS_MODULE,
721 };
722
ninet_register_protosw(struct inet_protosw * p)723 int ninet_register_protosw(struct inet_protosw *p)
724 {
725 struct list_head *lh;
726 struct inet_protosw *answer;
727 struct list_head *last_perm;
728 int protocol = p->protocol;
729 int ret;
730
731 spin_lock_bh(&inetsw_nip_lock);
732
733 ret = -EINVAL;
734 if (p->type >= SOCK_MAX)
735 goto out_illegal;
736
737 /* If we are trying to override a permanent protocol, bail. */
738 answer = NULL;
739 ret = -EPERM;
740 last_perm = &inetsw_nip[p->type];
741 list_for_each(lh, &inetsw_nip[p->type]) {
742 answer = list_entry(lh, struct inet_protosw, list);
743
744 /* Check only the non-wild match. */
745 if (answer->flags & INET_PROTOSW_PERMANENT) {
746 if (protocol == answer->protocol)
747 break;
748 last_perm = lh;
749 }
750
751 answer = NULL;
752 }
753 if (answer)
754 goto out_permanent;
755
756 list_add_rcu(&p->list, last_perm);
757 ret = 0;
758 out:
759 spin_unlock_bh(&inetsw_nip_lock);
760 return ret;
761
762 out_permanent:
763 nip_dbg("Attempt to override permanent protocol %d", protocol);
764 goto out;
765
766 out_illegal:
767 nip_dbg("Ignoring attempt to register invalid socket type %d", p->type);
768 goto out;
769 }
770
ninet_unregister_protosw(struct inet_protosw * p)771 void ninet_unregister_protosw(struct inet_protosw *p)
772 {
773 if (INET_PROTOSW_PERMANENT & p->flags) {
774 nip_dbg("Attempt to unregister permanent protocol %d", p->protocol);
775 } else {
776 spin_lock_bh(&inetsw_nip_lock);
777 list_del_rcu(&p->list);
778 spin_unlock_bh(&inetsw_nip_lock);
779
780 synchronize_net();
781 }
782 }
783
ninet_sk_rebuild_header(struct sock * sk)784 int ninet_sk_rebuild_header(struct sock *sk)
785 {
786 return 0;
787 }
788
789 /* register to data link layer */
790 static struct packet_type nip_packet_type __read_mostly = {
791 .type = cpu_to_be16(ETH_P_NEWIP),
792 .func = nip_rcv,
793 };
794
nip_packet_init(void)795 static int __init nip_packet_init(void)
796 {
797 dev_add_pack(&nip_packet_type);
798 return 0;
799 }
800
ninet_net_init(struct net * net)801 static int __net_init ninet_net_init(struct net *net)
802 {
803 int err = 0;
804 return err;
805 }
806
ninet_net_exit(struct net * net)807 static void __net_exit ninet_net_exit(struct net *net)
808 {
809 ;
810 }
811
812 static struct pernet_operations ninet_net_ops = {
813 .init = ninet_net_init,
814 .exit = ninet_net_exit,
815 };
816
ninet_init(void)817 static int __init ninet_init(void)
818 {
819 struct list_head *r;
820 int err = 0;
821
822 sock_skb_cb_check_size(sizeof(struct ninet_skb_parm));
823
824 nip_dbg("NET: start to init nip network");
825 /* register the socket-side information for ninet_create */
826 for (r = &inetsw_nip[0]; r < &inetsw_nip[SOCK_MAX]; ++r)
827 INIT_LIST_HEAD(r);
828
829 if (!newip_mod_enabled()) {
830 nip_dbg("Loaded, but administratively disabled, reboot required to enable");
831 goto out;
832 }
833
834 err = proto_register(&tcp_nip_prot, 1);
835 if (err)
836 goto out;
837
838 err = proto_register(&nip_udp_prot, 1);
839 if (err) {
840 nip_dbg("failed to register udp proto");
841 goto out_udp_register_fail;
842 }
843
844 err = sock_register(&ninet_family_ops);
845 if (err) {
846 nip_dbg("failed to register newip_family_ops");
847 goto out_sock_register_fail;
848 }
849
850 err = register_pernet_subsys(&ninet_net_ops);
851 if (err) {
852 nip_dbg("failed to register ninet_net_ops");
853 goto register_pernet_fail;
854 }
855
856 err = nip_icmp_init();
857 if (err) {
858 nip_dbg("nip_icmp_init failed");
859 goto nip_icmp_fail;
860 }
861
862 err = nndisc_init();
863 if (err) {
864 nip_dbg("nndisc_init failed");
865 goto nndisc_fail;
866 }
867
868 err = nip_route_init();
869 if (err)
870 goto nip_route_fail;
871
872 err = nip_addrconf_init();
873 if (err)
874 goto nip_addr_fail;
875
876 err = nip_udp_init();
877 if (err) {
878 nip_dbg("failed to init udp layer");
879 goto udp_fail;
880 }
881
882 err = tcp_nip_init();
883 if (err) {
884 nip_dbg("failed to init tcp layer");
885 goto tcp_fail;
886 } else {
887 nip_dbg("nip_tcp_init ok");
888 }
889
890 err = nip_packet_init();
891 if (err) {
892 nip_dbg("failed to register to l2 layer");
893 goto nip_packet_fail;
894 }
895
896 #ifdef CONFIG_NEWIP_HOOKS
897 ninet_hooks_init();
898 #endif
899 nip_dbg("init newip address family ok");
900
901 out:
902 return err;
903
904 nip_packet_fail:
905 tcp_nip_exit();
906 tcp_fail:
907 nip_udp_exit();
908 udp_fail:
909 nip_addrconf_cleanup();
910 nip_addr_fail:
911 nip_route_cleanup();
912 nip_route_fail:
913 nndisc_fail:
914 nip_icmp_fail:
915 unregister_pernet_subsys(&ninet_net_ops);
916 register_pernet_fail:
917 sock_unregister(PF_NINET);
918 out_sock_register_fail:
919 proto_unregister(&nip_udp_prot);
920 out_udp_register_fail:
921 nip_dbg("newip family init failed");
922 goto out;
923 }
924
925 module_init(ninet_init);
926
927 MODULE_ALIAS_NETPROTO(PF_NINET);
928
929