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 if (INET_PROTOSW_ICSK & answer_flags)
195 inet_init_csk_locks(sk);
196 inet = inet_sk(sk);
197 inet->is_icsk = (answer_flags & INET_PROTOSW_ICSK) != 0;
198 inet->nodefrag = 0;
199
200 if (sock->type == SOCK_RAW) {
201 inet->inet_num = protocol;
202 if (protocol == IPPROTO_RAW)
203 inet->hdrincl = 1;
204 }
205
206 sk->sk_destruct = inet_sock_destruct;
207 sk->sk_family = PF_NINET;
208 sk->sk_protocol = protocol;
209 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
210 sk->SK_NIP_DADDR = nip_any_addr;
211 sk->SK_NIP_RCV_SADDR = nip_any_addr;
212
213 inet->uc_ttl = -1;
214 inet->mc_loop = 1;
215 inet->mc_ttl = 1;
216 inet->mc_all = 1;
217 inet->mc_index = 0;
218 inet->mc_list = NULL;
219 inet->rcv_tos = 0;
220 sk_refcnt_debug_inc(sk);
221
222 if (inet->inet_num) {
223 inet->inet_sport = htons(inet->inet_num);
224 err = sk->sk_prot->hash(sk);
225 if (err) {
226 sk_common_release(sk);
227 goto number_sub;
228 }
229 }
230 if (sk->sk_prot->init) {
231 err = sk->sk_prot->init(sk);
232 if (err) {
233 sk_common_release(sk);
234 goto number_sub;
235 }
236 }
237 out:
238 nip_dbg("The final number of socket is: %d", num);
239 return err;
240 out_rcu_unlock:
241 rcu_read_unlock();
242 number_sub:
243 atomic_dec_if_positive(&g_nip_socket_number);
244 num = atomic_read(&g_nip_socket_number);
245 nip_dbg("[error] The final number of socket is: %d (after dec)", num);
246 goto out;
247 }
248
ninet_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)249 int ninet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
250 {
251 struct sockaddr_nin *addr = (struct sockaddr_nin *)uaddr;
252 struct sock *sk = sock->sk;
253 struct inet_sock *inet = inet_sk(sk);
254 struct net *net = sock_net(sk);
255 u_short snum;
256 const struct proto *prot;
257 int err = 0;
258
259 /* If the socket has its own bind function then use it */
260 prot = READ_ONCE(sk->sk_prot);
261 if (prot->bind)
262 return prot->bind(sk, uaddr, addr_len);
263
264 if (addr_len < sizeof(struct sockaddr_nin))
265 return -EINVAL;
266
267 snum = ntohs(addr->sin_port);
268 if (snum && snum < PROT_SOCK)
269 return -EACCES;
270
271 if (nip_bind_addr_check(net, &addr->sin_addr) == false) {
272 nip_dbg("binding-addr invalid, bitlen=%u", addr->sin_addr.bitlen);
273 return -EADDRNOTAVAIL;
274 }
275 lock_sock(sk);
276
277 /* check these errors (active socket, double bind) */
278 if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
279 err = -EINVAL;
280 goto out;
281 }
282
283 sk->SK_NIP_RCV_SADDR = addr->sin_addr;
284
285 /* make sure we are allowed to bind here */
286 if ((snum || !inet->bind_address_no_port) &&
287 sk->sk_prot->get_port(sk, snum)) {
288 inet->inet_saddr = 0;
289 err = -EADDRINUSE;
290 goto out;
291 }
292 inet->inet_sport = htons(inet->inet_num);
293 inet->inet_daddr = 0;
294 inet->inet_dport = 0;
295 sk_dst_reset(sk);
296
297 out:
298 release_sock(sk);
299 return err;
300 }
301
302 /* Function
303 * Move a socket into listening state.
304 * Parameter
305 * sock: The socket
306 * backlog: Specifies the number of clients that use a three-way handshake
307 * to establish a TCP connection
308 */
ninet_listen(struct socket * sock,int backlog)309 int ninet_listen(struct socket *sock, int backlog)
310 {
311 struct sock *sk = sock->sk;
312 unsigned char old_state;
313 int err;
314
315 lock_sock(sk);
316
317 err = -EINVAL;
318 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
319 goto out;
320
321 old_state = sk->sk_state;
322 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
323 goto out;
324
325 WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
326 /* Really, if the socket is already in listen state
327 * we can only allow the backlog to be adjusted.
328 */
329 if (old_state != TCP_LISTEN) {
330 err = inet_csk_listen_start(sk, backlog);
331 if (err)
332 goto out;
333 }
334 err = 0;
335
336 out:
337 release_sock(sk);
338 return err;
339 }
340
ninet_release(struct socket * sock)341 int ninet_release(struct socket *sock)
342 {
343 struct sock *sk = sock->sk;
344 int err;
345 int num;
346
347 if (!sk)
348 return -EINVAL;
349
350 atomic_dec_if_positive(&g_nip_socket_number);
351 err = inet_release(sock);
352 num = atomic_read(&g_nip_socket_number);
353 nip_dbg("%s, The final number of socket is: %d",
354 err ? "failed" : "success", num);
355 return err;
356 }
357
ninet_destroy_sock(struct sock * sk)358 void ninet_destroy_sock(struct sock *sk)
359 {
360 ;
361 }
362
ninet_getname(struct socket * sock,struct sockaddr * uaddr,int peer)363 int ninet_getname(struct socket *sock, struct sockaddr *uaddr,
364 int peer)
365 {
366 struct sock *sk = sock->sk;
367 struct inet_sock *inet = inet_sk(sk);
368 DECLARE_SOCKADDR(struct sockaddr_nin *, sin, uaddr);
369
370 sin->sin_family = AF_NINET;
371 if (peer) {
372 if (!inet->inet_dport)
373 return -ENOTCONN;
374 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
375 peer == 1)
376 return -ENOTCONN;
377 sin->sin_port = inet->inet_dport;
378 sin->sin_addr = sk->SK_NIP_DADDR;
379 } else {
380 sin->sin_port = inet->inet_sport;
381 sin->sin_addr = sk->SK_NIP_RCV_SADDR;
382 }
383 return sizeof(*sin);
384 }
385
ninet_wait_for_connect(struct sock * sk,long timeo,int writebias)386 static long ninet_wait_for_connect(struct sock *sk, long timeo, int writebias)
387 {
388 DEFINE_WAIT_FUNC(wait, woken_wake_function);
389
390 add_wait_queue(sk_sleep(sk), &wait);
391 sk->sk_write_pending += writebias;
392
393 /* Basic assumption: if someone sets sk->sk_err, he _must_
394 * change state of the socket from TCP_SYN_*.
395 * Connect() does not allow to get error notifications
396 * without closing the socket.
397 */
398 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
399 release_sock(sk);
400 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
401 lock_sock(sk);
402 if (signal_pending(current) || !timeo)
403 break;
404 }
405 remove_wait_queue(sk_sleep(sk), &wait);
406 sk->sk_write_pending -= writebias;
407 return timeo;
408 }
409
410 /* Function
411 * The client socket layer is used to establish connection requests
412 * Parameter
413 * sock: The socket
414 * uaddr:The destination address
415 */
__ninet_stream_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)416 int __ninet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
417 int addr_len, int flags)
418 {
419 struct sock *sk = sock->sk;
420 int err;
421 long timeo;
422
423 if (uaddr) {
424 if (addr_len < sizeof(uaddr->sa_family))
425 return -EINVAL;
426 }
427
428 switch (sock->state) {
429 default:
430 err = -EINVAL;
431 goto out;
432 case SS_CONNECTED:
433 err = -EISCONN;
434 goto out;
435 case SS_CONNECTING:
436 err = -EALREADY;
437 break;
438 case SS_UNCONNECTED:
439 err = -EISCONN;
440 if (sk->sk_state != TCP_CLOSE)
441 goto out;
442 /* Call the tcp_nip_connect function */
443 err = sk->sk_prot->connect(sk, uaddr, addr_len);
444 if (err < 0)
445 goto out;
446 /* Switch to connecting, and then perform subsequent operations */
447 sock->state = SS_CONNECTING;
448 err = -EINPROGRESS;
449 break;
450 }
451
452 /* Get blocking time */
453 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
454 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
455 int writebias = 0;
456 /* Error code is set above */
457 if (!timeo || !ninet_wait_for_connect(sk, timeo, writebias))
458 goto out;
459
460 err = sock_intr_errno(timeo);
461 if (signal_pending(current))
462 goto out;
463 }
464
465 if (sk->sk_state == TCP_CLOSE)
466 goto sock_error;
467 sock->state = SS_CONNECTED;
468 err = 0;
469
470 out:
471 return err;
472 sock_error:
473 err = sock_error(sk) ? : -ECONNABORTED;
474 sock->state = SS_DISCONNECTING;
475 goto out;
476 }
477
ninet_stream_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)478 int ninet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
479 int addr_len, int flags)
480 {
481 int err;
482
483 lock_sock(sock->sk);
484 err = __ninet_stream_connect(sock, uaddr, addr_len, flags);
485 release_sock(sock->sk);
486 return err;
487 }
488
ninet_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)489 int ninet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
490 {
491 struct sock *sk = sock->sk;
492 struct net *net = sock_net(sk);
493 const struct proto *prot;
494
495 nip_dbg("cmd=0x%x", cmd);
496 switch (cmd) {
497 case SIOCADDRT:
498 case SIOCDELRT: {
499 struct nip_rtmsg rtmsg;
500
501 if (copy_from_user(&rtmsg, (void __user *)arg, sizeof(rtmsg))) {
502 nip_dbg("fail to copy route cfg data");
503 return -EFAULT;
504 }
505 return nip_route_ioctl(net, cmd, &rtmsg);
506 }
507 case SIOCSIFADDR:
508 return nip_addrconf_add_ifaddr(net, (void __user *)arg);
509 case SIOCDIFADDR:
510 return nip_addrconf_del_ifaddr(net, (void __user *)arg);
511 case SIOCGIFADDR:
512 return nip_addrconf_get_ifaddr(net, cmd, (void __user *)arg);
513 case SIOCGIFCONF: {
514 struct ifconf ifc;
515 int err;
516
517 if (copy_from_user(&ifc, (void __user *)arg, sizeof(struct ifconf)))
518 return -EFAULT;
519 rtnl_lock();
520 err = nip_dev_ifconf(net, &ifc, sizeof(struct ifreq));
521 rtnl_unlock();
522 if (!err && copy_to_user((void __user *)arg, &ifc, sizeof(struct ifconf)))
523 err = -EFAULT;
524 return err;
525 }
526 default:
527 prot = READ_ONCE(sk->sk_prot);
528 if (!prot->ioctl) {
529 nip_dbg("sock sk_prot ioctl is null, cmd=0x%x", cmd);
530 return -ENOIOCTLCMD;
531 }
532 return prot->ioctl(sk, cmd, arg);
533 }
534 }
535
536 #ifdef CONFIG_COMPAT
537 struct compat_nip_rtmsg {
538 struct nip_addr rtmsg_dst;
539 struct nip_addr rtmsg_src;
540 struct nip_addr rtmsg_gateway;
541 char dev_name[10];
542 unsigned int rtmsg_type;
543 int rtmsg_ifindex;
544 unsigned int rtmsg_metric;
545 unsigned int rtmsg_info; /* long convert to int */
546 unsigned int rtmsg_flags;
547 };
548
ninet_compat_routing_ioctl(struct sock * sk,unsigned int cmd,struct compat_nip_rtmsg __user * ur)549 static int ninet_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
550 struct compat_nip_rtmsg __user *ur)
551 {
552 struct nip_rtmsg rt;
553
554 if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst, INDEX_3 * sizeof(struct nip_addr)) ||
555 copy_from_user(&rt.dev_name, &ur->dev_name, sizeof(rt.dev_name)) ||
556 get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
557 get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex) ||
558 get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
559 get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
560 get_user(rt.rtmsg_flags, &ur->rtmsg_flags)) {
561 nip_dbg("fail to convert input para, cmd=0x%x", cmd);
562 return -EFAULT;
563 }
564
565 nip_dbg("cmd=0x%x", cmd);
566 return nip_route_ioctl(sock_net(sk), cmd, &rt);
567 }
568
compat_nip_dev_ifconf(struct net * net,struct compat_ifconf __user * uifc32)569 static int compat_nip_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
570 {
571 struct compat_ifconf ifc32;
572 struct ifconf ifc;
573 int err;
574
575 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
576 return -EFAULT;
577
578 ifc.ifc_len = ifc32.ifc_len;
579 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
580
581 rtnl_lock();
582 err = nip_dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
583 rtnl_unlock();
584 if (err)
585 return err;
586
587 ifc32.ifc_len = ifc.ifc_len;
588 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
589 return -EFAULT;
590
591 return 0;
592 }
593
ninet_compat_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)594 int ninet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
595 {
596 void __user *argp = compat_ptr(arg);
597 struct sock *sk = sock->sk;
598 struct net *net = sock_net(sk);
599
600 switch (cmd) {
601 case SIOCADDRT:
602 case SIOCDELRT:
603 return ninet_compat_routing_ioctl(sk, cmd, argp);
604 case SIOCGIFCONF:
605 return compat_nip_dev_ifconf(net, argp);
606 default:
607 return -ENOIOCTLCMD;
608 }
609 }
610 EXPORT_SYMBOL_GPL(ninet_compat_ioctl);
611
compat_select_ninet_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg,int arglen)612 static int compat_select_ninet_ioctl(struct socket *sock, unsigned int cmd,
613 unsigned long arg, int arglen)
614 {
615 switch (cmd) {
616 case SIOCADDRT:
617 case SIOCDELRT:
618 if (sizeof(struct nip_rtmsg) != arglen) {
619 void __user *argp = compat_ptr(arg);
620 struct sock *sk = sock->sk;
621
622 return ninet_compat_routing_ioctl(sk, cmd, argp);
623 }
624 return ninet_ioctl(sock, cmd, arg);
625 default:
626 return ninet_ioctl(sock, cmd, arg);
627 }
628 }
629 #endif /* CONFIG_COMPAT */
630
__ninet_ioctl_cmd(struct socket * sock,unsigned int cmd,void __user * iov_base,__kernel_size_t iov_len)631 static int __ninet_ioctl_cmd(struct socket *sock, unsigned int cmd,
632 void __user *iov_base, __kernel_size_t iov_len)
633 {
634 unsigned long arg = (unsigned long)((char *)iov_base + NINET_IOCTL_HEAD_LEN);
635 #ifdef CONFIG_COMPAT
636 int arglen = iov_len - NINET_IOCTL_HEAD_LEN;
637
638 return compat_select_ninet_ioctl(sock, cmd, arg, arglen);
639 #else
640 return ninet_ioctl(sock, cmd, arg);
641 #endif
642 }
643
ninet_ioctl_cmd(struct socket * sock,const struct iovec * iov)644 int ninet_ioctl_cmd(struct socket *sock, const struct iovec *iov)
645 {
646 const char ioctl_flag[NINET_IOCTL_FLAG_LEN] = NINET_IOCTL_FLAG_VALUE;
647 char ioctl_head[NINET_IOCTL_HEAD_LEN];
648 int i;
649 unsigned int cmd;
650
651 if (!iov || !iov->iov_base || !sock ||
652 iov->iov_len < NINET_IOCTL_HEAD_LEN) {
653 nip_dbg("invalid parameter");
654 return -NIP_IOCTL_FLAG_INVALID;
655 }
656
657 if (copy_from_user(ioctl_head, (void __user *)iov->iov_base, NINET_IOCTL_HEAD_LEN)) {
658 nip_dbg("fail to copy ioctl head");
659 return -NIP_IOCTL_FLAG_INVALID;
660 }
661
662 for (i = 0; i < NINET_IOCTL_FLAG_LEN; i++) {
663 if (ioctl_head[i] != ioctl_flag[i]) {
664 nip_dbg("not ninet ioctl cmd");
665 return -NIP_IOCTL_FLAG_INVALID;
666 }
667 }
668 cmd = *(unsigned int *)(ioctl_head + NINET_IOCTL_FLAG_LEN);
669 return __ninet_ioctl_cmd(sock, cmd, iov->iov_base, iov->iov_len);
670 }
671
672 /* register new IP socket */
673 const struct proto_ops ninet_dgram_ops = {
674 .family = PF_NINET,
675 .owner = THIS_MODULE,
676 .release = ninet_release,
677 .bind = ninet_bind,
678 .connect = inet_dgram_connect,
679 .socketpair = sock_no_socketpair,
680 .accept = sock_no_accept,
681 .getname = ninet_getname,
682 .poll = datagram_poll,
683 .ioctl = ninet_ioctl,
684 .gettstamp = sock_gettstamp,
685 .listen = sock_no_listen,
686 .shutdown = inet_shutdown,
687 .setsockopt = sock_common_setsockopt,
688 .getsockopt = sock_common_getsockopt,
689 .sendmsg = inet_sendmsg,
690 .recvmsg = inet_recvmsg,
691 .mmap = sock_no_mmap,
692 .sendpage = sock_no_sendpage,
693 .set_peek_off = sk_set_peek_off,
694 #ifdef CONFIG_COMPAT
695 .compat_ioctl = ninet_compat_ioctl,
696 #endif
697 };
698
699 const struct proto_ops ninet_stream_ops = {
700 .family = PF_NINET,
701 .owner = THIS_MODULE,
702 .release = ninet_release,
703 .bind = ninet_bind,
704 .connect = ninet_stream_connect,
705 .socketpair = sock_no_socketpair,
706 .accept = inet_accept,
707 .getname = ninet_getname,
708 .poll = tcp_poll,
709 .ioctl = ninet_ioctl,
710 .listen = ninet_listen,
711 .shutdown = inet_shutdown,
712 .setsockopt = sock_common_setsockopt,
713 .getsockopt = sock_common_getsockopt,
714 .sendmsg = inet_sendmsg,
715 .recvmsg = inet_recvmsg,
716 .mmap = sock_no_mmap,
717 .sendpage = inet_sendpage,
718 #ifdef CONFIG_COMPAT
719 .compat_ioctl = ninet_compat_ioctl,
720 #endif
721 };
722
723 static const struct net_proto_family ninet_family_ops = {
724 .family = PF_NINET,
725 .create = ninet_create,
726 .owner = THIS_MODULE,
727 };
728
ninet_register_protosw(struct inet_protosw * p)729 int ninet_register_protosw(struct inet_protosw *p)
730 {
731 struct list_head *lh;
732 struct inet_protosw *answer;
733 struct list_head *last_perm;
734 int protocol = p->protocol;
735 int ret;
736
737 spin_lock_bh(&inetsw_nip_lock);
738
739 ret = -EINVAL;
740 if (p->type >= SOCK_MAX)
741 goto out_illegal;
742
743 /* If we are trying to override a permanent protocol, bail. */
744 answer = NULL;
745 ret = -EPERM;
746 last_perm = &inetsw_nip[p->type];
747 list_for_each(lh, &inetsw_nip[p->type]) {
748 answer = list_entry(lh, struct inet_protosw, list);
749
750 /* Check only the non-wild match. */
751 if (answer->flags & INET_PROTOSW_PERMANENT) {
752 if (protocol == answer->protocol)
753 break;
754 last_perm = lh;
755 }
756
757 answer = NULL;
758 }
759 if (answer)
760 goto out_permanent;
761
762 list_add_rcu(&p->list, last_perm);
763 ret = 0;
764 out:
765 spin_unlock_bh(&inetsw_nip_lock);
766 return ret;
767
768 out_permanent:
769 nip_dbg("Attempt to override permanent protocol %d", protocol);
770 goto out;
771
772 out_illegal:
773 nip_dbg("Ignoring attempt to register invalid socket type %d", p->type);
774 goto out;
775 }
776
ninet_unregister_protosw(struct inet_protosw * p)777 void ninet_unregister_protosw(struct inet_protosw *p)
778 {
779 if (INET_PROTOSW_PERMANENT & p->flags) {
780 nip_dbg("Attempt to unregister permanent protocol %d", p->protocol);
781 } else {
782 spin_lock_bh(&inetsw_nip_lock);
783 list_del_rcu(&p->list);
784 spin_unlock_bh(&inetsw_nip_lock);
785
786 synchronize_net();
787 }
788 }
789
ninet_sk_rebuild_header(struct sock * sk)790 int ninet_sk_rebuild_header(struct sock *sk)
791 {
792 return 0;
793 }
794
795 /* register to data link layer */
796 static struct packet_type nip_packet_type __read_mostly = {
797 .type = cpu_to_be16(ETH_P_NEWIP),
798 .func = nip_rcv,
799 };
800
nip_packet_init(void)801 static int __init nip_packet_init(void)
802 {
803 dev_add_pack(&nip_packet_type);
804 return 0;
805 }
806
ninet_net_init(struct net * net)807 static int __net_init ninet_net_init(struct net *net)
808 {
809 int err = 0;
810 return err;
811 }
812
ninet_net_exit(struct net * net)813 static void __net_exit ninet_net_exit(struct net *net)
814 {
815 ;
816 }
817
818 static struct pernet_operations ninet_net_ops = {
819 .init = ninet_net_init,
820 .exit = ninet_net_exit,
821 };
822
ninet_init(void)823 static int __init ninet_init(void)
824 {
825 struct list_head *r;
826 int err = 0;
827
828 sock_skb_cb_check_size(sizeof(struct ninet_skb_parm));
829
830 nip_dbg("NET: start to init nip network");
831 /* register the socket-side information for ninet_create */
832 for (r = &inetsw_nip[0]; r < &inetsw_nip[SOCK_MAX]; ++r)
833 INIT_LIST_HEAD(r);
834
835 if (!newip_mod_enabled()) {
836 nip_dbg("Loaded, but administratively disabled, reboot required to enable");
837 goto out;
838 }
839
840 err = proto_register(&tcp_nip_prot, 1);
841 if (err)
842 goto out;
843
844 err = proto_register(&nip_udp_prot, 1);
845 if (err) {
846 nip_dbg("failed to register udp proto");
847 goto out_udp_register_fail;
848 }
849
850 err = sock_register(&ninet_family_ops);
851 if (err) {
852 nip_dbg("failed to register newip_family_ops");
853 goto out_sock_register_fail;
854 }
855
856 err = register_pernet_subsys(&ninet_net_ops);
857 if (err) {
858 nip_dbg("failed to register ninet_net_ops");
859 goto register_pernet_fail;
860 }
861
862 err = nip_icmp_init();
863 if (err) {
864 nip_dbg("nip_icmp_init failed");
865 goto nip_icmp_fail;
866 }
867
868 err = nndisc_init();
869 if (err) {
870 nip_dbg("nndisc_init failed");
871 goto nndisc_fail;
872 }
873
874 err = nip_route_init();
875 if (err)
876 goto nip_route_fail;
877
878 err = nip_addrconf_init();
879 if (err)
880 goto nip_addr_fail;
881
882 err = nip_udp_init();
883 if (err) {
884 nip_dbg("failed to init udp layer");
885 goto udp_fail;
886 }
887
888 err = tcp_nip_init();
889 if (err) {
890 nip_dbg("failed to init tcp layer");
891 goto tcp_fail;
892 } else {
893 nip_dbg("nip_tcp_init ok");
894 }
895
896 err = nip_packet_init();
897 if (err) {
898 nip_dbg("failed to register to l2 layer");
899 goto nip_packet_fail;
900 }
901
902 #ifdef CONFIG_NEWIP_HOOKS
903 ninet_hooks_init();
904 #endif
905 nip_dbg("init newip address family ok");
906
907 out:
908 return err;
909
910 nip_packet_fail:
911 tcp_nip_exit();
912 tcp_fail:
913 nip_udp_exit();
914 udp_fail:
915 nip_addrconf_cleanup();
916 nip_addr_fail:
917 nip_route_cleanup();
918 nip_route_fail:
919 nndisc_fail:
920 nip_icmp_fail:
921 unregister_pernet_subsys(&ninet_net_ops);
922 register_pernet_fail:
923 sock_unregister(PF_NINET);
924 out_sock_register_fail:
925 proto_unregister(&nip_udp_prot);
926 out_udp_register_fail:
927 nip_dbg("newip family init failed");
928 goto out;
929 }
930
931 module_init(ninet_init);
932
933 MODULE_ALIAS_NETPROTO(PF_NINET);
934
935