aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv4/udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/ipv4/udp.c')
-rw-r--r--net/ipv4/udp.c109
1 files changed, 55 insertions, 54 deletions
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 8b54c68a0d12..ce6c46034314 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -20,8 +20,8 @@
20 * for udp at least is 'valid'. 20 * for udp at least is 'valid'.
21 * Alan Cox : Fixed icmp handling properly 21 * Alan Cox : Fixed icmp handling properly
22 * Alan Cox : Correct error for oversized datagrams 22 * Alan Cox : Correct error for oversized datagrams
23 * Alan Cox : Tidied select() semantics. 23 * Alan Cox : Tidied select() semantics.
24 * Alan Cox : udp_err() fixed properly, also now 24 * Alan Cox : udp_err() fixed properly, also now
25 * select and read wake correctly on errors 25 * select and read wake correctly on errors
26 * Alan Cox : udp_send verify_area moved to avoid mem leak 26 * Alan Cox : udp_send verify_area moved to avoid mem leak
27 * Alan Cox : UDP can count its memory 27 * Alan Cox : UDP can count its memory
@@ -56,7 +56,7 @@
56 * does have a high hit rate. 56 * does have a high hit rate.
57 * Olaf Kirch : Don't linearise iovec on sendmsg. 57 * Olaf Kirch : Don't linearise iovec on sendmsg.
58 * Andi Kleen : Some cleanups, cache destination entry 58 * Andi Kleen : Some cleanups, cache destination entry
59 * for connect. 59 * for connect.
60 * Vitaly E. Lavrov : Transparent proxy revived after year coma. 60 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
61 * Melvin Smith : Check msg_name not msg_namelen in sendto(), 61 * Melvin Smith : Check msg_name not msg_namelen in sendto(),
62 * return ENOTCONN for unconnected sockets (POSIX) 62 * return ENOTCONN for unconnected sockets (POSIX)
@@ -77,7 +77,7 @@
77 * as published by the Free Software Foundation; either version 77 * as published by the Free Software Foundation; either version
78 * 2 of the License, or (at your option) any later version. 78 * 2 of the License, or (at your option) any later version.
79 */ 79 */
80 80
81#include <asm/system.h> 81#include <asm/system.h>
82#include <asm/uaccess.h> 82#include <asm/uaccess.h>
83#include <asm/ioctls.h> 83#include <asm/ioctls.h>
@@ -120,7 +120,7 @@ static inline int __udp_lib_lport_inuse(__u16 num, struct hlist_head udptable[])
120 struct hlist_node *node; 120 struct hlist_node *node;
121 121
122 sk_for_each(sk, node, &udptable[num & (UDP_HTABLE_SIZE - 1)]) 122 sk_for_each(sk, node, &udptable[num & (UDP_HTABLE_SIZE - 1)])
123 if (inet_sk(sk)->num == num) 123 if (sk->sk_hash == num)
124 return 1; 124 return 1;
125 return 0; 125 return 0;
126} 126}
@@ -191,7 +191,7 @@ gotit:
191 head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; 191 head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
192 192
193 sk_for_each(sk2, node, head) 193 sk_for_each(sk2, node, head)
194 if (inet_sk(sk2)->num == snum && 194 if (sk2->sk_hash == snum &&
195 sk2 != sk && 195 sk2 != sk &&
196 (!sk2->sk_reuse || !sk->sk_reuse) && 196 (!sk2->sk_reuse || !sk->sk_reuse) &&
197 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 197 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
@@ -200,6 +200,7 @@ gotit:
200 goto fail; 200 goto fail;
201 } 201 }
202 inet_sk(sk)->num = snum; 202 inet_sk(sk)->num = snum;
203 sk->sk_hash = snum;
203 if (sk_unhashed(sk)) { 204 if (sk_unhashed(sk)) {
204 head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; 205 head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
205 sk_add_node(sk, head); 206 sk_add_node(sk, head);
@@ -247,7 +248,7 @@ static struct sock *__udp4_lib_lookup(__be32 saddr, __be16 sport,
247 sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) { 248 sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) {
248 struct inet_sock *inet = inet_sk(sk); 249 struct inet_sock *inet = inet_sk(sk);
249 250
250 if (inet->num == hnum && !ipv6_only_sock(sk)) { 251 if (sk->sk_hash == hnum && !ipv6_only_sock(sk)) {
251 int score = (sk->sk_family == PF_INET ? 1 : 0); 252 int score = (sk->sk_family == PF_INET ? 1 : 0);
252 if (inet->rcv_saddr) { 253 if (inet->rcv_saddr) {
253 if (inet->rcv_saddr != daddr) 254 if (inet->rcv_saddr != daddr)
@@ -296,7 +297,7 @@ static inline struct sock *udp_v4_mcast_next(struct sock *sk,
296 sk_for_each_from(s, node) { 297 sk_for_each_from(s, node) {
297 struct inet_sock *inet = inet_sk(s); 298 struct inet_sock *inet = inet_sk(s);
298 299
299 if (inet->num != hnum || 300 if (s->sk_hash != hnum ||
300 (inet->daddr && inet->daddr != rmt_addr) || 301 (inet->daddr && inet->daddr != rmt_addr) ||
301 (inet->dport != rmt_port && inet->dport) || 302 (inet->dport != rmt_port && inet->dport) ||
302 (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || 303 (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
@@ -306,17 +307,17 @@ static inline struct sock *udp_v4_mcast_next(struct sock *sk,
306 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 307 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
307 continue; 308 continue;
308 goto found; 309 goto found;
309 } 310 }
310 s = NULL; 311 s = NULL;
311found: 312found:
312 return s; 313 return s;
313} 314}
314 315
315/* 316/*
316 * This routine is called by the ICMP module when it gets some 317 * This routine is called by the ICMP module when it gets some
317 * sort of error condition. If err < 0 then the socket should 318 * sort of error condition. If err < 0 then the socket should
318 * be closed and the error returned to the user. If err > 0 319 * be closed and the error returned to the user. If err > 0
319 * it's just the icmp type << 8 | icmp code. 320 * it's just the icmp type << 8 | icmp code.
320 * Header points to the ip header of the error packet. We move 321 * Header points to the ip header of the error packet. We move
321 * on past this. Then (as it used to claim before adjustment) 322 * on past this. Then (as it used to claim before adjustment)
322 * header points to the first 8 bytes of the udp header. We need 323 * header points to the first 8 bytes of the udp header. We need
@@ -338,7 +339,7 @@ void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[])
338 skb->dev->ifindex, udptable ); 339 skb->dev->ifindex, udptable );
339 if (sk == NULL) { 340 if (sk == NULL) {
340 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); 341 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
341 return; /* No socket for error */ 342 return; /* No socket for error */
342 } 343 }
343 344
344 err = 0; 345 err = 0;
@@ -374,7 +375,7 @@ void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[])
374 } 375 }
375 376
376 /* 377 /*
377 * RFC1122: OK. Passes ICMP errors back to application, as per 378 * RFC1122: OK. Passes ICMP errors back to application, as per
378 * 4.1.3.3. 379 * 4.1.3.3.
379 */ 380 */
380 if (!inet->recverr) { 381 if (!inet->recverr) {
@@ -524,7 +525,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
524 if (len > 0xFFFF) 525 if (len > 0xFFFF)
525 return -EMSGSIZE; 526 return -EMSGSIZE;
526 527
527 /* 528 /*
528 * Check the flags. 529 * Check the flags.
529 */ 530 */
530 531
@@ -536,7 +537,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
536 if (up->pending) { 537 if (up->pending) {
537 /* 538 /*
538 * There are pending frames. 539 * There are pending frames.
539 * The socket lock must be held while it's corked. 540 * The socket lock must be held while it's corked.
540 */ 541 */
541 lock_sock(sk); 542 lock_sock(sk);
542 if (likely(up->pending)) { 543 if (likely(up->pending)) {
@@ -544,14 +545,14 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
544 release_sock(sk); 545 release_sock(sk);
545 return -EINVAL; 546 return -EINVAL;
546 } 547 }
547 goto do_append_data; 548 goto do_append_data;
548 } 549 }
549 release_sock(sk); 550 release_sock(sk);
550 } 551 }
551 ulen += sizeof(struct udphdr); 552 ulen += sizeof(struct udphdr);
552 553
553 /* 554 /*
554 * Get and verify the address. 555 * Get and verify the address.
555 */ 556 */
556 if (msg->msg_name) { 557 if (msg->msg_name) {
557 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 558 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
@@ -575,7 +576,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
575 Route will not be used, if at least one option is set. 576 Route will not be used, if at least one option is set.
576 */ 577 */
577 connected = 1; 578 connected = 1;
578 } 579 }
579 ipc.addr = inet->saddr; 580 ipc.addr = inet->saddr;
580 581
581 ipc.oif = sk->sk_bound_dev_if; 582 ipc.oif = sk->sk_bound_dev_if;
@@ -601,7 +602,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
601 } 602 }
602 tos = RT_TOS(inet->tos); 603 tos = RT_TOS(inet->tos);
603 if (sock_flag(sk, SOCK_LOCALROUTE) || 604 if (sock_flag(sk, SOCK_LOCALROUTE) ||
604 (msg->msg_flags & MSG_DONTROUTE) || 605 (msg->msg_flags & MSG_DONTROUTE) ||
605 (ipc.opt && ipc.opt->is_strictroute)) { 606 (ipc.opt && ipc.opt->is_strictroute)) {
606 tos |= RTO_ONLINK; 607 tos |= RTO_ONLINK;
607 connected = 0; 608 connected = 0;
@@ -761,10 +762,10 @@ out:
761/* 762/*
762 * IOCTL requests applicable to the UDP protocol 763 * IOCTL requests applicable to the UDP protocol
763 */ 764 */
764 765
765int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 766int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
766{ 767{
767 switch(cmd) 768 switch(cmd)
768 { 769 {
769 case SIOCOUTQ: 770 case SIOCOUTQ:
770 { 771 {
@@ -804,11 +805,11 @@ int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
804 */ 805 */
805 806
806int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 807int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
807 size_t len, int noblock, int flags, int *addr_len) 808 size_t len, int noblock, int flags, int *addr_len)
808{ 809{
809 struct inet_sock *inet = inet_sk(sk); 810 struct inet_sock *inet = inet_sk(sk);
810 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 811 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
811 struct sk_buff *skb; 812 struct sk_buff *skb;
812 int copied, err, copy_only, is_udplite = IS_UDPLITE(sk); 813 int copied, err, copy_only, is_udplite = IS_UDPLITE(sk);
813 814
814 /* 815 /*
@@ -824,8 +825,8 @@ try_again:
824 skb = skb_recv_datagram(sk, flags, noblock, &err); 825 skb = skb_recv_datagram(sk, flags, noblock, &err);
825 if (!skb) 826 if (!skb)
826 goto out; 827 goto out;
827 828
828 copied = skb->len - sizeof(struct udphdr); 829 copied = skb->len - sizeof(struct udphdr);
829 if (copied > len) { 830 if (copied > len) {
830 copied = len; 831 copied = len;
831 msg->msg_flags |= MSG_TRUNC; 832 msg->msg_flags |= MSG_TRUNC;
@@ -868,18 +869,18 @@ try_again:
868 sin->sin_port = skb->h.uh->source; 869 sin->sin_port = skb->h.uh->source;
869 sin->sin_addr.s_addr = skb->nh.iph->saddr; 870 sin->sin_addr.s_addr = skb->nh.iph->saddr;
870 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 871 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
871 } 872 }
872 if (inet->cmsg_flags) 873 if (inet->cmsg_flags)
873 ip_cmsg_recv(msg, skb); 874 ip_cmsg_recv(msg, skb);
874 875
875 err = copied; 876 err = copied;
876 if (flags & MSG_TRUNC) 877 if (flags & MSG_TRUNC)
877 err = skb->len - sizeof(struct udphdr); 878 err = skb->len - sizeof(struct udphdr);
878 879
879out_free: 880out_free:
880 skb_free_datagram(sk, skb); 881 skb_free_datagram(sk, skb);
881out: 882out:
882 return err; 883 return err;
883 884
884csum_copy_err: 885csum_copy_err:
885 UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite); 886 UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
@@ -887,7 +888,7 @@ csum_copy_err:
887 skb_kill_datagram(sk, skb, flags); 888 skb_kill_datagram(sk, skb, flags);
888 889
889 if (noblock) 890 if (noblock)
890 return -EAGAIN; 891 return -EAGAIN;
891 goto try_again; 892 goto try_again;
892} 893}
893 894
@@ -898,7 +899,7 @@ int udp_disconnect(struct sock *sk, int flags)
898 /* 899 /*
899 * 1003.1g - break association. 900 * 1003.1g - break association.
900 */ 901 */
901 902
902 sk->sk_state = TCP_CLOSE; 903 sk->sk_state = TCP_CLOSE;
903 inet->daddr = 0; 904 inet->daddr = 0;
904 inet->dport = 0; 905 inet->dport = 0;
@@ -922,13 +923,13 @@ int udp_disconnect(struct sock *sk, int flags)
922static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb) 923static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
923{ 924{
924#ifndef CONFIG_XFRM 925#ifndef CONFIG_XFRM
925 return 1; 926 return 1;
926#else 927#else
927 struct udp_sock *up = udp_sk(sk); 928 struct udp_sock *up = udp_sk(sk);
928 struct udphdr *uh; 929 struct udphdr *uh;
929 struct iphdr *iph; 930 struct iphdr *iph;
930 int iphlen, len; 931 int iphlen, len;
931 932
932 __u8 *udpdata; 933 __u8 *udpdata;
933 __be32 *udpdata32; 934 __be32 *udpdata32;
934 __u16 encap_type = up->encap_type; 935 __u16 encap_type = up->encap_type;
@@ -971,7 +972,7 @@ static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
971 return 0; 972 return 0;
972 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) && 973 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
973 udpdata32[0] == 0 && udpdata32[1] == 0) { 974 udpdata32[0] == 0 && udpdata32[1] == 0) {
974 975
975 /* ESP Packet with Non-IKE marker */ 976 /* ESP Packet with Non-IKE marker */
976 len = sizeof(struct udphdr) + 2 * sizeof(u32); 977 len = sizeof(struct udphdr) + 2 * sizeof(u32);
977 } else 978 } else
@@ -1187,14 +1188,14 @@ static inline void udp4_csum_init(struct sk_buff *skb, struct udphdr *uh)
1187} 1188}
1188 1189
1189/* 1190/*
1190 * All we need to do is get the socket, and then do a checksum. 1191 * All we need to do is get the socket, and then do a checksum.
1191 */ 1192 */
1192 1193
1193int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], 1194int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
1194 int is_udplite) 1195 int is_udplite)
1195{ 1196{
1196 struct sock *sk; 1197 struct sock *sk;
1197 struct udphdr *uh = skb->h.uh; 1198 struct udphdr *uh = skb->h.uh;
1198 unsigned short ulen; 1199 unsigned short ulen;
1199 struct rtable *rt = (struct rtable*)skb->dst; 1200 struct rtable *rt = (struct rtable*)skb->dst;
1200 __be32 saddr = skb->nh.iph->saddr; 1201 __be32 saddr = skb->nh.iph->saddr;
@@ -1270,9 +1271,9 @@ short_packet:
1270 goto drop; 1271 goto drop;
1271 1272
1272csum_error: 1273csum_error:
1273 /* 1274 /*
1274 * RFC1122: OK. Discards the bad packet silently (as far as 1275 * RFC1122: OK. Discards the bad packet silently (as far as
1275 * the network is concerned, anyway) as per 4.1.3.4 (MUST). 1276 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1276 */ 1277 */
1277 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n", 1278 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1278 is_udplite? "-Lite" : "", 1279 is_udplite? "-Lite" : "",
@@ -1328,7 +1329,7 @@ int udp_lib_setsockopt(struct sock *sk, int level, int optname,
1328 release_sock(sk); 1329 release_sock(sk);
1329 } 1330 }
1330 break; 1331 break;
1331 1332
1332 case UDP_ENCAP: 1333 case UDP_ENCAP:
1333 switch (val) { 1334 switch (val) {
1334 case 0: 1335 case 0:
@@ -1356,8 +1357,8 @@ int udp_lib_setsockopt(struct sock *sk, int level, int optname,
1356 up->pcflag |= UDPLITE_SEND_CC; 1357 up->pcflag |= UDPLITE_SEND_CC;
1357 break; 1358 break;
1358 1359
1359 /* The receiver specifies a minimum checksum coverage value. To make 1360 /* The receiver specifies a minimum checksum coverage value. To make
1360 * sense, this should be set to at least 8 (as done below). If zero is 1361 * sense, this should be set to at least 8 (as done below). If zero is
1361 * used, this again means full checksum coverage. */ 1362 * used, this again means full checksum coverage. */
1362 case UDPLITE_RECV_CSCOV: 1363 case UDPLITE_RECV_CSCOV:
1363 if (!up->pcflag) /* Disable the option on UDP sockets */ 1364 if (!up->pcflag) /* Disable the option on UDP sockets */
@@ -1406,7 +1407,7 @@ int udp_lib_getsockopt(struct sock *sk, int level, int optname,
1406 return -EFAULT; 1407 return -EFAULT;
1407 1408
1408 len = min_t(unsigned int, len, sizeof(int)); 1409 len = min_t(unsigned int, len, sizeof(int));
1409 1410
1410 if(len < 0) 1411 if(len < 0)
1411 return -EINVAL; 1412 return -EINVAL;
1412 1413
@@ -1433,11 +1434,11 @@ int udp_lib_getsockopt(struct sock *sk, int level, int optname,
1433 return -ENOPROTOOPT; 1434 return -ENOPROTOOPT;
1434 }; 1435 };
1435 1436
1436 if(put_user(len, optlen)) 1437 if(put_user(len, optlen))
1437 return -EFAULT; 1438 return -EFAULT;
1438 if(copy_to_user(optval, &val,len)) 1439 if(copy_to_user(optval, &val,len))
1439 return -EFAULT; 1440 return -EFAULT;
1440 return 0; 1441 return 0;
1441} 1442}
1442 1443
1443int udp_getsockopt(struct sock *sk, int level, int optname, 1444int udp_getsockopt(struct sock *sk, int level, int optname,
@@ -1463,7 +1464,7 @@ int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1463 * @sock - socket 1464 * @sock - socket
1464 * @wait - poll table 1465 * @wait - poll table
1465 * 1466 *
1466 * This is same as datagram poll, except for the special case of 1467 * This is same as datagram poll, except for the special case of
1467 * blocking sockets. If application is using a blocking fd 1468 * blocking sockets. If application is using a blocking fd
1468 * and a packet with checksum error is in the queue; 1469 * and a packet with checksum error is in the queue;
1469 * then it could get return from select indicating data available 1470 * then it could get return from select indicating data available
@@ -1502,11 +1503,11 @@ unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1502 } 1503 }
1503 1504
1504 return mask; 1505 return mask;
1505 1506
1506} 1507}
1507 1508
1508struct proto udp_prot = { 1509struct proto udp_prot = {
1509 .name = "UDP", 1510 .name = "UDP",
1510 .owner = THIS_MODULE, 1511 .owner = THIS_MODULE,
1511 .close = udp_lib_close, 1512 .close = udp_lib_close,
1512 .connect = ip4_datagram_connect, 1513 .connect = ip4_datagram_connect,
@@ -1670,7 +1671,7 @@ static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1670 1671
1671 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X" 1672 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1672 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p", 1673 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1673 bucket, src, srcp, dest, destp, sp->sk_state, 1674 bucket, src, srcp, dest, destp, sp->sk_state,
1674 atomic_read(&sp->sk_wmem_alloc), 1675 atomic_read(&sp->sk_wmem_alloc),
1675 atomic_read(&sp->sk_rmem_alloc), 1676 atomic_read(&sp->sk_rmem_alloc),
1676 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 1677 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),