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.c156
1 files changed, 96 insertions, 60 deletions
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 3f93292b0ad8..6d6142f9c478 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -91,7 +91,6 @@
91#include <linux/errno.h> 91#include <linux/errno.h>
92#include <linux/timer.h> 92#include <linux/timer.h>
93#include <linux/mm.h> 93#include <linux/mm.h>
94#include <linux/config.h>
95#include <linux/inet.h> 94#include <linux/inet.h>
96#include <linux/ipv6.h> 95#include <linux/ipv6.h>
97#include <linux/netdevice.h> 96#include <linux/netdevice.h>
@@ -119,14 +118,33 @@ DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
119struct hlist_head udp_hash[UDP_HTABLE_SIZE]; 118struct hlist_head udp_hash[UDP_HTABLE_SIZE];
120DEFINE_RWLOCK(udp_hash_lock); 119DEFINE_RWLOCK(udp_hash_lock);
121 120
122/* Shared by v4/v6 udp. */ 121static int udp_port_rover;
123int udp_port_rover;
124 122
125static int udp_v4_get_port(struct sock *sk, unsigned short snum) 123static inline int udp_lport_inuse(u16 num)
124{
125 struct sock *sk;
126 struct hlist_node *node;
127
128 sk_for_each(sk, node, &udp_hash[num & (UDP_HTABLE_SIZE - 1)])
129 if (inet_sk(sk)->num == num)
130 return 1;
131 return 0;
132}
133
134/**
135 * udp_get_port - common port lookup for IPv4 and IPv6
136 *
137 * @sk: socket struct in question
138 * @snum: port number to look up
139 * @saddr_comp: AF-dependent comparison of bound local IP addresses
140 */
141int udp_get_port(struct sock *sk, unsigned short snum,
142 int (*saddr_cmp)(const struct sock *sk1, const struct sock *sk2))
126{ 143{
127 struct hlist_node *node; 144 struct hlist_node *node;
145 struct hlist_head *head;
128 struct sock *sk2; 146 struct sock *sk2;
129 struct inet_sock *inet = inet_sk(sk); 147 int error = 1;
130 148
131 write_lock_bh(&udp_hash_lock); 149 write_lock_bh(&udp_hash_lock);
132 if (snum == 0) { 150 if (snum == 0) {
@@ -138,11 +156,10 @@ static int udp_v4_get_port(struct sock *sk, unsigned short snum)
138 best_size_so_far = 32767; 156 best_size_so_far = 32767;
139 best = result = udp_port_rover; 157 best = result = udp_port_rover;
140 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) { 158 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
141 struct hlist_head *list;
142 int size; 159 int size;
143 160
144 list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)]; 161 head = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
145 if (hlist_empty(list)) { 162 if (hlist_empty(head)) {
146 if (result > sysctl_local_port_range[1]) 163 if (result > sysctl_local_port_range[1])
147 result = sysctl_local_port_range[0] + 164 result = sysctl_local_port_range[0] +
148 ((result - sysctl_local_port_range[0]) & 165 ((result - sysctl_local_port_range[0]) &
@@ -150,12 +167,11 @@ static int udp_v4_get_port(struct sock *sk, unsigned short snum)
150 goto gotit; 167 goto gotit;
151 } 168 }
152 size = 0; 169 size = 0;
153 sk_for_each(sk2, node, list) 170 sk_for_each(sk2, node, head)
154 if (++size >= best_size_so_far) 171 if (++size < best_size_so_far) {
155 goto next; 172 best_size_so_far = size;
156 best_size_so_far = size; 173 best = result;
157 best = result; 174 }
158 next:;
159 } 175 }
160 result = best; 176 result = best;
161 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) { 177 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
@@ -171,38 +187,44 @@ static int udp_v4_get_port(struct sock *sk, unsigned short snum)
171gotit: 187gotit:
172 udp_port_rover = snum = result; 188 udp_port_rover = snum = result;
173 } else { 189 } else {
174 sk_for_each(sk2, node, 190 head = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
175 &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) { 191
176 struct inet_sock *inet2 = inet_sk(sk2); 192 sk_for_each(sk2, node, head)
177 193 if (inet_sk(sk2)->num == snum &&
178 if (inet2->num == snum && 194 sk2 != sk &&
179 sk2 != sk && 195 (!sk2->sk_reuse || !sk->sk_reuse) &&
180 !ipv6_only_sock(sk2) && 196 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
181 (!sk2->sk_bound_dev_if || 197 || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
182 !sk->sk_bound_dev_if || 198 (*saddr_cmp)(sk, sk2) )
183 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
184 (!inet2->rcv_saddr ||
185 !inet->rcv_saddr ||
186 inet2->rcv_saddr == inet->rcv_saddr) &&
187 (!sk2->sk_reuse || !sk->sk_reuse))
188 goto fail; 199 goto fail;
189 }
190 } 200 }
191 inet->num = snum; 201 inet_sk(sk)->num = snum;
192 if (sk_unhashed(sk)) { 202 if (sk_unhashed(sk)) {
193 struct hlist_head *h = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]; 203 head = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
194 204 sk_add_node(sk, head);
195 sk_add_node(sk, h);
196 sock_prot_inc_use(sk->sk_prot); 205 sock_prot_inc_use(sk->sk_prot);
197 } 206 }
198 write_unlock_bh(&udp_hash_lock); 207 error = 0;
199 return 0;
200
201fail: 208fail:
202 write_unlock_bh(&udp_hash_lock); 209 write_unlock_bh(&udp_hash_lock);
203 return 1; 210 return error;
211}
212
213static inline int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
214{
215 struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
216
217 return ( !ipv6_only_sock(sk2) &&
218 (!inet1->rcv_saddr || !inet2->rcv_saddr ||
219 inet1->rcv_saddr == inet2->rcv_saddr ));
204} 220}
205 221
222static inline int udp_v4_get_port(struct sock *sk, unsigned short snum)
223{
224 return udp_get_port(sk, snum, ipv4_rcv_saddr_equal);
225}
226
227
206static void udp_v4_hash(struct sock *sk) 228static void udp_v4_hash(struct sock *sk)
207{ 229{
208 BUG(); 230 BUG();
@@ -221,8 +243,8 @@ static void udp_v4_unhash(struct sock *sk)
221/* UDP is nearly always wildcards out the wazoo, it makes no sense to try 243/* UDP is nearly always wildcards out the wazoo, it makes no sense to try
222 * harder than this. -DaveM 244 * harder than this. -DaveM
223 */ 245 */
224static struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport, 246static struct sock *udp_v4_lookup_longway(__be32 saddr, __be16 sport,
225 u32 daddr, u16 dport, int dif) 247 __be32 daddr, __be16 dport, int dif)
226{ 248{
227 struct sock *sk, *result = NULL; 249 struct sock *sk, *result = NULL;
228 struct hlist_node *node; 250 struct hlist_node *node;
@@ -266,8 +288,8 @@ static struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport,
266 return result; 288 return result;
267} 289}
268 290
269static __inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport, 291static __inline__ struct sock *udp_v4_lookup(__be32 saddr, __be16 sport,
270 u32 daddr, u16 dport, int dif) 292 __be32 daddr, __be16 dport, int dif)
271{ 293{
272 struct sock *sk; 294 struct sock *sk;
273 295
@@ -280,8 +302,8 @@ static __inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport,
280} 302}
281 303
282static inline struct sock *udp_v4_mcast_next(struct sock *sk, 304static inline struct sock *udp_v4_mcast_next(struct sock *sk,
283 u16 loc_port, u32 loc_addr, 305 __be16 loc_port, __be32 loc_addr,
284 u16 rmt_port, u32 rmt_addr, 306 __be16 rmt_port, __be32 rmt_addr,
285 int dif) 307 int dif)
286{ 308{
287 struct hlist_node *node; 309 struct hlist_node *node;
@@ -430,7 +452,7 @@ static int udp_push_pending_frames(struct sock *sk, struct udp_sock *up)
430 /* 452 /*
431 * Only one fragment on the socket. 453 * Only one fragment on the socket.
432 */ 454 */
433 if (skb->ip_summed == CHECKSUM_HW) { 455 if (skb->ip_summed == CHECKSUM_PARTIAL) {
434 skb->csum = offsetof(struct udphdr, check); 456 skb->csum = offsetof(struct udphdr, check);
435 uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, 457 uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
436 up->len, IPPROTO_UDP, 0); 458 up->len, IPPROTO_UDP, 0);
@@ -449,7 +471,7 @@ static int udp_push_pending_frames(struct sock *sk, struct udp_sock *up)
449 * fragments on the socket so that all csums of sk_buffs 471 * fragments on the socket so that all csums of sk_buffs
450 * should be together. 472 * should be together.
451 */ 473 */
452 if (skb->ip_summed == CHECKSUM_HW) { 474 if (skb->ip_summed == CHECKSUM_PARTIAL) {
453 int offset = (unsigned char *)uh - skb->data; 475 int offset = (unsigned char *)uh - skb->data;
454 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 476 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
455 477
@@ -476,7 +498,7 @@ out:
476} 498}
477 499
478 500
479static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base) 501static unsigned short udp_check(struct udphdr *uh, int len, __be32 saddr, __be32 daddr, unsigned long base)
480{ 502{
481 return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base)); 503 return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
482} 504}
@@ -491,8 +513,8 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
491 struct rtable *rt = NULL; 513 struct rtable *rt = NULL;
492 int free = 0; 514 int free = 0;
493 int connected = 0; 515 int connected = 0;
494 u32 daddr, faddr, saddr; 516 __be32 daddr, faddr, saddr;
495 u16 dport; 517 __be16 dport;
496 u8 tos; 518 u8 tos;
497 int err; 519 int err;
498 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 520 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
@@ -604,6 +626,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
604 .uli_u = { .ports = 626 .uli_u = { .ports =
605 { .sport = inet->sport, 627 { .sport = inet->sport,
606 .dport = dport } } }; 628 .dport = dport } } };
629 security_sk_classify_flow(sk, &fl);
607 err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT)); 630 err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
608 if (err) 631 if (err)
609 goto out; 632 goto out;
@@ -662,6 +685,16 @@ out:
662 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS); 685 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
663 return len; 686 return len;
664 } 687 }
688 /*
689 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
690 * ENOBUFS might not be good (it's not tunable per se), but otherwise
691 * we don't have a good statistic (IpOutDiscards but it can be too many
692 * things). We could add another new stat but at least for now that
693 * seems like overkill.
694 */
695 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
696 UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS);
697 }
665 return err; 698 return err;
666 699
667do_confirm: 700do_confirm:
@@ -898,7 +931,7 @@ static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
898 int iphlen, len; 931 int iphlen, len;
899 932
900 __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr); 933 __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
901 __u32 *udpdata32 = (__u32 *)udpdata; 934 __be32 *udpdata32 = (__be32 *)udpdata;
902 __u16 encap_type = up->encap_type; 935 __u16 encap_type = up->encap_type;
903 936
904 /* if we're overly short, let UDP handle it */ 937 /* if we're overly short, let UDP handle it */
@@ -981,6 +1014,7 @@ static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
981static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 1014static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
982{ 1015{
983 struct udp_sock *up = udp_sk(sk); 1016 struct udp_sock *up = udp_sk(sk);
1017 int rc;
984 1018
985 /* 1019 /*
986 * Charge it to the socket, dropping if the queue is full. 1020 * Charge it to the socket, dropping if the queue is full.
@@ -1027,7 +1061,10 @@ static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1027 skb->ip_summed = CHECKSUM_UNNECESSARY; 1061 skb->ip_summed = CHECKSUM_UNNECESSARY;
1028 } 1062 }
1029 1063
1030 if (sock_queue_rcv_skb(sk,skb)<0) { 1064 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
1065 /* Note that an ENOMEM error is charged twice */
1066 if (rc == -ENOMEM)
1067 UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS);
1031 UDP_INC_STATS_BH(UDP_MIB_INERRORS); 1068 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1032 kfree_skb(skb); 1069 kfree_skb(skb);
1033 return -1; 1070 return -1;
@@ -1043,7 +1080,7 @@ static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1043 * so we don't need to lock the hashes. 1080 * so we don't need to lock the hashes.
1044 */ 1081 */
1045static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh, 1082static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
1046 u32 saddr, u32 daddr) 1083 __be32 saddr, __be32 daddr)
1047{ 1084{
1048 struct sock *sk; 1085 struct sock *sk;
1049 int dif; 1086 int dif;
@@ -1084,11 +1121,11 @@ static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
1084 * including udp header and folding it to skb->csum. 1121 * including udp header and folding it to skb->csum.
1085 */ 1122 */
1086static void udp_checksum_init(struct sk_buff *skb, struct udphdr *uh, 1123static void udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
1087 unsigned short ulen, u32 saddr, u32 daddr) 1124 unsigned short ulen, __be32 saddr, __be32 daddr)
1088{ 1125{
1089 if (uh->check == 0) { 1126 if (uh->check == 0) {
1090 skb->ip_summed = CHECKSUM_UNNECESSARY; 1127 skb->ip_summed = CHECKSUM_UNNECESSARY;
1091 } else if (skb->ip_summed == CHECKSUM_HW) { 1128 } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
1092 if (!udp_check(uh, ulen, saddr, daddr, skb->csum)) 1129 if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
1093 skb->ip_summed = CHECKSUM_UNNECESSARY; 1130 skb->ip_summed = CHECKSUM_UNNECESSARY;
1094 } 1131 }
@@ -1109,8 +1146,8 @@ int udp_rcv(struct sk_buff *skb)
1109 struct udphdr *uh; 1146 struct udphdr *uh;
1110 unsigned short ulen; 1147 unsigned short ulen;
1111 struct rtable *rt = (struct rtable*)skb->dst; 1148 struct rtable *rt = (struct rtable*)skb->dst;
1112 u32 saddr = skb->nh.iph->saddr; 1149 __be32 saddr = skb->nh.iph->saddr;
1113 u32 daddr = skb->nh.iph->daddr; 1150 __be32 daddr = skb->nh.iph->daddr;
1114 int len = skb->len; 1151 int len = skb->len;
1115 1152
1116 /* 1153 /*
@@ -1469,11 +1506,10 @@ static int udp_seq_open(struct inode *inode, struct file *file)
1469 struct udp_seq_afinfo *afinfo = PDE(inode)->data; 1506 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1470 struct seq_file *seq; 1507 struct seq_file *seq;
1471 int rc = -ENOMEM; 1508 int rc = -ENOMEM;
1472 struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL); 1509 struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
1473 1510
1474 if (!s) 1511 if (!s)
1475 goto out; 1512 goto out;
1476 memset(s, 0, sizeof(*s));
1477 s->family = afinfo->family; 1513 s->family = afinfo->family;
1478 s->seq_ops.start = udp_seq_start; 1514 s->seq_ops.start = udp_seq_start;
1479 s->seq_ops.next = udp_seq_next; 1515 s->seq_ops.next = udp_seq_next;
@@ -1527,8 +1563,8 @@ void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1527static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket) 1563static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1528{ 1564{
1529 struct inet_sock *inet = inet_sk(sp); 1565 struct inet_sock *inet = inet_sk(sp);
1530 unsigned int dest = inet->daddr; 1566 __be32 dest = inet->daddr;
1531 unsigned int src = inet->rcv_saddr; 1567 __be32 src = inet->rcv_saddr;
1532 __u16 destp = ntohs(inet->dport); 1568 __u16 destp = ntohs(inet->dport);
1533 __u16 srcp = ntohs(inet->sport); 1569 __u16 srcp = ntohs(inet->sport);
1534 1570
@@ -1583,7 +1619,7 @@ EXPORT_SYMBOL(udp_disconnect);
1583EXPORT_SYMBOL(udp_hash); 1619EXPORT_SYMBOL(udp_hash);
1584EXPORT_SYMBOL(udp_hash_lock); 1620EXPORT_SYMBOL(udp_hash_lock);
1585EXPORT_SYMBOL(udp_ioctl); 1621EXPORT_SYMBOL(udp_ioctl);
1586EXPORT_SYMBOL(udp_port_rover); 1622EXPORT_SYMBOL(udp_get_port);
1587EXPORT_SYMBOL(udp_prot); 1623EXPORT_SYMBOL(udp_prot);
1588EXPORT_SYMBOL(udp_sendmsg); 1624EXPORT_SYMBOL(udp_sendmsg);
1589EXPORT_SYMBOL(udp_poll); 1625EXPORT_SYMBOL(udp_poll);