68 #define pr_fmt(fmt) "IPv4: " fmt
71 #include <linux/errno.h>
72 #include <linux/types.h>
73 #include <linux/socket.h>
75 #include <linux/kernel.h>
76 #include <linux/module.h>
77 #include <linux/sched.h>
79 #include <linux/string.h>
81 #include <linux/net.h>
82 #include <linux/capability.h>
83 #include <linux/fcntl.h>
86 #include <linux/stat.h>
88 #include <linux/poll.h>
89 #include <linux/netfilter_ipv4.h>
90 #include <linux/random.h>
91 #include <linux/slab.h>
93 #include <asm/uaccess.h>
96 #include <linux/igmp.h>
98 #include <linux/netdevice.h>
118 #ifdef CONFIG_IP_MROUTE
119 #include <linux/mroute.h>
144 pr_err(
"Attempt to release TCP socket in state %d %p\n",
149 pr_err(
"Attempt to release alive inet socket %p\n", sk);
175 static int inet_autobind(
struct sock *
sk)
182 if (sk->sk_prot->get_port(sk, 0)) {
197 struct sock *sk = sock->
sk;
198 unsigned char old_state;
207 old_state = sk->sk_state;
223 inet_csk(sk)->icsk_accept_queue.fastopenq ==
NULL) {
225 err = fastopen_init_queue(sk, backlog);
228 err = fastopen_init_queue(sk,
274 if (ipprot ==
NULL) {
285 static int inet_create(
struct net *net,
struct socket *
sock,
int protocol,
291 struct proto *answer_prot;
292 unsigned char answer_flags;
293 char answer_no_check;
294 int try_loading_module = 0;
307 list_for_each_entry_rcu(answer, &inetsw[sock->
type],
list) {
327 if (try_loading_module < 2) {
333 if (++try_loading_module == 1)
334 request_module(
"net-pf-%d-proto-%d-type-%d",
341 request_module(
"net-pf-%d-proto-%d",
343 goto lookup_protocol;
353 if (!inet_netns_ok(net, protocol))
357 answer_prot = answer->
prot;
359 answer_flags = answer->
flags;
416 sk->sk_prot->hash(sk);
419 if (sk->sk_prot->init) {
420 err = sk->sk_prot->init(sk);
439 struct sock *sk = sock->
sk;
444 sock_rps_reset_flow(sk);
461 sk->sk_prot->close(sk, timeout);
474 struct sock *sk = sock->
sk;
481 if (sk->sk_prot->bind) {
482 err = sk->sk_prot->bind(sk, uaddr, addr_len);
534 goto out_release_sock;
541 if (sk->sk_prot->get_port(sk, snum)) {
544 goto out_release_sock;
547 if (inet->inet_rcv_saddr)
552 inet->inet_daddr = 0;
564 int addr_len,
int flags)
566 struct sock *sk = sock->
sk;
571 return sk->sk_prot->disconnect(sk, flags);
573 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
575 return sk->sk_prot->connect(sk, uaddr, addr_len);
579 static long inet_wait_for_connect(
struct sock *sk,
long timeo,
int writebias)
595 if (signal_pending(
current) || !timeo)
609 int addr_len,
int flags)
611 struct sock *sk = sock->
sk;
619 err = sk->sk_prot->disconnect(sk, flags);
624 switch (sock->
state) {
640 err = sk->sk_prot->connect(sk, uaddr, addr_len);
654 timeo = sock_sndtimeo(sk, flags &
O_NONBLOCK);
658 tcp_sk(sk)->fastopen_req &&
659 tcp_sk(sk)->fastopen_req->data ? 1 : 0;
662 if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
665 err = sock_intr_errno(timeo);
689 if (sk->sk_prot->disconnect(sk, flags))
696 int addr_len,
int flags)
713 struct sock *sk1 = sock->
sk;
715 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
722 sock_rps_record_flow(sk2);
723 WARN_ON(!((1 << sk2->sk_state) &
727 sock_graft(sk2, newsock);
742 int *uaddr_len,
int peer)
744 struct sock *sk = sock->
sk;
755 sin->sin_addr.s_addr = inet->inet_daddr;
761 sin->sin_addr.s_addr =
addr;
763 memset(sin->sin_zero, 0,
sizeof(sin->sin_zero));
764 *uaddr_len =
sizeof(*sin);
772 struct sock *sk = sock->
sk;
774 sock_rps_record_flow(sk);
777 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
781 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
788 struct sock *sk = sock->
sk;
790 sock_rps_record_flow(sk);
793 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
797 if (sk->sk_prot->sendpage)
798 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
806 struct sock *sk = sock->
sk;
810 sock_rps_record_flow(sk);
812 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags &
MSG_DONTWAIT,
813 flags & ~MSG_DONTWAIT, &addr_len);
822 struct sock *sk = sock->
sk;
836 if ((1 << sk->sk_state) &
843 switch (sk->sk_state) {
850 if (sk->sk_prot->shutdown)
851 sk->sk_prot->shutdown(sk, how);
863 err = sk->sk_prot->disconnect(sk,
O_NONBLOCK);
887 struct sock *sk = sock->
sk;
889 struct net *net = sock_net(sk);
922 if (sk->sk_prot->ioctl)
923 err = sk->sk_prot->ioctl(sk, cmd, arg);
933 static int inet_compat_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
935 struct sock *sk = sock->
sk;
938 if (sk->sk_prot->compat_ioctl)
939 err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
968 .compat_ioctl = inet_compat_ioctl,
995 .compat_ioctl = inet_compat_ioctl,
1004 static const struct proto_ops inet_sockraw_ops = {
1023 #ifdef CONFIG_COMPAT
1026 .compat_ioctl = inet_compat_ioctl,
1032 .create = inet_create,
1073 .ops = &inet_sockraw_ops,
1079 #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
1088 spin_lock_bh(&inetsw_lock);
1095 last_perm = &inetsw[p->
type];
1117 list_add_rcu(&p->
list, last_perm);
1119 spin_unlock_bh(&inetsw_lock);
1124 pr_err(
"Attempt to override permanent protocol %d\n", protocol);
1128 pr_err(
"Ignoring attempt to register invalid socket type %d\n",
1137 pr_err(
"Attempt to unregister permanent protocol %d\n",
1140 spin_lock_bh(&inetsw_lock);
1141 list_del_rcu(&p->
list);
1142 spin_unlock_bh(&inetsw_lock);
1155 static int inet_sk_reselect_saddr(
struct sock *sk)
1167 if (inet_opt && inet_opt->
opt.srr)
1168 daddr = inet_opt->
opt.faddr;
1171 fl4 = &inet->
cork.fl.u.ip4;
1180 new_saddr = fl4->
saddr;
1182 if (new_saddr == old_saddr)
1185 if (sysctl_ip_dynaddr > 1) {
1186 pr_info(
"%s(): shifting inet->saddr from %pI4 to %pI4\n",
1187 __func__, &old_saddr, &new_saddr);
1190 inet->
inet_saddr = inet->inet_rcv_saddr = new_saddr;
1200 __sk_prot_rehash(sk);
1220 daddr = inet->inet_daddr;
1221 if (inet_opt && inet_opt->
opt.srr)
1222 daddr = inet_opt->
opt.faddr;
1224 fl4 = &inet->
cork.fl.u.ip4;
1225 rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->
inet_saddr,
1228 sk->sk_bound_dev_if);
1244 (err = inet_sk_reselect_saddr(sk)) != 0)
1252 static int inet_gso_send_check(
struct sk_buff *
skb)
1255 const struct iphdr *iph;
1260 if (
unlikely(!pskb_may_pull(skb,
sizeof(*iph))))
1265 if (ihl <
sizeof(*iph))
1268 if (
unlikely(!pskb_may_pull(skb, ihl)))
1271 __skb_pull(skb, ihl);
1272 skb_reset_transport_header(skb);
1301 if (
unlikely(skb_shinfo(skb)->gso_type &
1309 if (
unlikely(!pskb_may_pull(skb,
sizeof(*iph))))
1314 if (ihl <
sizeof(*iph))
1317 if (
unlikely(!pskb_may_pull(skb, ihl)))
1320 __skb_pull(skb, ihl);
1321 skb_reset_transport_header(skb);
1333 if (!segs || IS_ERR(segs))
1344 offset += (skb->
len - skb->
mac_len - iph->ihl * 4);
1350 }
while ((skb = skb->
next));
1362 const struct iphdr *iph;
1369 off = skb_gro_offset(skb);
1370 hlen = off +
sizeof(*iph);
1371 iph = skb_gro_header_fast(skb, off);
1372 if (skb_gro_header_hard(skb, hlen)) {
1373 iph = skb_gro_header_slow(skb, hlen, off);
1378 proto = iph->protocol;
1385 if (*(
u8 *)iph != 0x45)
1395 for (p = *head;
p; p = p->
next) {
1403 if ((iph->protocol ^ iph2->
protocol) |
1412 (iph->ttl ^ iph2->
ttl) |
1413 (iph->tos ^ iph2->
tos) |
1420 skb_gro_pull(skb,
sizeof(*iph));
1421 skb_set_transport_header(skb, skb_gro_offset(skb));
1434 static int inet_gro_complete(
struct sk_buff *skb)
1437 struct iphdr *iph = ip_hdr(skb);
1459 unsigned short type,
unsigned char protocol,
1472 (*sk)->sk_prot->unhash(*sk);
1474 sk_change_net(*sk, net);
1482 unsigned long res = 0;
1487 res += *(((
unsigned long *)
per_cpu_ptr(mib[j], i)) + offt);
1493 #if BITS_PER_LONG==32
1495 u64 snmp_fold_field64(
void __percpu *mib[],
int offt,
size_t syncp_offset)
1509 start = u64_stats_fetch_begin_bh(syncp);
1510 v = *(((
u64 *) bhptr) + offt);
1511 }
while (u64_stats_fetch_retry_bh(syncp, start));
1526 #if SNMP_ARRAY_SZ == 2
1550 #ifdef CONFIG_IP_MULTICAST
1585 static __net_init int ipv4_mib_init_net(
struct net *net)
1589 __alignof__(
struct tcp_mib)) < 0)
1601 __alignof__(
struct udp_mib)) < 0)
1605 __alignof__(
struct udp_mib)) < 0)
1606 goto err_udplite_mib;
1611 net->
mib.icmpmsg_statistics = kzalloc(
sizeof(
struct icmpmsg_mib),
1613 if (!net->
mib.icmpmsg_statistics)
1614 goto err_icmpmsg_mib;
1635 static __net_exit void ipv4_mib_exit_net(
struct net *net)
1637 kfree(net->
mib.icmpmsg_statistics);
1647 .init = ipv4_mib_init_net,
1648 .exit = ipv4_mib_exit_net,
1651 static int __init init_ipv4_mibs(
void)
1656 static int ipv4_proc_init(
void);
1662 static struct packet_type ip_packet_type __read_mostly = {
1665 .gso_send_check = inet_gso_send_check,
1666 .gso_segment = inet_gso_segment,
1667 .gro_receive = inet_gro_receive,
1668 .gro_complete = inet_gro_complete,
1671 static int __init inet_init(
void)
1686 goto out_free_reserved_ports;
1690 goto out_unregister_tcp_proto;
1694 goto out_unregister_udp_proto;
1698 goto out_unregister_raw_proto;
1706 #ifdef CONFIG_SYSCTL
1717 pr_crit(
"%s: Cannot add ICMP protocol\n", __func__);
1719 pr_crit(
"%s: Cannot add UDP protocol\n", __func__);
1721 pr_crit(
"%s: Cannot add TCP protocol\n", __func__);
1722 #ifdef CONFIG_IP_MULTICAST
1724 pr_crit(
"%s: Cannot add IGMP protocol\n", __func__);
1728 for (r = &inetsw[0]; r < &inetsw[
SOCK_MAX]; ++
r)
1764 panic(
"Failed to create the ICMP control socket.\n");
1769 #if defined(CONFIG_IP_MROUTE)
1771 pr_crit(
"%s: Cannot init ipv4 mroute\n", __func__);
1777 if (init_ipv4_mibs())
1778 pr_crit(
"%s: Cannot init ipv4 mibs\n", __func__);
1789 out_unregister_raw_proto:
1791 out_unregister_udp_proto:
1793 out_unregister_tcp_proto:
1795 out_free_reserved_ports:
1804 #ifdef CONFIG_PROC_FS
1805 static int __init ipv4_proc_init(
void)
1809 if (raw_proc_init())
1811 if (tcp4_proc_init())
1813 if (udp4_proc_init())
1815 if (ping_proc_init())
1835 static int __init ipv4_proc_init(
void)