aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv4/af_inet.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/ipv4/af_inet.c')
-rw-r--r--net/ipv4/af_inet.c126
1 files changed, 65 insertions, 61 deletions
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index 57737b8d1711..f71357422380 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -86,6 +86,7 @@
86#include <linux/poll.h> 86#include <linux/poll.h>
87#include <linux/netfilter_ipv4.h> 87#include <linux/netfilter_ipv4.h>
88#include <linux/random.h> 88#include <linux/random.h>
89#include <linux/slab.h>
89 90
90#include <asm/uaccess.h> 91#include <asm/uaccess.h>
91#include <asm/system.h> 92#include <asm/system.h>
@@ -174,12 +175,12 @@ static int inet_autobind(struct sock *sk)
174 /* We may need to bind the socket. */ 175 /* We may need to bind the socket. */
175 lock_sock(sk); 176 lock_sock(sk);
176 inet = inet_sk(sk); 177 inet = inet_sk(sk);
177 if (!inet->num) { 178 if (!inet->inet_num) {
178 if (sk->sk_prot->get_port(sk, 0)) { 179 if (sk->sk_prot->get_port(sk, 0)) {
179 release_sock(sk); 180 release_sock(sk);
180 return -EAGAIN; 181 return -EAGAIN;
181 } 182 }
182 inet->sport = htons(inet->num); 183 inet->inet_sport = htons(inet->inet_num);
183 } 184 }
184 release_sock(sk); 185 release_sock(sk);
185 return 0; 186 return 0;
@@ -262,7 +263,8 @@ static inline int inet_netns_ok(struct net *net, int protocol)
262 * Create an inet socket. 263 * Create an inet socket.
263 */ 264 */
264 265
265static int inet_create(struct net *net, struct socket *sock, int protocol) 266static int inet_create(struct net *net, struct socket *sock, int protocol,
267 int kern)
266{ 268{
267 struct sock *sk; 269 struct sock *sk;
268 struct inet_protosw *answer; 270 struct inet_protosw *answer;
@@ -325,7 +327,7 @@ lookup_protocol:
325 } 327 }
326 328
327 err = -EPERM; 329 err = -EPERM;
328 if (answer->capability > 0 && !capable(answer->capability)) 330 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
329 goto out_rcu_unlock; 331 goto out_rcu_unlock;
330 332
331 err = -EAFNOSUPPORT; 333 err = -EAFNOSUPPORT;
@@ -354,7 +356,7 @@ lookup_protocol:
354 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0; 356 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
355 357
356 if (SOCK_RAW == sock->type) { 358 if (SOCK_RAW == sock->type) {
357 inet->num = protocol; 359 inet->inet_num = protocol;
358 if (IPPROTO_RAW == protocol) 360 if (IPPROTO_RAW == protocol)
359 inet->hdrincl = 1; 361 inet->hdrincl = 1;
360 } 362 }
@@ -364,7 +366,7 @@ lookup_protocol:
364 else 366 else
365 inet->pmtudisc = IP_PMTUDISC_WANT; 367 inet->pmtudisc = IP_PMTUDISC_WANT;
366 368
367 inet->id = 0; 369 inet->inet_id = 0;
368 370
369 sock_init_data(sock, sk); 371 sock_init_data(sock, sk);
370 372
@@ -381,13 +383,13 @@ lookup_protocol:
381 383
382 sk_refcnt_debug_inc(sk); 384 sk_refcnt_debug_inc(sk);
383 385
384 if (inet->num) { 386 if (inet->inet_num) {
385 /* It assumes that any protocol which allows 387 /* It assumes that any protocol which allows
386 * the user to assign a number at socket 388 * the user to assign a number at socket
387 * creation time automatically 389 * creation time automatically
388 * shares. 390 * shares.
389 */ 391 */
390 inet->sport = htons(inet->num); 392 inet->inet_sport = htons(inet->inet_num);
391 /* Add to protocol hash chains. */ 393 /* Add to protocol hash chains. */
392 sk->sk_prot->hash(sk); 394 sk->sk_prot->hash(sk);
393 } 395 }
@@ -494,27 +496,27 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
494 496
495 /* Check these errors (active socket, double bind). */ 497 /* Check these errors (active socket, double bind). */
496 err = -EINVAL; 498 err = -EINVAL;
497 if (sk->sk_state != TCP_CLOSE || inet->num) 499 if (sk->sk_state != TCP_CLOSE || inet->inet_num)
498 goto out_release_sock; 500 goto out_release_sock;
499 501
500 inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr; 502 inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
501 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) 503 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
502 inet->saddr = 0; /* Use device */ 504 inet->inet_saddr = 0; /* Use device */
503 505
504 /* Make sure we are allowed to bind here. */ 506 /* Make sure we are allowed to bind here. */
505 if (sk->sk_prot->get_port(sk, snum)) { 507 if (sk->sk_prot->get_port(sk, snum)) {
506 inet->saddr = inet->rcv_saddr = 0; 508 inet->inet_saddr = inet->inet_rcv_saddr = 0;
507 err = -EADDRINUSE; 509 err = -EADDRINUSE;
508 goto out_release_sock; 510 goto out_release_sock;
509 } 511 }
510 512
511 if (inet->rcv_saddr) 513 if (inet->inet_rcv_saddr)
512 sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 514 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
513 if (snum) 515 if (snum)
514 sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 516 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
515 inet->sport = htons(inet->num); 517 inet->inet_sport = htons(inet->inet_num);
516 inet->daddr = 0; 518 inet->inet_daddr = 0;
517 inet->dport = 0; 519 inet->inet_dport = 0;
518 sk_dst_reset(sk); 520 sk_dst_reset(sk);
519 err = 0; 521 err = 0;
520out_release_sock: 522out_release_sock:
@@ -529,10 +531,12 @@ int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
529{ 531{
530 struct sock *sk = sock->sk; 532 struct sock *sk = sock->sk;
531 533
534 if (addr_len < sizeof(uaddr->sa_family))
535 return -EINVAL;
532 if (uaddr->sa_family == AF_UNSPEC) 536 if (uaddr->sa_family == AF_UNSPEC)
533 return sk->sk_prot->disconnect(sk, flags); 537 return sk->sk_prot->disconnect(sk, flags);
534 538
535 if (!inet_sk(sk)->num && inet_autobind(sk)) 539 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
536 return -EAGAIN; 540 return -EAGAIN;
537 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len); 541 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
538} 542}
@@ -572,6 +576,9 @@ int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
572 int err; 576 int err;
573 long timeo; 577 long timeo;
574 578
579 if (addr_len < sizeof(uaddr->sa_family))
580 return -EINVAL;
581
575 lock_sock(sk); 582 lock_sock(sk);
576 583
577 if (uaddr->sa_family == AF_UNSPEC) { 584 if (uaddr->sa_family == AF_UNSPEC) {
@@ -685,21 +692,21 @@ int inet_getname(struct socket *sock, struct sockaddr *uaddr,
685{ 692{
686 struct sock *sk = sock->sk; 693 struct sock *sk = sock->sk;
687 struct inet_sock *inet = inet_sk(sk); 694 struct inet_sock *inet = inet_sk(sk);
688 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; 695 DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
689 696
690 sin->sin_family = AF_INET; 697 sin->sin_family = AF_INET;
691 if (peer) { 698 if (peer) {
692 if (!inet->dport || 699 if (!inet->inet_dport ||
693 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 700 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
694 peer == 1)) 701 peer == 1))
695 return -ENOTCONN; 702 return -ENOTCONN;
696 sin->sin_port = inet->dport; 703 sin->sin_port = inet->inet_dport;
697 sin->sin_addr.s_addr = inet->daddr; 704 sin->sin_addr.s_addr = inet->inet_daddr;
698 } else { 705 } else {
699 __be32 addr = inet->rcv_saddr; 706 __be32 addr = inet->inet_rcv_saddr;
700 if (!addr) 707 if (!addr)
701 addr = inet->saddr; 708 addr = inet->inet_saddr;
702 sin->sin_port = inet->sport; 709 sin->sin_port = inet->inet_sport;
703 sin->sin_addr.s_addr = addr; 710 sin->sin_addr.s_addr = addr;
704 } 711 }
705 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 712 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
@@ -714,7 +721,7 @@ int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
714 struct sock *sk = sock->sk; 721 struct sock *sk = sock->sk;
715 722
716 /* We may need to bind the socket. */ 723 /* We may need to bind the socket. */
717 if (!inet_sk(sk)->num && inet_autobind(sk)) 724 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
718 return -EAGAIN; 725 return -EAGAIN;
719 726
720 return sk->sk_prot->sendmsg(iocb, sk, msg, size); 727 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
@@ -728,7 +735,7 @@ static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
728 struct sock *sk = sock->sk; 735 struct sock *sk = sock->sk;
729 736
730 /* We may need to bind the socket. */ 737 /* We may need to bind the socket. */
731 if (!inet_sk(sk)->num && inet_autobind(sk)) 738 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
732 return -EAGAIN; 739 return -EAGAIN;
733 740
734 if (sk->sk_prot->sendpage) 741 if (sk->sk_prot->sendpage)
@@ -931,7 +938,7 @@ static const struct proto_ops inet_sockraw_ops = {
931#endif 938#endif
932}; 939};
933 940
934static struct net_proto_family inet_family_ops = { 941static const struct net_proto_family inet_family_ops = {
935 .family = PF_INET, 942 .family = PF_INET,
936 .create = inet_create, 943 .create = inet_create,
937 .owner = THIS_MODULE, 944 .owner = THIS_MODULE,
@@ -947,7 +954,6 @@ static struct inet_protosw inetsw_array[] =
947 .protocol = IPPROTO_TCP, 954 .protocol = IPPROTO_TCP,
948 .prot = &tcp_prot, 955 .prot = &tcp_prot,
949 .ops = &inet_stream_ops, 956 .ops = &inet_stream_ops,
950 .capability = -1,
951 .no_check = 0, 957 .no_check = 0,
952 .flags = INET_PROTOSW_PERMANENT | 958 .flags = INET_PROTOSW_PERMANENT |
953 INET_PROTOSW_ICSK, 959 INET_PROTOSW_ICSK,
@@ -958,7 +964,6 @@ static struct inet_protosw inetsw_array[] =
958 .protocol = IPPROTO_UDP, 964 .protocol = IPPROTO_UDP,
959 .prot = &udp_prot, 965 .prot = &udp_prot,
960 .ops = &inet_dgram_ops, 966 .ops = &inet_dgram_ops,
961 .capability = -1,
962 .no_check = UDP_CSUM_DEFAULT, 967 .no_check = UDP_CSUM_DEFAULT,
963 .flags = INET_PROTOSW_PERMANENT, 968 .flags = INET_PROTOSW_PERMANENT,
964 }, 969 },
@@ -969,7 +974,6 @@ static struct inet_protosw inetsw_array[] =
969 .protocol = IPPROTO_IP, /* wild card */ 974 .protocol = IPPROTO_IP, /* wild card */
970 .prot = &raw_prot, 975 .prot = &raw_prot,
971 .ops = &inet_sockraw_ops, 976 .ops = &inet_sockraw_ops,
972 .capability = CAP_NET_RAW,
973 .no_check = UDP_CSUM_DEFAULT, 977 .no_check = UDP_CSUM_DEFAULT,
974 .flags = INET_PROTOSW_REUSE, 978 .flags = INET_PROTOSW_REUSE,
975 } 979 }
@@ -1059,9 +1063,9 @@ static int inet_sk_reselect_saddr(struct sock *sk)
1059 struct inet_sock *inet = inet_sk(sk); 1063 struct inet_sock *inet = inet_sk(sk);
1060 int err; 1064 int err;
1061 struct rtable *rt; 1065 struct rtable *rt;
1062 __be32 old_saddr = inet->saddr; 1066 __be32 old_saddr = inet->inet_saddr;
1063 __be32 new_saddr; 1067 __be32 new_saddr;
1064 __be32 daddr = inet->daddr; 1068 __be32 daddr = inet->inet_daddr;
1065 1069
1066 if (inet->opt && inet->opt->srr) 1070 if (inet->opt && inet->opt->srr)
1067 daddr = inet->opt->faddr; 1071 daddr = inet->opt->faddr;
@@ -1071,7 +1075,7 @@ static int inet_sk_reselect_saddr(struct sock *sk)
1071 RT_CONN_FLAGS(sk), 1075 RT_CONN_FLAGS(sk),
1072 sk->sk_bound_dev_if, 1076 sk->sk_bound_dev_if,
1073 sk->sk_protocol, 1077 sk->sk_protocol,
1074 inet->sport, inet->dport, sk, 0); 1078 inet->inet_sport, inet->inet_dport, sk, 0);
1075 if (err) 1079 if (err)
1076 return err; 1080 return err;
1077 1081
@@ -1087,7 +1091,7 @@ static int inet_sk_reselect_saddr(struct sock *sk)
1087 __func__, &old_saddr, &new_saddr); 1091 __func__, &old_saddr, &new_saddr);
1088 } 1092 }
1089 1093
1090 inet->saddr = inet->rcv_saddr = new_saddr; 1094 inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
1091 1095
1092 /* 1096 /*
1093 * XXX The only one ugly spot where we need to 1097 * XXX The only one ugly spot where we need to
@@ -1113,7 +1117,7 @@ int inet_sk_rebuild_header(struct sock *sk)
1113 return 0; 1117 return 0;
1114 1118
1115 /* Reroute. */ 1119 /* Reroute. */
1116 daddr = inet->daddr; 1120 daddr = inet->inet_daddr;
1117 if (inet->opt && inet->opt->srr) 1121 if (inet->opt && inet->opt->srr)
1118 daddr = inet->opt->faddr; 1122 daddr = inet->opt->faddr;
1119{ 1123{
@@ -1123,7 +1127,7 @@ int inet_sk_rebuild_header(struct sock *sk)
1123 .nl_u = { 1127 .nl_u = {
1124 .ip4_u = { 1128 .ip4_u = {
1125 .daddr = daddr, 1129 .daddr = daddr,
1126 .saddr = inet->saddr, 1130 .saddr = inet->inet_saddr,
1127 .tos = RT_CONN_FLAGS(sk), 1131 .tos = RT_CONN_FLAGS(sk),
1128 }, 1132 },
1129 }, 1133 },
@@ -1131,8 +1135,8 @@ int inet_sk_rebuild_header(struct sock *sk)
1131 .flags = inet_sk_flowi_flags(sk), 1135 .flags = inet_sk_flowi_flags(sk),
1132 .uli_u = { 1136 .uli_u = {
1133 .ports = { 1137 .ports = {
1134 .sport = inet->sport, 1138 .sport = inet->inet_sport,
1135 .dport = inet->dport, 1139 .dport = inet->inet_dport,
1136 }, 1140 },
1137 }, 1141 },
1138 }; 1142 };
@@ -1387,7 +1391,7 @@ int inet_ctl_sock_create(struct sock **sk, unsigned short family,
1387} 1391}
1388EXPORT_SYMBOL_GPL(inet_ctl_sock_create); 1392EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1389 1393
1390unsigned long snmp_fold_field(void *mib[], int offt) 1394unsigned long snmp_fold_field(void __percpu *mib[], int offt)
1391{ 1395{
1392 unsigned long res = 0; 1396 unsigned long res = 0;
1393 int i; 1397 int i;
@@ -1400,7 +1404,7 @@ unsigned long snmp_fold_field(void *mib[], int offt)
1400} 1404}
1401EXPORT_SYMBOL_GPL(snmp_fold_field); 1405EXPORT_SYMBOL_GPL(snmp_fold_field);
1402 1406
1403int snmp_mib_init(void *ptr[2], size_t mibsize) 1407int snmp_mib_init(void __percpu *ptr[2], size_t mibsize)
1404{ 1408{
1405 BUG_ON(ptr == NULL); 1409 BUG_ON(ptr == NULL);
1406 ptr[0] = __alloc_percpu(mibsize, __alignof__(unsigned long long)); 1410 ptr[0] = __alloc_percpu(mibsize, __alignof__(unsigned long long));
@@ -1418,7 +1422,7 @@ err0:
1418} 1422}
1419EXPORT_SYMBOL_GPL(snmp_mib_init); 1423EXPORT_SYMBOL_GPL(snmp_mib_init);
1420 1424
1421void snmp_mib_free(void *ptr[2]) 1425void snmp_mib_free(void __percpu *ptr[2])
1422{ 1426{
1423 BUG_ON(ptr == NULL); 1427 BUG_ON(ptr == NULL);
1424 free_percpu(ptr[0]); 1428 free_percpu(ptr[0]);
@@ -1462,25 +1466,25 @@ static const struct net_protocol icmp_protocol = {
1462 1466
1463static __net_init int ipv4_mib_init_net(struct net *net) 1467static __net_init int ipv4_mib_init_net(struct net *net)
1464{ 1468{
1465 if (snmp_mib_init((void **)net->mib.tcp_statistics, 1469 if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics,
1466 sizeof(struct tcp_mib)) < 0) 1470 sizeof(struct tcp_mib)) < 0)
1467 goto err_tcp_mib; 1471 goto err_tcp_mib;
1468 if (snmp_mib_init((void **)net->mib.ip_statistics, 1472 if (snmp_mib_init((void __percpu **)net->mib.ip_statistics,
1469 sizeof(struct ipstats_mib)) < 0) 1473 sizeof(struct ipstats_mib)) < 0)
1470 goto err_ip_mib; 1474 goto err_ip_mib;
1471 if (snmp_mib_init((void **)net->mib.net_statistics, 1475 if (snmp_mib_init((void __percpu **)net->mib.net_statistics,
1472 sizeof(struct linux_mib)) < 0) 1476 sizeof(struct linux_mib)) < 0)
1473 goto err_net_mib; 1477 goto err_net_mib;
1474 if (snmp_mib_init((void **)net->mib.udp_statistics, 1478 if (snmp_mib_init((void __percpu **)net->mib.udp_statistics,
1475 sizeof(struct udp_mib)) < 0) 1479 sizeof(struct udp_mib)) < 0)
1476 goto err_udp_mib; 1480 goto err_udp_mib;
1477 if (snmp_mib_init((void **)net->mib.udplite_statistics, 1481 if (snmp_mib_init((void __percpu **)net->mib.udplite_statistics,
1478 sizeof(struct udp_mib)) < 0) 1482 sizeof(struct udp_mib)) < 0)
1479 goto err_udplite_mib; 1483 goto err_udplite_mib;
1480 if (snmp_mib_init((void **)net->mib.icmp_statistics, 1484 if (snmp_mib_init((void __percpu **)net->mib.icmp_statistics,
1481 sizeof(struct icmp_mib)) < 0) 1485 sizeof(struct icmp_mib)) < 0)
1482 goto err_icmp_mib; 1486 goto err_icmp_mib;
1483 if (snmp_mib_init((void **)net->mib.icmpmsg_statistics, 1487 if (snmp_mib_init((void __percpu **)net->mib.icmpmsg_statistics,
1484 sizeof(struct icmpmsg_mib)) < 0) 1488 sizeof(struct icmpmsg_mib)) < 0)
1485 goto err_icmpmsg_mib; 1489 goto err_icmpmsg_mib;
1486 1490
@@ -1488,30 +1492,30 @@ static __net_init int ipv4_mib_init_net(struct net *net)
1488 return 0; 1492 return 0;
1489 1493
1490err_icmpmsg_mib: 1494err_icmpmsg_mib:
1491 snmp_mib_free((void **)net->mib.icmp_statistics); 1495 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
1492err_icmp_mib: 1496err_icmp_mib:
1493 snmp_mib_free((void **)net->mib.udplite_statistics); 1497 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
1494err_udplite_mib: 1498err_udplite_mib:
1495 snmp_mib_free((void **)net->mib.udp_statistics); 1499 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
1496err_udp_mib: 1500err_udp_mib:
1497 snmp_mib_free((void **)net->mib.net_statistics); 1501 snmp_mib_free((void __percpu **)net->mib.net_statistics);
1498err_net_mib: 1502err_net_mib:
1499 snmp_mib_free((void **)net->mib.ip_statistics); 1503 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
1500err_ip_mib: 1504err_ip_mib:
1501 snmp_mib_free((void **)net->mib.tcp_statistics); 1505 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
1502err_tcp_mib: 1506err_tcp_mib:
1503 return -ENOMEM; 1507 return -ENOMEM;
1504} 1508}
1505 1509
1506static __net_exit void ipv4_mib_exit_net(struct net *net) 1510static __net_exit void ipv4_mib_exit_net(struct net *net)
1507{ 1511{
1508 snmp_mib_free((void **)net->mib.icmpmsg_statistics); 1512 snmp_mib_free((void __percpu **)net->mib.icmpmsg_statistics);
1509 snmp_mib_free((void **)net->mib.icmp_statistics); 1513 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
1510 snmp_mib_free((void **)net->mib.udplite_statistics); 1514 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
1511 snmp_mib_free((void **)net->mib.udp_statistics); 1515 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
1512 snmp_mib_free((void **)net->mib.net_statistics); 1516 snmp_mib_free((void __percpu **)net->mib.net_statistics);
1513 snmp_mib_free((void **)net->mib.ip_statistics); 1517 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
1514 snmp_mib_free((void **)net->mib.tcp_statistics); 1518 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
1515} 1519}
1516 1520
1517static __net_initdata struct pernet_operations ipv4_mib_ops = { 1521static __net_initdata struct pernet_operations ipv4_mib_ops = {