79 |
79 |
80 #include <asm/system.h> |
80 #include <asm/system.h> |
81 #include <asm/uaccess.h> |
81 #include <asm/uaccess.h> |
82 #include <asm/ioctls.h> |
82 #include <asm/ioctls.h> |
83 #include <linux/bootmem.h> |
83 #include <linux/bootmem.h> |
|
84 #include <linux/highmem.h> |
|
85 #include <linux/swap.h> |
84 #include <linux/types.h> |
86 #include <linux/types.h> |
85 #include <linux/fcntl.h> |
87 #include <linux/fcntl.h> |
86 #include <linux/module.h> |
88 #include <linux/module.h> |
87 #include <linux/socket.h> |
89 #include <linux/socket.h> |
88 #include <linux/sockios.h> |
90 #include <linux/sockios.h> |
120 EXPORT_SYMBOL(sysctl_udp_wmem_min); |
118 EXPORT_SYMBOL(sysctl_udp_wmem_min); |
121 |
119 |
122 atomic_t udp_memory_allocated; |
120 atomic_t udp_memory_allocated; |
123 EXPORT_SYMBOL(udp_memory_allocated); |
121 EXPORT_SYMBOL(udp_memory_allocated); |
124 |
122 |
|
123 #define PORTS_PER_CHAIN (65536 / UDP_HTABLE_SIZE) |
|
124 |
125 static int udp_lib_lport_inuse(struct net *net, __u16 num, |
125 static int udp_lib_lport_inuse(struct net *net, __u16 num, |
126 const struct hlist_head udptable[], |
126 const struct udp_hslot *hslot, |
|
127 unsigned long *bitmap, |
127 struct sock *sk, |
128 struct sock *sk, |
128 int (*saddr_comp)(const struct sock *sk1, |
129 int (*saddr_comp)(const struct sock *sk1, |
129 const struct sock *sk2)) |
130 const struct sock *sk2)) |
130 { |
131 { |
131 struct sock *sk2; |
132 struct sock *sk2; |
132 struct hlist_node *node; |
133 struct hlist_nulls_node *node; |
133 |
134 |
134 sk_for_each(sk2, node, &udptable[udp_hashfn(net, num)]) |
135 sk_nulls_for_each(sk2, node, &hslot->head) |
135 if (net_eq(sock_net(sk2), net) && |
136 if (net_eq(sock_net(sk2), net) && |
136 sk2 != sk && |
137 sk2 != sk && |
137 sk2->sk_hash == num && |
138 (bitmap || sk2->sk_hash == num) && |
138 (!sk2->sk_reuse || !sk->sk_reuse) && |
139 (!sk2->sk_reuse || !sk->sk_reuse) && |
139 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if |
140 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if |
140 || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
141 || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
141 (*saddr_comp)(sk, sk2)) |
142 (*saddr_comp)(sk, sk2)) { |
142 return 1; |
143 if (bitmap) |
|
144 __set_bit(sk2->sk_hash / UDP_HTABLE_SIZE, |
|
145 bitmap); |
|
146 else |
|
147 return 1; |
|
148 } |
143 return 0; |
149 return 0; |
144 } |
150 } |
145 |
151 |
146 /** |
152 /** |
147 * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 |
153 * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 |
152 */ |
158 */ |
153 int udp_lib_get_port(struct sock *sk, unsigned short snum, |
159 int udp_lib_get_port(struct sock *sk, unsigned short snum, |
154 int (*saddr_comp)(const struct sock *sk1, |
160 int (*saddr_comp)(const struct sock *sk1, |
155 const struct sock *sk2 ) ) |
161 const struct sock *sk2 ) ) |
156 { |
162 { |
157 struct hlist_head *udptable = sk->sk_prot->h.udp_hash; |
163 struct udp_hslot *hslot; |
|
164 struct udp_table *udptable = sk->sk_prot->h.udp_table; |
158 int error = 1; |
165 int error = 1; |
159 struct net *net = sock_net(sk); |
166 struct net *net = sock_net(sk); |
160 |
|
161 write_lock_bh(&udp_hash_lock); |
|
162 |
167 |
163 if (!snum) { |
168 if (!snum) { |
164 int low, high, remaining; |
169 int low, high, remaining; |
165 unsigned rand; |
170 unsigned rand; |
166 unsigned short first; |
171 unsigned short first, last; |
|
172 DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); |
167 |
173 |
168 inet_get_local_port_range(&low, &high); |
174 inet_get_local_port_range(&low, &high); |
169 remaining = (high - low) + 1; |
175 remaining = (high - low) + 1; |
170 |
176 |
171 rand = net_random(); |
177 rand = net_random(); |
172 snum = first = rand % remaining + low; |
178 first = (((u64)rand * remaining) >> 32) + low; |
173 rand |= 1; |
179 /* |
174 while (udp_lib_lport_inuse(net, snum, udptable, sk, |
180 * force rand to be an odd multiple of UDP_HTABLE_SIZE |
175 saddr_comp)) { |
181 */ |
|
182 rand = (rand | 1) * UDP_HTABLE_SIZE; |
|
183 for (last = first + UDP_HTABLE_SIZE; first != last; first++) { |
|
184 hslot = &udptable->hash[udp_hashfn(net, first)]; |
|
185 bitmap_zero(bitmap, PORTS_PER_CHAIN); |
|
186 spin_lock_bh(&hslot->lock); |
|
187 udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, |
|
188 saddr_comp); |
|
189 |
|
190 snum = first; |
|
191 /* |
|
192 * Iterate on all possible values of snum for this hash. |
|
193 * Using steps of an odd multiple of UDP_HTABLE_SIZE |
|
194 * give us randomization and full range coverage. |
|
195 */ |
176 do { |
196 do { |
177 snum = snum + rand; |
197 if (low <= snum && snum <= high && |
178 } while (snum < low || snum > high); |
198 !test_bit(snum / UDP_HTABLE_SIZE, bitmap)) |
179 if (snum == first) |
199 goto found; |
180 goto fail; |
200 snum += rand; |
181 } |
201 } while (snum != first); |
182 } else if (udp_lib_lport_inuse(net, snum, udptable, sk, saddr_comp)) |
202 spin_unlock_bh(&hslot->lock); |
|
203 } |
183 goto fail; |
204 goto fail; |
184 |
205 } else { |
|
206 hslot = &udptable->hash[udp_hashfn(net, snum)]; |
|
207 spin_lock_bh(&hslot->lock); |
|
208 if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, saddr_comp)) |
|
209 goto fail_unlock; |
|
210 } |
|
211 found: |
185 inet_sk(sk)->num = snum; |
212 inet_sk(sk)->num = snum; |
186 sk->sk_hash = snum; |
213 sk->sk_hash = snum; |
187 if (sk_unhashed(sk)) { |
214 if (sk_unhashed(sk)) { |
188 sk_add_node(sk, &udptable[udp_hashfn(net, snum)]); |
215 sk_nulls_add_node_rcu(sk, &hslot->head); |
189 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
216 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
190 } |
217 } |
191 error = 0; |
218 error = 0; |
|
219 fail_unlock: |
|
220 spin_unlock_bh(&hslot->lock); |
192 fail: |
221 fail: |
193 write_unlock_bh(&udp_hash_lock); |
|
194 return error; |
222 return error; |
195 } |
223 } |
196 |
224 |
197 static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) |
225 static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) |
198 { |
226 { |
204 } |
232 } |
205 |
233 |
206 int udp_v4_get_port(struct sock *sk, unsigned short snum) |
234 int udp_v4_get_port(struct sock *sk, unsigned short snum) |
207 { |
235 { |
208 return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal); |
236 return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal); |
|
237 } |
|
238 |
|
239 static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr, |
|
240 unsigned short hnum, |
|
241 __be16 sport, __be32 daddr, __be16 dport, int dif) |
|
242 { |
|
243 int score = -1; |
|
244 |
|
245 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && |
|
246 !ipv6_only_sock(sk)) { |
|
247 struct inet_sock *inet = inet_sk(sk); |
|
248 |
|
249 score = (sk->sk_family == PF_INET ? 1 : 0); |
|
250 if (inet->rcv_saddr) { |
|
251 if (inet->rcv_saddr != daddr) |
|
252 return -1; |
|
253 score += 2; |
|
254 } |
|
255 if (inet->daddr) { |
|
256 if (inet->daddr != saddr) |
|
257 return -1; |
|
258 score += 2; |
|
259 } |
|
260 if (inet->dport) { |
|
261 if (inet->dport != sport) |
|
262 return -1; |
|
263 score += 2; |
|
264 } |
|
265 if (sk->sk_bound_dev_if) { |
|
266 if (sk->sk_bound_dev_if != dif) |
|
267 return -1; |
|
268 score += 2; |
|
269 } |
|
270 } |
|
271 return score; |
209 } |
272 } |
210 |
273 |
211 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try |
274 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try |
212 * harder than this. -DaveM |
275 * harder than this. -DaveM |
213 */ |
276 */ |
214 static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, |
277 static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, |
215 __be16 sport, __be32 daddr, __be16 dport, |
278 __be16 sport, __be32 daddr, __be16 dport, |
216 int dif, struct hlist_head udptable[]) |
279 int dif, struct udp_table *udptable) |
217 { |
280 { |
218 struct sock *sk, *result = NULL; |
281 struct sock *sk, *result; |
219 struct hlist_node *node; |
282 struct hlist_nulls_node *node; |
220 unsigned short hnum = ntohs(dport); |
283 unsigned short hnum = ntohs(dport); |
221 int badness = -1; |
284 unsigned int hash = udp_hashfn(net, hnum); |
222 |
285 struct udp_hslot *hslot = &udptable->hash[hash]; |
223 read_lock(&udp_hash_lock); |
286 int score, badness; |
224 sk_for_each(sk, node, &udptable[udp_hashfn(net, hnum)]) { |
287 |
225 struct inet_sock *inet = inet_sk(sk); |
288 rcu_read_lock(); |
226 |
289 begin: |
227 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && |
290 result = NULL; |
228 !ipv6_only_sock(sk)) { |
291 badness = -1; |
229 int score = (sk->sk_family == PF_INET ? 1 : 0); |
292 sk_nulls_for_each_rcu(sk, node, &hslot->head) { |
230 if (inet->rcv_saddr) { |
293 score = compute_score(sk, net, saddr, hnum, sport, |
231 if (inet->rcv_saddr != daddr) |
294 daddr, dport, dif); |
232 continue; |
295 if (score > badness) { |
233 score+=2; |
296 result = sk; |
234 } |
297 badness = score; |
235 if (inet->daddr) { |
298 } |
236 if (inet->daddr != saddr) |
299 } |
237 continue; |
300 /* |
238 score+=2; |
301 * if the nulls value we got at the end of this lookup is |
239 } |
302 * not the expected one, we must restart lookup. |
240 if (inet->dport) { |
303 * We probably met an item that was moved to another chain. |
241 if (inet->dport != sport) |
304 */ |
242 continue; |
305 if (get_nulls_value(node) != hash) |
243 score+=2; |
306 goto begin; |
244 } |
307 |
245 if (sk->sk_bound_dev_if) { |
308 if (result) { |
246 if (sk->sk_bound_dev_if != dif) |
309 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt))) |
247 continue; |
310 result = NULL; |
248 score+=2; |
311 else if (unlikely(compute_score(result, net, saddr, hnum, sport, |
249 } |
312 daddr, dport, dif) < badness)) { |
250 if (score == 9) { |
313 sock_put(result); |
251 result = sk; |
314 goto begin; |
252 break; |
315 } |
253 } else if (score > badness) { |
316 } |
254 result = sk; |
317 rcu_read_unlock(); |
255 badness = score; |
|
256 } |
|
257 } |
|
258 } |
|
259 if (result) |
|
260 sock_hold(result); |
|
261 read_unlock(&udp_hash_lock); |
|
262 return result; |
318 return result; |
263 } |
319 } |
264 |
320 |
265 static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, |
321 static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, |
266 __be16 sport, __be16 dport, |
322 __be16 sport, __be16 dport, |
267 struct hlist_head udptable[]) |
323 struct udp_table *udptable) |
268 { |
324 { |
269 struct sock *sk; |
325 struct sock *sk; |
270 const struct iphdr *iph = ip_hdr(skb); |
326 const struct iphdr *iph = ip_hdr(skb); |
271 |
327 |
272 if (unlikely(sk = skb_steal_sock(skb))) |
328 if (unlikely(sk = skb_steal_sock(skb))) |
278 } |
334 } |
279 |
335 |
280 struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, |
336 struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, |
281 __be32 daddr, __be16 dport, int dif) |
337 __be32 daddr, __be16 dport, int dif) |
282 { |
338 { |
283 return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, udp_hash); |
339 return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table); |
284 } |
340 } |
285 EXPORT_SYMBOL_GPL(udp4_lib_lookup); |
341 EXPORT_SYMBOL_GPL(udp4_lib_lookup); |
286 |
342 |
287 static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk, |
343 static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk, |
288 __be16 loc_port, __be32 loc_addr, |
344 __be16 loc_port, __be32 loc_addr, |
289 __be16 rmt_port, __be32 rmt_addr, |
345 __be16 rmt_port, __be32 rmt_addr, |
290 int dif) |
346 int dif) |
291 { |
347 { |
292 struct hlist_node *node; |
348 struct hlist_nulls_node *node; |
293 struct sock *s = sk; |
349 struct sock *s = sk; |
294 unsigned short hnum = ntohs(loc_port); |
350 unsigned short hnum = ntohs(loc_port); |
295 |
351 |
296 sk_for_each_from(s, node) { |
352 sk_nulls_for_each_from(s, node) { |
297 struct inet_sock *inet = inet_sk(s); |
353 struct inet_sock *inet = inet_sk(s); |
298 |
354 |
299 if (!net_eq(sock_net(s), net) || |
355 if (!net_eq(sock_net(s), net) || |
300 s->sk_hash != hnum || |
356 s->sk_hash != hnum || |
301 (inet->daddr && inet->daddr != rmt_addr) || |
357 (inet->daddr && inet->daddr != rmt_addr) || |
322 * on past this. Then (as it used to claim before adjustment) |
378 * on past this. Then (as it used to claim before adjustment) |
323 * header points to the first 8 bytes of the udp header. We need |
379 * header points to the first 8 bytes of the udp header. We need |
324 * to find the appropriate port. |
380 * to find the appropriate port. |
325 */ |
381 */ |
326 |
382 |
327 void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[]) |
383 void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) |
328 { |
384 { |
329 struct inet_sock *inet; |
385 struct inet_sock *inet; |
330 struct iphdr *iph = (struct iphdr*)skb->data; |
386 struct iphdr *iph = (struct iphdr*)skb->data; |
331 struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); |
387 struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); |
332 const int type = icmp_hdr(skb)->type; |
388 const int type = icmp_hdr(skb)->type; |
684 |
740 |
685 do_append_data: |
741 do_append_data: |
686 up->len += ulen; |
742 up->len += ulen; |
687 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; |
743 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; |
688 err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, |
744 err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, |
689 sizeof(struct udphdr), &ipc, rt, |
745 sizeof(struct udphdr), &ipc, &rt, |
690 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); |
746 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); |
691 if (err) |
747 if (err) |
692 udp_flush_pending_frames(sk); |
748 udp_flush_pending_frames(sk); |
693 else if (!corkreq) |
749 else if (!corkreq) |
694 err = udp_push_pending_frames(sk); |
750 err = udp_push_pending_frames(sk); |
933 } |
989 } |
934 sk_dst_reset(sk); |
990 sk_dst_reset(sk); |
935 return 0; |
991 return 0; |
936 } |
992 } |
937 |
993 |
|
994 void udp_lib_unhash(struct sock *sk) |
|
995 { |
|
996 if (sk_hashed(sk)) { |
|
997 struct udp_table *udptable = sk->sk_prot->h.udp_table; |
|
998 unsigned int hash = udp_hashfn(sock_net(sk), sk->sk_hash); |
|
999 struct udp_hslot *hslot = &udptable->hash[hash]; |
|
1000 |
|
1001 spin_lock_bh(&hslot->lock); |
|
1002 if (sk_nulls_del_node_init_rcu(sk)) { |
|
1003 inet_sk(sk)->num = 0; |
|
1004 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
|
1005 } |
|
1006 spin_unlock_bh(&hslot->lock); |
|
1007 } |
|
1008 } |
|
1009 EXPORT_SYMBOL(udp_lib_unhash); |
|
1010 |
938 static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
1011 static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
939 { |
1012 { |
940 int is_udplite = IS_UDPLITE(sk); |
1013 int is_udplite = IS_UDPLITE(sk); |
941 int rc; |
1014 int rc; |
942 |
1015 |
943 if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) { |
1016 if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) { |
944 /* Note that an ENOMEM error is charged twice */ |
1017 /* Note that an ENOMEM error is charged twice */ |
945 if (rc == -ENOMEM) |
1018 if (rc == -ENOMEM) { |
946 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, |
1019 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, |
947 is_udplite); |
1020 is_udplite); |
|
1021 atomic_inc(&sk->sk_drops); |
|
1022 } |
948 goto drop; |
1023 goto drop; |
949 } |
1024 } |
950 |
1025 |
951 return 0; |
1026 return 0; |
952 |
1027 |
1071 * so we don't need to lock the hashes. |
1146 * so we don't need to lock the hashes. |
1072 */ |
1147 */ |
1073 static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, |
1148 static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, |
1074 struct udphdr *uh, |
1149 struct udphdr *uh, |
1075 __be32 saddr, __be32 daddr, |
1150 __be32 saddr, __be32 daddr, |
1076 struct hlist_head udptable[]) |
1151 struct udp_table *udptable) |
1077 { |
1152 { |
1078 struct sock *sk; |
1153 struct sock *sk; |
|
1154 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))]; |
1079 int dif; |
1155 int dif; |
1080 |
1156 |
1081 read_lock(&udp_hash_lock); |
1157 spin_lock(&hslot->lock); |
1082 sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]); |
1158 sk = sk_nulls_head(&hslot->head); |
1083 dif = skb->dev->ifindex; |
1159 dif = skb->dev->ifindex; |
1084 sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); |
1160 sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); |
1085 if (sk) { |
1161 if (sk) { |
1086 struct sock *sknext = NULL; |
1162 struct sock *sknext = NULL; |
1087 |
1163 |
1088 do { |
1164 do { |
1089 struct sk_buff *skb1 = skb; |
1165 struct sk_buff *skb1 = skb; |
1090 |
1166 |
1091 sknext = udp_v4_mcast_next(net, sk_next(sk), uh->dest, |
1167 sknext = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest, |
1092 daddr, uh->source, saddr, |
1168 daddr, uh->source, saddr, |
1093 dif); |
1169 dif); |
1094 if (sknext) |
1170 if (sknext) |
1095 skb1 = skb_clone(skb, GFP_ATOMIC); |
1171 skb1 = skb_clone(skb, GFP_ATOMIC); |
1096 |
1172 |
1149 |
1225 |
1150 /* |
1226 /* |
1151 * All we need to do is get the socket, and then do a checksum. |
1227 * All we need to do is get the socket, and then do a checksum. |
1152 */ |
1228 */ |
1153 |
1229 |
1154 int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], |
1230 int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, |
1155 int proto) |
1231 int proto) |
1156 { |
1232 { |
1157 struct sock *sk; |
1233 struct sock *sk; |
1158 struct udphdr *uh = udp_hdr(skb); |
1234 struct udphdr *uh; |
1159 unsigned short ulen; |
1235 unsigned short ulen; |
1160 struct rtable *rt = (struct rtable*)skb->dst; |
1236 struct rtable *rt = (struct rtable*)skb->dst; |
1161 __be32 saddr = ip_hdr(skb)->saddr; |
1237 __be32 saddr, daddr; |
1162 __be32 daddr = ip_hdr(skb)->daddr; |
|
1163 struct net *net = dev_net(skb->dev); |
1238 struct net *net = dev_net(skb->dev); |
1164 |
1239 |
1165 /* |
1240 /* |
1166 * Validate the packet. |
1241 * Validate the packet. |
1167 */ |
1242 */ |
1168 if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
1243 if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
1169 goto drop; /* No space for header. */ |
1244 goto drop; /* No space for header. */ |
1170 |
1245 |
|
1246 uh = udp_hdr(skb); |
1171 ulen = ntohs(uh->len); |
1247 ulen = ntohs(uh->len); |
1172 if (ulen > skb->len) |
1248 if (ulen > skb->len) |
1173 goto short_packet; |
1249 goto short_packet; |
1174 |
1250 |
1175 if (proto == IPPROTO_UDP) { |
1251 if (proto == IPPROTO_UDP) { |
1217 */ |
1296 */ |
1218 kfree_skb(skb); |
1297 kfree_skb(skb); |
1219 return 0; |
1298 return 0; |
1220 |
1299 |
1221 short_packet: |
1300 short_packet: |
1222 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From " NIPQUAD_FMT ":%u %d/%d to " NIPQUAD_FMT ":%u\n", |
1301 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", |
1223 proto == IPPROTO_UDPLITE ? "-Lite" : "", |
1302 proto == IPPROTO_UDPLITE ? "-Lite" : "", |
1224 NIPQUAD(saddr), |
1303 &saddr, |
1225 ntohs(uh->source), |
1304 ntohs(uh->source), |
1226 ulen, |
1305 ulen, |
1227 skb->len, |
1306 skb->len, |
1228 NIPQUAD(daddr), |
1307 &daddr, |
1229 ntohs(uh->dest)); |
1308 ntohs(uh->dest)); |
1230 goto drop; |
1309 goto drop; |
1231 |
1310 |
1232 csum_error: |
1311 csum_error: |
1233 /* |
1312 /* |
1234 * RFC1122: OK. Discards the bad packet silently (as far as |
1313 * RFC1122: OK. Discards the bad packet silently (as far as |
1235 * the network is concerned, anyway) as per 4.1.3.4 (MUST). |
1314 * the network is concerned, anyway) as per 4.1.3.4 (MUST). |
1236 */ |
1315 */ |
1237 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From " NIPQUAD_FMT ":%u to " NIPQUAD_FMT ":%u ulen %d\n", |
1316 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", |
1238 proto == IPPROTO_UDPLITE ? "-Lite" : "", |
1317 proto == IPPROTO_UDPLITE ? "-Lite" : "", |
1239 NIPQUAD(saddr), |
1318 &saddr, |
1240 ntohs(uh->source), |
1319 ntohs(uh->source), |
1241 NIPQUAD(daddr), |
1320 &daddr, |
1242 ntohs(uh->dest), |
1321 ntohs(uh->dest), |
1243 ulen); |
1322 ulen); |
1244 drop: |
1323 drop: |
1245 UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); |
1324 UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); |
1246 kfree_skb(skb); |
1325 kfree_skb(skb); |
1247 return 0; |
1326 return 0; |
1248 } |
1327 } |
1249 |
1328 |
1250 int udp_rcv(struct sk_buff *skb) |
1329 int udp_rcv(struct sk_buff *skb) |
1251 { |
1330 { |
1252 return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP); |
1331 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); |
1253 } |
1332 } |
1254 |
1333 |
1255 void udp_destroy_sock(struct sock *sk) |
1334 void udp_destroy_sock(struct sock *sk) |
1256 { |
1335 { |
1257 lock_sock(sk); |
1336 lock_sock(sk); |
1489 .memory_allocated = &udp_memory_allocated, |
1568 .memory_allocated = &udp_memory_allocated, |
1490 .sysctl_mem = sysctl_udp_mem, |
1569 .sysctl_mem = sysctl_udp_mem, |
1491 .sysctl_wmem = &sysctl_udp_wmem_min, |
1570 .sysctl_wmem = &sysctl_udp_wmem_min, |
1492 .sysctl_rmem = &sysctl_udp_rmem_min, |
1571 .sysctl_rmem = &sysctl_udp_rmem_min, |
1493 .obj_size = sizeof(struct udp_sock), |
1572 .obj_size = sizeof(struct udp_sock), |
1494 .h.udp_hash = udp_hash, |
1573 .slab_flags = SLAB_DESTROY_BY_RCU, |
|
1574 .h.udp_table = &udp_table, |
1495 #ifdef CONFIG_COMPAT |
1575 #ifdef CONFIG_COMPAT |
1496 .compat_setsockopt = compat_udp_setsockopt, |
1576 .compat_setsockopt = compat_udp_setsockopt, |
1497 .compat_getsockopt = compat_udp_getsockopt, |
1577 .compat_getsockopt = compat_udp_getsockopt, |
1498 #endif |
1578 #endif |
1499 }; |
1579 }; |
1500 |
1580 |
1501 /* ------------------------------------------------------------------------ */ |
1581 /* ------------------------------------------------------------------------ */ |
1502 #ifdef CONFIG_PROC_FS |
1582 #ifdef CONFIG_PROC_FS |
1503 |
1583 |
1504 static struct sock *udp_get_first(struct seq_file *seq) |
1584 static struct sock *udp_get_first(struct seq_file *seq, int start) |
1505 { |
1585 { |
1506 struct sock *sk; |
1586 struct sock *sk; |
1507 struct udp_iter_state *state = seq->private; |
1587 struct udp_iter_state *state = seq->private; |
1508 struct net *net = seq_file_net(seq); |
1588 struct net *net = seq_file_net(seq); |
1509 |
1589 |
1510 for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { |
1590 for (state->bucket = start; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { |
1511 struct hlist_node *node; |
1591 struct hlist_nulls_node *node; |
1512 sk_for_each(sk, node, state->hashtable + state->bucket) { |
1592 struct udp_hslot *hslot = &state->udp_table->hash[state->bucket]; |
|
1593 spin_lock_bh(&hslot->lock); |
|
1594 sk_nulls_for_each(sk, node, &hslot->head) { |
1513 if (!net_eq(sock_net(sk), net)) |
1595 if (!net_eq(sock_net(sk), net)) |
1514 continue; |
1596 continue; |
1515 if (sk->sk_family == state->family) |
1597 if (sk->sk_family == state->family) |
1516 goto found; |
1598 goto found; |
1517 } |
1599 } |
|
1600 spin_unlock_bh(&hslot->lock); |
1518 } |
1601 } |
1519 sk = NULL; |
1602 sk = NULL; |
1520 found: |
1603 found: |
1521 return sk; |
1604 return sk; |
1522 } |
1605 } |
1525 { |
1608 { |
1526 struct udp_iter_state *state = seq->private; |
1609 struct udp_iter_state *state = seq->private; |
1527 struct net *net = seq_file_net(seq); |
1610 struct net *net = seq_file_net(seq); |
1528 |
1611 |
1529 do { |
1612 do { |
1530 sk = sk_next(sk); |
1613 sk = sk_nulls_next(sk); |
1531 try_again: |
|
1532 ; |
|
1533 } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); |
1614 } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); |
1534 |
1615 |
1535 if (!sk && ++state->bucket < UDP_HTABLE_SIZE) { |
1616 if (!sk) { |
1536 sk = sk_head(state->hashtable + state->bucket); |
1617 spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); |
1537 goto try_again; |
1618 return udp_get_first(seq, state->bucket + 1); |
1538 } |
1619 } |
1539 return sk; |
1620 return sk; |
1540 } |
1621 } |
1541 |
1622 |
1542 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) |
1623 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) |
1543 { |
1624 { |
1544 struct sock *sk = udp_get_first(seq); |
1625 struct sock *sk = udp_get_first(seq, 0); |
1545 |
1626 |
1546 if (sk) |
1627 if (sk) |
1547 while (pos && (sk = udp_get_next(seq, sk)) != NULL) |
1628 while (pos && (sk = udp_get_next(seq, sk)) != NULL) |
1548 --pos; |
1629 --pos; |
1549 return pos ? NULL : sk; |
1630 return pos ? NULL : sk; |
1550 } |
1631 } |
1551 |
1632 |
1552 static void *udp_seq_start(struct seq_file *seq, loff_t *pos) |
1633 static void *udp_seq_start(struct seq_file *seq, loff_t *pos) |
1553 __acquires(udp_hash_lock) |
1634 { |
1554 { |
|
1555 read_lock(&udp_hash_lock); |
|
1556 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; |
1635 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; |
1557 } |
1636 } |
1558 |
1637 |
1559 static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
1638 static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
1560 { |
1639 { |
1693 { |
1774 { |
1694 unregister_pernet_subsys(&udp4_net_ops); |
1775 unregister_pernet_subsys(&udp4_net_ops); |
1695 } |
1776 } |
1696 #endif /* CONFIG_PROC_FS */ |
1777 #endif /* CONFIG_PROC_FS */ |
1697 |
1778 |
|
1779 void __init udp_table_init(struct udp_table *table) |
|
1780 { |
|
1781 int i; |
|
1782 |
|
1783 for (i = 0; i < UDP_HTABLE_SIZE; i++) { |
|
1784 INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i); |
|
1785 spin_lock_init(&table->hash[i].lock); |
|
1786 } |
|
1787 } |
|
1788 |
1698 void __init udp_init(void) |
1789 void __init udp_init(void) |
1699 { |
1790 { |
1700 unsigned long limit; |
1791 unsigned long nr_pages, limit; |
1701 |
1792 |
|
1793 udp_table_init(&udp_table); |
1702 /* Set the pressure threshold up by the same strategy of TCP. It is a |
1794 /* Set the pressure threshold up by the same strategy of TCP. It is a |
1703 * fraction of global memory that is up to 1/2 at 256 MB, decreasing |
1795 * fraction of global memory that is up to 1/2 at 256 MB, decreasing |
1704 * toward zero with the amount of memory, with a floor of 128 pages. |
1796 * toward zero with the amount of memory, with a floor of 128 pages. |
1705 */ |
1797 */ |
1706 limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); |
1798 nr_pages = totalram_pages - totalhigh_pages; |
1707 limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); |
1799 limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); |
|
1800 limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); |
1708 limit = max(limit, 128UL); |
1801 limit = max(limit, 128UL); |
1709 sysctl_udp_mem[0] = limit / 4 * 3; |
1802 sysctl_udp_mem[0] = limit / 4 * 3; |
1710 sysctl_udp_mem[1] = limit; |
1803 sysctl_udp_mem[1] = limit; |
1711 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; |
1804 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; |
1712 |
1805 |
1713 sysctl_udp_rmem_min = SK_MEM_QUANTUM; |
1806 sysctl_udp_rmem_min = SK_MEM_QUANTUM; |
1714 sysctl_udp_wmem_min = SK_MEM_QUANTUM; |
1807 sysctl_udp_wmem_min = SK_MEM_QUANTUM; |
1715 } |
1808 } |
1716 |
1809 |
1717 EXPORT_SYMBOL(udp_disconnect); |
1810 EXPORT_SYMBOL(udp_disconnect); |
1718 EXPORT_SYMBOL(udp_hash); |
|
1719 EXPORT_SYMBOL(udp_hash_lock); |
|
1720 EXPORT_SYMBOL(udp_ioctl); |
1811 EXPORT_SYMBOL(udp_ioctl); |
1721 EXPORT_SYMBOL(udp_prot); |
1812 EXPORT_SYMBOL(udp_prot); |
1722 EXPORT_SYMBOL(udp_sendmsg); |
1813 EXPORT_SYMBOL(udp_sendmsg); |
1723 EXPORT_SYMBOL(udp_lib_getsockopt); |
1814 EXPORT_SYMBOL(udp_lib_getsockopt); |
1724 EXPORT_SYMBOL(udp_lib_setsockopt); |
1815 EXPORT_SYMBOL(udp_lib_setsockopt); |