]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * UDP over IPv6 | |
3 | * Linux INET6 implementation | |
4 | * | |
5 | * Authors: | |
6 | * Pedro Roque <[email protected]> | |
7 | * | |
8 | * Based on linux/ipv4/udp.c | |
9 | * | |
10 | * $Id: udp.c,v 1.65 2002/02/01 22:01:04 davem Exp $ | |
11 | * | |
12 | * Fixes: | |
13 | * Hideaki YOSHIFUJI : sin6_scope_id support | |
14 | * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which | |
15 | * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind | |
16 | * a single port at the same time. | |
17 | * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data | |
18 | * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. | |
19 | * | |
20 | * This program is free software; you can redistribute it and/or | |
21 | * modify it under the terms of the GNU General Public License | |
22 | * as published by the Free Software Foundation; either version | |
23 | * 2 of the License, or (at your option) any later version. | |
24 | */ | |
25 | ||
26 | #include <linux/config.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/types.h> | |
29 | #include <linux/socket.h> | |
30 | #include <linux/sockios.h> | |
31 | #include <linux/sched.h> | |
32 | #include <linux/net.h> | |
33 | #include <linux/in6.h> | |
34 | #include <linux/netdevice.h> | |
35 | #include <linux/if_arp.h> | |
36 | #include <linux/ipv6.h> | |
37 | #include <linux/icmpv6.h> | |
38 | #include <linux/init.h> | |
3305b80c | 39 | #include <linux/skbuff.h> |
1da177e4 LT |
40 | #include <asm/uaccess.h> |
41 | ||
42 | #include <net/sock.h> | |
43 | #include <net/snmp.h> | |
44 | ||
45 | #include <net/ipv6.h> | |
46 | #include <net/ndisc.h> | |
47 | #include <net/protocol.h> | |
48 | #include <net/transp_v6.h> | |
49 | #include <net/ip6_route.h> | |
50 | #include <net/addrconf.h> | |
51 | #include <net/ip.h> | |
52 | #include <net/udp.h> | |
53 | #include <net/raw.h> | |
54 | #include <net/inet_common.h> | |
c752f073 | 55 | #include <net/tcp_states.h> |
1da177e4 LT |
56 | |
57 | #include <net/ip6_checksum.h> | |
58 | #include <net/xfrm.h> | |
59 | ||
60 | #include <linux/proc_fs.h> | |
61 | #include <linux/seq_file.h> | |
62 | ||
ba89966c | 63 | DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly; |
1da177e4 LT |
64 | |
65 | /* Grrr, addr_type already calculated by caller, but I don't want | |
66 | * to add some silly "cookie" argument to this method just for that. | |
67 | */ | |
68 | static int udp_v6_get_port(struct sock *sk, unsigned short snum) | |
69 | { | |
70 | struct sock *sk2; | |
71 | struct hlist_node *node; | |
72 | ||
73 | write_lock_bh(&udp_hash_lock); | |
74 | if (snum == 0) { | |
75 | int best_size_so_far, best, result, i; | |
76 | ||
77 | if (udp_port_rover > sysctl_local_port_range[1] || | |
78 | udp_port_rover < sysctl_local_port_range[0]) | |
79 | udp_port_rover = sysctl_local_port_range[0]; | |
80 | best_size_so_far = 32767; | |
81 | best = result = udp_port_rover; | |
82 | for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) { | |
83 | int size; | |
84 | struct hlist_head *list; | |
85 | ||
86 | list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)]; | |
87 | if (hlist_empty(list)) { | |
88 | if (result > sysctl_local_port_range[1]) | |
89 | result = sysctl_local_port_range[0] + | |
90 | ((result - sysctl_local_port_range[0]) & | |
91 | (UDP_HTABLE_SIZE - 1)); | |
92 | goto gotit; | |
93 | } | |
94 | size = 0; | |
95 | sk_for_each(sk2, node, list) | |
96 | if (++size >= best_size_so_far) | |
97 | goto next; | |
98 | best_size_so_far = size; | |
99 | best = result; | |
100 | next:; | |
101 | } | |
102 | result = best; | |
87bf9c97 | 103 | for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) { |
1da177e4 LT |
104 | if (result > sysctl_local_port_range[1]) |
105 | result = sysctl_local_port_range[0] | |
106 | + ((result - sysctl_local_port_range[0]) & | |
107 | (UDP_HTABLE_SIZE - 1)); | |
108 | if (!udp_lport_inuse(result)) | |
109 | break; | |
110 | } | |
87bf9c97 YH |
111 | if (i >= (1 << 16) / UDP_HTABLE_SIZE) |
112 | goto fail; | |
1da177e4 LT |
113 | gotit: |
114 | udp_port_rover = snum = result; | |
115 | } else { | |
116 | sk_for_each(sk2, node, | |
117 | &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) { | |
118 | if (inet_sk(sk2)->num == snum && | |
119 | sk2 != sk && | |
120 | (!sk2->sk_bound_dev_if || | |
121 | !sk->sk_bound_dev_if || | |
122 | sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && | |
123 | (!sk2->sk_reuse || !sk->sk_reuse) && | |
124 | ipv6_rcv_saddr_equal(sk, sk2)) | |
125 | goto fail; | |
126 | } | |
127 | } | |
128 | ||
129 | inet_sk(sk)->num = snum; | |
130 | if (sk_unhashed(sk)) { | |
131 | sk_add_node(sk, &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]); | |
132 | sock_prot_inc_use(sk->sk_prot); | |
133 | } | |
134 | write_unlock_bh(&udp_hash_lock); | |
135 | return 0; | |
136 | ||
137 | fail: | |
138 | write_unlock_bh(&udp_hash_lock); | |
139 | return 1; | |
140 | } | |
141 | ||
142 | static void udp_v6_hash(struct sock *sk) | |
143 | { | |
144 | BUG(); | |
145 | } | |
146 | ||
147 | static void udp_v6_unhash(struct sock *sk) | |
148 | { | |
149 | write_lock_bh(&udp_hash_lock); | |
150 | if (sk_del_node_init(sk)) { | |
151 | inet_sk(sk)->num = 0; | |
152 | sock_prot_dec_use(sk->sk_prot); | |
153 | } | |
154 | write_unlock_bh(&udp_hash_lock); | |
155 | } | |
156 | ||
157 | static struct sock *udp_v6_lookup(struct in6_addr *saddr, u16 sport, | |
158 | struct in6_addr *daddr, u16 dport, int dif) | |
159 | { | |
160 | struct sock *sk, *result = NULL; | |
161 | struct hlist_node *node; | |
162 | unsigned short hnum = ntohs(dport); | |
163 | int badness = -1; | |
164 | ||
165 | read_lock(&udp_hash_lock); | |
166 | sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) { | |
167 | struct inet_sock *inet = inet_sk(sk); | |
168 | ||
169 | if (inet->num == hnum && sk->sk_family == PF_INET6) { | |
170 | struct ipv6_pinfo *np = inet6_sk(sk); | |
171 | int score = 0; | |
172 | if (inet->dport) { | |
173 | if (inet->dport != sport) | |
174 | continue; | |
175 | score++; | |
176 | } | |
177 | if (!ipv6_addr_any(&np->rcv_saddr)) { | |
178 | if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) | |
179 | continue; | |
180 | score++; | |
181 | } | |
182 | if (!ipv6_addr_any(&np->daddr)) { | |
183 | if (!ipv6_addr_equal(&np->daddr, saddr)) | |
184 | continue; | |
185 | score++; | |
186 | } | |
187 | if (sk->sk_bound_dev_if) { | |
188 | if (sk->sk_bound_dev_if != dif) | |
189 | continue; | |
190 | score++; | |
191 | } | |
192 | if(score == 4) { | |
193 | result = sk; | |
194 | break; | |
195 | } else if(score > badness) { | |
196 | result = sk; | |
197 | badness = score; | |
198 | } | |
199 | } | |
200 | } | |
201 | if (result) | |
202 | sock_hold(result); | |
203 | read_unlock(&udp_hash_lock); | |
204 | return result; | |
205 | } | |
206 | ||
207 | /* | |
208 | * | |
209 | */ | |
210 | ||
211 | static void udpv6_close(struct sock *sk, long timeout) | |
212 | { | |
213 | sk_common_release(sk); | |
214 | } | |
215 | ||
216 | /* | |
217 | * This should be easy, if there is something there we | |
218 | * return it, otherwise we block. | |
219 | */ | |
220 | ||
221 | static int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, | |
222 | struct msghdr *msg, size_t len, | |
223 | int noblock, int flags, int *addr_len) | |
224 | { | |
225 | struct ipv6_pinfo *np = inet6_sk(sk); | |
226 | struct inet_sock *inet = inet_sk(sk); | |
227 | struct sk_buff *skb; | |
228 | size_t copied; | |
229 | int err; | |
230 | ||
231 | if (addr_len) | |
232 | *addr_len=sizeof(struct sockaddr_in6); | |
233 | ||
234 | if (flags & MSG_ERRQUEUE) | |
235 | return ipv6_recv_error(sk, msg, len); | |
236 | ||
237 | try_again: | |
238 | skb = skb_recv_datagram(sk, flags, noblock, &err); | |
239 | if (!skb) | |
240 | goto out; | |
241 | ||
242 | copied = skb->len - sizeof(struct udphdr); | |
243 | if (copied > len) { | |
244 | copied = len; | |
245 | msg->msg_flags |= MSG_TRUNC; | |
246 | } | |
247 | ||
248 | if (skb->ip_summed==CHECKSUM_UNNECESSARY) { | |
249 | err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov, | |
250 | copied); | |
251 | } else if (msg->msg_flags&MSG_TRUNC) { | |
fb286bb2 | 252 | if (__skb_checksum_complete(skb)) |
1da177e4 LT |
253 | goto csum_copy_err; |
254 | err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov, | |
255 | copied); | |
256 | } else { | |
257 | err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); | |
258 | if (err == -EINVAL) | |
259 | goto csum_copy_err; | |
260 | } | |
261 | if (err) | |
262 | goto out_free; | |
263 | ||
264 | sock_recv_timestamp(msg, sk, skb); | |
265 | ||
266 | /* Copy the address. */ | |
267 | if (msg->msg_name) { | |
268 | struct sockaddr_in6 *sin6; | |
269 | ||
270 | sin6 = (struct sockaddr_in6 *) msg->msg_name; | |
271 | sin6->sin6_family = AF_INET6; | |
272 | sin6->sin6_port = skb->h.uh->source; | |
273 | sin6->sin6_flowinfo = 0; | |
274 | sin6->sin6_scope_id = 0; | |
275 | ||
276 | if (skb->protocol == htons(ETH_P_IP)) | |
277 | ipv6_addr_set(&sin6->sin6_addr, 0, 0, | |
278 | htonl(0xffff), skb->nh.iph->saddr); | |
279 | else { | |
280 | ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr); | |
281 | if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) | |
282 | sin6->sin6_scope_id = IP6CB(skb)->iif; | |
283 | } | |
284 | ||
285 | } | |
286 | if (skb->protocol == htons(ETH_P_IP)) { | |
287 | if (inet->cmsg_flags) | |
288 | ip_cmsg_recv(msg, skb); | |
289 | } else { | |
290 | if (np->rxopt.all) | |
291 | datagram_recv_ctl(sk, msg, skb); | |
292 | } | |
293 | ||
294 | err = copied; | |
295 | if (flags & MSG_TRUNC) | |
296 | err = skb->len - sizeof(struct udphdr); | |
297 | ||
298 | out_free: | |
299 | skb_free_datagram(sk, skb); | |
300 | out: | |
301 | return err; | |
302 | ||
303 | csum_copy_err: | |
3305b80c | 304 | skb_kill_datagram(sk, skb, flags); |
1da177e4 LT |
305 | |
306 | if (flags & MSG_DONTWAIT) { | |
307 | UDP6_INC_STATS_USER(UDP_MIB_INERRORS); | |
308 | return -EAGAIN; | |
309 | } | |
310 | goto try_again; | |
311 | } | |
312 | ||
313 | static void udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
314 | int type, int code, int offset, __u32 info) | |
315 | { | |
316 | struct ipv6_pinfo *np; | |
317 | struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data; | |
318 | struct net_device *dev = skb->dev; | |
319 | struct in6_addr *saddr = &hdr->saddr; | |
320 | struct in6_addr *daddr = &hdr->daddr; | |
321 | struct udphdr *uh = (struct udphdr*)(skb->data+offset); | |
322 | struct sock *sk; | |
323 | int err; | |
324 | ||
325 | sk = udp_v6_lookup(daddr, uh->dest, saddr, uh->source, dev->ifindex); | |
326 | ||
327 | if (sk == NULL) | |
328 | return; | |
329 | ||
330 | np = inet6_sk(sk); | |
331 | ||
332 | if (!icmpv6_err_convert(type, code, &err) && !np->recverr) | |
333 | goto out; | |
334 | ||
335 | if (sk->sk_state != TCP_ESTABLISHED && !np->recverr) | |
336 | goto out; | |
337 | ||
338 | if (np->recverr) | |
339 | ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); | |
340 | ||
341 | sk->sk_err = err; | |
342 | sk->sk_error_report(sk); | |
343 | out: | |
344 | sock_put(sk); | |
345 | } | |
346 | ||
347 | static inline int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) | |
348 | { | |
349 | if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { | |
350 | kfree_skb(skb); | |
351 | return -1; | |
352 | } | |
353 | ||
fb286bb2 HX |
354 | if (skb_checksum_complete(skb)) { |
355 | UDP6_INC_STATS_BH(UDP_MIB_INERRORS); | |
356 | kfree_skb(skb); | |
357 | return 0; | |
1da177e4 LT |
358 | } |
359 | ||
360 | if (sock_queue_rcv_skb(sk,skb)<0) { | |
361 | UDP6_INC_STATS_BH(UDP_MIB_INERRORS); | |
362 | kfree_skb(skb); | |
363 | return 0; | |
364 | } | |
365 | UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS); | |
366 | return 0; | |
367 | } | |
368 | ||
369 | static struct sock *udp_v6_mcast_next(struct sock *sk, | |
370 | u16 loc_port, struct in6_addr *loc_addr, | |
371 | u16 rmt_port, struct in6_addr *rmt_addr, | |
372 | int dif) | |
373 | { | |
374 | struct hlist_node *node; | |
375 | struct sock *s = sk; | |
376 | unsigned short num = ntohs(loc_port); | |
377 | ||
378 | sk_for_each_from(s, node) { | |
379 | struct inet_sock *inet = inet_sk(s); | |
380 | ||
381 | if (inet->num == num && s->sk_family == PF_INET6) { | |
382 | struct ipv6_pinfo *np = inet6_sk(s); | |
383 | if (inet->dport) { | |
384 | if (inet->dport != rmt_port) | |
385 | continue; | |
386 | } | |
387 | if (!ipv6_addr_any(&np->daddr) && | |
388 | !ipv6_addr_equal(&np->daddr, rmt_addr)) | |
389 | continue; | |
390 | ||
391 | if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) | |
392 | continue; | |
393 | ||
394 | if (!ipv6_addr_any(&np->rcv_saddr)) { | |
40796c5e DS |
395 | if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) |
396 | continue; | |
1da177e4 LT |
397 | } |
398 | if(!inet6_mc_check(s, loc_addr, rmt_addr)) | |
399 | continue; | |
400 | return s; | |
401 | } | |
402 | } | |
403 | return NULL; | |
404 | } | |
405 | ||
406 | /* | |
407 | * Note: called only from the BH handler context, | |
408 | * so we don't need to lock the hashes. | |
409 | */ | |
410 | static void udpv6_mcast_deliver(struct udphdr *uh, | |
411 | struct in6_addr *saddr, struct in6_addr *daddr, | |
412 | struct sk_buff *skb) | |
413 | { | |
414 | struct sock *sk, *sk2; | |
415 | int dif; | |
416 | ||
417 | read_lock(&udp_hash_lock); | |
418 | sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]); | |
419 | dif = skb->dev->ifindex; | |
420 | sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); | |
421 | if (!sk) { | |
422 | kfree_skb(skb); | |
423 | goto out; | |
424 | } | |
425 | ||
426 | sk2 = sk; | |
427 | while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr, | |
428 | uh->source, saddr, dif))) { | |
429 | struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC); | |
430 | if (buff) | |
431 | udpv6_queue_rcv_skb(sk2, buff); | |
432 | } | |
433 | udpv6_queue_rcv_skb(sk, skb); | |
434 | out: | |
435 | read_unlock(&udp_hash_lock); | |
436 | } | |
437 | ||
951dbc8a | 438 | static int udpv6_rcv(struct sk_buff **pskb) |
1da177e4 LT |
439 | { |
440 | struct sk_buff *skb = *pskb; | |
441 | struct sock *sk; | |
442 | struct udphdr *uh; | |
443 | struct net_device *dev = skb->dev; | |
444 | struct in6_addr *saddr, *daddr; | |
445 | u32 ulen = 0; | |
446 | ||
447 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) | |
448 | goto short_packet; | |
449 | ||
450 | saddr = &skb->nh.ipv6h->saddr; | |
451 | daddr = &skb->nh.ipv6h->daddr; | |
452 | uh = skb->h.uh; | |
453 | ||
454 | ulen = ntohs(uh->len); | |
455 | ||
456 | /* Check for jumbo payload */ | |
457 | if (ulen == 0) | |
458 | ulen = skb->len; | |
459 | ||
460 | if (ulen > skb->len || ulen < sizeof(*uh)) | |
461 | goto short_packet; | |
462 | ||
463 | if (uh->check == 0) { | |
464 | /* RFC 2460 section 8.1 says that we SHOULD log | |
465 | this error. Well, it is reasonable. | |
466 | */ | |
64ce2073 | 467 | LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n"); |
1da177e4 LT |
468 | goto discard; |
469 | } | |
470 | ||
471 | if (ulen < skb->len) { | |
42ca89c1 | 472 | if (pskb_trim_rcsum(skb, ulen)) |
1da177e4 LT |
473 | goto discard; |
474 | saddr = &skb->nh.ipv6h->saddr; | |
475 | daddr = &skb->nh.ipv6h->daddr; | |
476 | uh = skb->h.uh; | |
477 | } | |
478 | ||
fb286bb2 HX |
479 | if (skb->ip_summed == CHECKSUM_HW && |
480 | !csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, skb->csum)) | |
1da177e4 | 481 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
fb286bb2 | 482 | |
1da177e4 LT |
483 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) |
484 | skb->csum = ~csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, 0); | |
485 | ||
486 | /* | |
487 | * Multicast receive code | |
488 | */ | |
489 | if (ipv6_addr_is_multicast(daddr)) { | |
490 | udpv6_mcast_deliver(uh, saddr, daddr, skb); | |
491 | return 0; | |
492 | } | |
493 | ||
494 | /* Unicast */ | |
495 | ||
496 | /* | |
497 | * check socket cache ... must talk to Alan about his plans | |
498 | * for sock caches... i'll skip this for now. | |
499 | */ | |
500 | sk = udp_v6_lookup(saddr, uh->source, daddr, uh->dest, dev->ifindex); | |
501 | ||
502 | if (sk == NULL) { | |
503 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) | |
504 | goto discard; | |
505 | ||
fb286bb2 | 506 | if (skb_checksum_complete(skb)) |
1da177e4 LT |
507 | goto discard; |
508 | UDP6_INC_STATS_BH(UDP_MIB_NOPORTS); | |
509 | ||
510 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev); | |
511 | ||
512 | kfree_skb(skb); | |
513 | return(0); | |
514 | } | |
515 | ||
516 | /* deliver */ | |
517 | ||
518 | udpv6_queue_rcv_skb(sk, skb); | |
519 | sock_put(sk); | |
520 | return(0); | |
521 | ||
522 | short_packet: | |
523 | if (net_ratelimit()) | |
524 | printk(KERN_DEBUG "UDP: short packet: %d/%u\n", ulen, skb->len); | |
525 | ||
526 | discard: | |
527 | UDP6_INC_STATS_BH(UDP_MIB_INERRORS); | |
528 | kfree_skb(skb); | |
529 | return(0); | |
530 | } | |
531 | /* | |
532 | * Throw away all pending data and cancel the corking. Socket is locked. | |
533 | */ | |
534 | static void udp_v6_flush_pending_frames(struct sock *sk) | |
535 | { | |
536 | struct udp_sock *up = udp_sk(sk); | |
537 | ||
538 | if (up->pending) { | |
539 | up->len = 0; | |
540 | up->pending = 0; | |
541 | ip6_flush_pending_frames(sk); | |
542 | } | |
543 | } | |
544 | ||
545 | /* | |
546 | * Sending | |
547 | */ | |
548 | ||
549 | static int udp_v6_push_pending_frames(struct sock *sk, struct udp_sock *up) | |
550 | { | |
551 | struct sk_buff *skb; | |
552 | struct udphdr *uh; | |
553 | struct inet_sock *inet = inet_sk(sk); | |
554 | struct flowi *fl = &inet->cork.fl; | |
555 | int err = 0; | |
556 | ||
557 | /* Grab the skbuff where UDP header space exists. */ | |
558 | if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) | |
559 | goto out; | |
560 | ||
561 | /* | |
562 | * Create a UDP header | |
563 | */ | |
564 | uh = skb->h.uh; | |
565 | uh->source = fl->fl_ip_sport; | |
566 | uh->dest = fl->fl_ip_dport; | |
567 | uh->len = htons(up->len); | |
568 | uh->check = 0; | |
569 | ||
570 | if (sk->sk_no_check == UDP_CSUM_NOXMIT) { | |
571 | skb->ip_summed = CHECKSUM_NONE; | |
572 | goto send; | |
573 | } | |
574 | ||
575 | if (skb_queue_len(&sk->sk_write_queue) == 1) { | |
576 | skb->csum = csum_partial((char *)uh, | |
577 | sizeof(struct udphdr), skb->csum); | |
578 | uh->check = csum_ipv6_magic(&fl->fl6_src, | |
579 | &fl->fl6_dst, | |
580 | up->len, fl->proto, skb->csum); | |
581 | } else { | |
582 | u32 tmp_csum = 0; | |
583 | ||
584 | skb_queue_walk(&sk->sk_write_queue, skb) { | |
585 | tmp_csum = csum_add(tmp_csum, skb->csum); | |
586 | } | |
587 | tmp_csum = csum_partial((char *)uh, | |
588 | sizeof(struct udphdr), tmp_csum); | |
589 | tmp_csum = csum_ipv6_magic(&fl->fl6_src, | |
590 | &fl->fl6_dst, | |
591 | up->len, fl->proto, tmp_csum); | |
592 | uh->check = tmp_csum; | |
593 | ||
594 | } | |
595 | if (uh->check == 0) | |
596 | uh->check = -1; | |
597 | ||
598 | send: | |
599 | err = ip6_push_pending_frames(sk); | |
600 | out: | |
601 | up->len = 0; | |
602 | up->pending = 0; | |
603 | return err; | |
604 | } | |
605 | ||
606 | static int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, | |
607 | struct msghdr *msg, size_t len) | |
608 | { | |
609 | struct ipv6_txoptions opt_space; | |
610 | struct udp_sock *up = udp_sk(sk); | |
611 | struct inet_sock *inet = inet_sk(sk); | |
612 | struct ipv6_pinfo *np = inet6_sk(sk); | |
613 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name; | |
614 | struct in6_addr *daddr, *final_p = NULL, final; | |
615 | struct ipv6_txoptions *opt = NULL; | |
616 | struct ip6_flowlabel *flowlabel = NULL; | |
617 | struct flowi *fl = &inet->cork.fl; | |
618 | struct dst_entry *dst; | |
619 | int addr_len = msg->msg_namelen; | |
620 | int ulen = len; | |
621 | int hlimit = -1; | |
41a1f8ea | 622 | int tclass = -1; |
1da177e4 LT |
623 | int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; |
624 | int err; | |
987905de | 625 | int connected = 0; |
1da177e4 LT |
626 | |
627 | /* destination address check */ | |
628 | if (sin6) { | |
629 | if (addr_len < offsetof(struct sockaddr, sa_data)) | |
630 | return -EINVAL; | |
631 | ||
632 | switch (sin6->sin6_family) { | |
633 | case AF_INET6: | |
634 | if (addr_len < SIN6_LEN_RFC2133) | |
635 | return -EINVAL; | |
636 | daddr = &sin6->sin6_addr; | |
637 | break; | |
638 | case AF_INET: | |
639 | goto do_udp_sendmsg; | |
640 | case AF_UNSPEC: | |
641 | msg->msg_name = sin6 = NULL; | |
642 | msg->msg_namelen = addr_len = 0; | |
643 | daddr = NULL; | |
644 | break; | |
645 | default: | |
646 | return -EINVAL; | |
647 | } | |
648 | } else if (!up->pending) { | |
649 | if (sk->sk_state != TCP_ESTABLISHED) | |
650 | return -EDESTADDRREQ; | |
651 | daddr = &np->daddr; | |
652 | } else | |
653 | daddr = NULL; | |
654 | ||
655 | if (daddr) { | |
656 | if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) { | |
657 | struct sockaddr_in sin; | |
658 | sin.sin_family = AF_INET; | |
659 | sin.sin_port = sin6 ? sin6->sin6_port : inet->dport; | |
660 | sin.sin_addr.s_addr = daddr->s6_addr32[3]; | |
661 | msg->msg_name = &sin; | |
662 | msg->msg_namelen = sizeof(sin); | |
663 | do_udp_sendmsg: | |
664 | if (__ipv6_only_sock(sk)) | |
665 | return -ENETUNREACH; | |
666 | return udp_sendmsg(iocb, sk, msg, len); | |
667 | } | |
668 | } | |
669 | ||
670 | if (up->pending == AF_INET) | |
671 | return udp_sendmsg(iocb, sk, msg, len); | |
672 | ||
673 | /* Rough check on arithmetic overflow, | |
674 | better check is made in ip6_build_xmit | |
675 | */ | |
676 | if (len > INT_MAX - sizeof(struct udphdr)) | |
677 | return -EMSGSIZE; | |
678 | ||
679 | if (up->pending) { | |
680 | /* | |
681 | * There are pending frames. | |
682 | * The socket lock must be held while it's corked. | |
683 | */ | |
684 | lock_sock(sk); | |
685 | if (likely(up->pending)) { | |
686 | if (unlikely(up->pending != AF_INET6)) { | |
687 | release_sock(sk); | |
688 | return -EAFNOSUPPORT; | |
689 | } | |
690 | dst = NULL; | |
691 | goto do_append_data; | |
692 | } | |
693 | release_sock(sk); | |
694 | } | |
695 | ulen += sizeof(struct udphdr); | |
696 | ||
697 | memset(fl, 0, sizeof(*fl)); | |
698 | ||
699 | if (sin6) { | |
700 | if (sin6->sin6_port == 0) | |
701 | return -EINVAL; | |
702 | ||
703 | fl->fl_ip_dport = sin6->sin6_port; | |
704 | daddr = &sin6->sin6_addr; | |
705 | ||
706 | if (np->sndflow) { | |
707 | fl->fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; | |
708 | if (fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) { | |
709 | flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel); | |
710 | if (flowlabel == NULL) | |
711 | return -EINVAL; | |
712 | daddr = &flowlabel->dst; | |
713 | } | |
714 | } | |
715 | ||
716 | /* | |
717 | * Otherwise it will be difficult to maintain | |
718 | * sk->sk_dst_cache. | |
719 | */ | |
720 | if (sk->sk_state == TCP_ESTABLISHED && | |
721 | ipv6_addr_equal(daddr, &np->daddr)) | |
722 | daddr = &np->daddr; | |
723 | ||
724 | if (addr_len >= sizeof(struct sockaddr_in6) && | |
725 | sin6->sin6_scope_id && | |
726 | ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) | |
727 | fl->oif = sin6->sin6_scope_id; | |
728 | } else { | |
729 | if (sk->sk_state != TCP_ESTABLISHED) | |
730 | return -EDESTADDRREQ; | |
731 | ||
732 | fl->fl_ip_dport = inet->dport; | |
733 | daddr = &np->daddr; | |
734 | fl->fl6_flowlabel = np->flow_label; | |
987905de | 735 | connected = 1; |
1da177e4 LT |
736 | } |
737 | ||
738 | if (!fl->oif) | |
739 | fl->oif = sk->sk_bound_dev_if; | |
740 | ||
741 | if (msg->msg_controllen) { | |
742 | opt = &opt_space; | |
743 | memset(opt, 0, sizeof(struct ipv6_txoptions)); | |
744 | opt->tot_len = sizeof(*opt); | |
745 | ||
41a1f8ea | 746 | err = datagram_send_ctl(msg, fl, opt, &hlimit, &tclass); |
1da177e4 LT |
747 | if (err < 0) { |
748 | fl6_sock_release(flowlabel); | |
749 | return err; | |
750 | } | |
751 | if ((fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { | |
752 | flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel); | |
753 | if (flowlabel == NULL) | |
754 | return -EINVAL; | |
755 | } | |
756 | if (!(opt->opt_nflen|opt->opt_flen)) | |
757 | opt = NULL; | |
987905de | 758 | connected = 0; |
1da177e4 LT |
759 | } |
760 | if (opt == NULL) | |
761 | opt = np->opt; | |
df9890c3 YH |
762 | if (flowlabel) |
763 | opt = fl6_merge_options(&opt_space, flowlabel, opt); | |
764 | opt = ipv6_fixup_options(&opt_space, opt); | |
1da177e4 LT |
765 | |
766 | fl->proto = IPPROTO_UDP; | |
767 | ipv6_addr_copy(&fl->fl6_dst, daddr); | |
768 | if (ipv6_addr_any(&fl->fl6_src) && !ipv6_addr_any(&np->saddr)) | |
769 | ipv6_addr_copy(&fl->fl6_src, &np->saddr); | |
770 | fl->fl_ip_sport = inet->sport; | |
771 | ||
772 | /* merge ip6_build_xmit from ip6_output */ | |
773 | if (opt && opt->srcrt) { | |
774 | struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt; | |
775 | ipv6_addr_copy(&final, &fl->fl6_dst); | |
776 | ipv6_addr_copy(&fl->fl6_dst, rt0->addr); | |
777 | final_p = &final; | |
987905de | 778 | connected = 0; |
1da177e4 LT |
779 | } |
780 | ||
987905de | 781 | if (!fl->oif && ipv6_addr_is_multicast(&fl->fl6_dst)) { |
1da177e4 | 782 | fl->oif = np->mcast_oif; |
987905de MK |
783 | connected = 0; |
784 | } | |
1da177e4 LT |
785 | |
786 | err = ip6_dst_lookup(sk, &dst, fl); | |
787 | if (err) | |
788 | goto out; | |
789 | if (final_p) | |
790 | ipv6_addr_copy(&fl->fl6_dst, final_p); | |
791 | ||
e104411b | 792 | if ((err = xfrm_lookup(&dst, fl, sk, 0)) < 0) |
1da177e4 | 793 | goto out; |
1da177e4 LT |
794 | |
795 | if (hlimit < 0) { | |
796 | if (ipv6_addr_is_multicast(&fl->fl6_dst)) | |
797 | hlimit = np->mcast_hops; | |
798 | else | |
799 | hlimit = np->hop_limit; | |
800 | if (hlimit < 0) | |
801 | hlimit = dst_metric(dst, RTAX_HOPLIMIT); | |
802 | if (hlimit < 0) | |
803 | hlimit = ipv6_get_hoplimit(dst->dev); | |
804 | } | |
805 | ||
41a1f8ea YH |
806 | if (tclass < 0) { |
807 | tclass = np->tclass; | |
808 | if (tclass < 0) | |
809 | tclass = 0; | |
810 | } | |
811 | ||
1da177e4 LT |
812 | if (msg->msg_flags&MSG_CONFIRM) |
813 | goto do_confirm; | |
814 | back_from_confirm: | |
815 | ||
816 | lock_sock(sk); | |
817 | if (unlikely(up->pending)) { | |
818 | /* The socket is already corked while preparing it. */ | |
819 | /* ... which is an evident application bug. --ANK */ | |
820 | release_sock(sk); | |
821 | ||
64ce2073 | 822 | LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); |
1da177e4 LT |
823 | err = -EINVAL; |
824 | goto out; | |
825 | } | |
826 | ||
827 | up->pending = AF_INET6; | |
828 | ||
829 | do_append_data: | |
830 | up->len += ulen; | |
41a1f8ea YH |
831 | err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen, |
832 | sizeof(struct udphdr), hlimit, tclass, opt, fl, | |
833 | (struct rt6_info*)dst, | |
834 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); | |
1da177e4 LT |
835 | if (err) |
836 | udp_v6_flush_pending_frames(sk); | |
837 | else if (!corkreq) | |
838 | err = udp_v6_push_pending_frames(sk, up); | |
839 | ||
a5e7c210 DM |
840 | if (dst) { |
841 | if (connected) { | |
842 | ip6_dst_store(sk, dst, | |
843 | ipv6_addr_equal(&fl->fl6_dst, &np->daddr) ? | |
844 | &np->daddr : NULL); | |
845 | } else { | |
846 | dst_release(dst); | |
847 | } | |
848 | } | |
849 | ||
1da177e4 LT |
850 | if (err > 0) |
851 | err = np->recverr ? net_xmit_errno(err) : 0; | |
852 | release_sock(sk); | |
853 | out: | |
854 | fl6_sock_release(flowlabel); | |
855 | if (!err) { | |
856 | UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS); | |
857 | return len; | |
858 | } | |
859 | return err; | |
860 | ||
861 | do_confirm: | |
862 | dst_confirm(dst); | |
863 | if (!(msg->msg_flags&MSG_PROBE) || len) | |
864 | goto back_from_confirm; | |
865 | err = 0; | |
866 | goto out; | |
867 | } | |
868 | ||
869 | static int udpv6_destroy_sock(struct sock *sk) | |
870 | { | |
871 | lock_sock(sk); | |
872 | udp_v6_flush_pending_frames(sk); | |
873 | release_sock(sk); | |
874 | ||
875 | inet6_destroy_sock(sk); | |
876 | ||
877 | return 0; | |
878 | } | |
879 | ||
880 | /* | |
881 | * Socket option code for UDP | |
882 | */ | |
3fdadf7d | 883 | static int do_udpv6_setsockopt(struct sock *sk, int level, int optname, |
1da177e4 LT |
884 | char __user *optval, int optlen) |
885 | { | |
886 | struct udp_sock *up = udp_sk(sk); | |
887 | int val; | |
888 | int err = 0; | |
889 | ||
1da177e4 LT |
890 | if(optlen<sizeof(int)) |
891 | return -EINVAL; | |
892 | ||
893 | if (get_user(val, (int __user *)optval)) | |
894 | return -EFAULT; | |
895 | ||
896 | switch(optname) { | |
897 | case UDP_CORK: | |
898 | if (val != 0) { | |
899 | up->corkflag = 1; | |
900 | } else { | |
901 | up->corkflag = 0; | |
902 | lock_sock(sk); | |
903 | udp_v6_push_pending_frames(sk, up); | |
904 | release_sock(sk); | |
905 | } | |
906 | break; | |
907 | ||
908 | case UDP_ENCAP: | |
909 | switch (val) { | |
910 | case 0: | |
911 | up->encap_type = val; | |
912 | break; | |
913 | default: | |
914 | err = -ENOPROTOOPT; | |
915 | break; | |
916 | } | |
917 | break; | |
918 | ||
919 | default: | |
920 | err = -ENOPROTOOPT; | |
921 | break; | |
922 | }; | |
923 | ||
924 | return err; | |
925 | } | |
926 | ||
3fdadf7d DM |
927 | static int udpv6_setsockopt(struct sock *sk, int level, int optname, |
928 | char __user *optval, int optlen) | |
929 | { | |
930 | if (level != SOL_UDP) | |
931 | return ipv6_setsockopt(sk, level, optname, optval, optlen); | |
932 | return do_udpv6_setsockopt(sk, level, optname, optval, optlen); | |
933 | } | |
934 | ||
935 | #ifdef CONFIG_COMPAT | |
936 | static int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, | |
543d9cfe | 937 | char __user *optval, int optlen) |
3fdadf7d DM |
938 | { |
939 | if (level != SOL_UDP) | |
543d9cfe ACM |
940 | return compat_ipv6_setsockopt(sk, level, optname, |
941 | optval, optlen); | |
3fdadf7d DM |
942 | return do_udpv6_setsockopt(sk, level, optname, optval, optlen); |
943 | } | |
944 | #endif | |
945 | ||
946 | static int do_udpv6_getsockopt(struct sock *sk, int level, int optname, | |
1da177e4 LT |
947 | char __user *optval, int __user *optlen) |
948 | { | |
949 | struct udp_sock *up = udp_sk(sk); | |
950 | int val, len; | |
951 | ||
1da177e4 LT |
952 | if(get_user(len,optlen)) |
953 | return -EFAULT; | |
954 | ||
955 | len = min_t(unsigned int, len, sizeof(int)); | |
956 | ||
957 | if(len < 0) | |
958 | return -EINVAL; | |
959 | ||
960 | switch(optname) { | |
961 | case UDP_CORK: | |
962 | val = up->corkflag; | |
963 | break; | |
964 | ||
965 | case UDP_ENCAP: | |
966 | val = up->encap_type; | |
967 | break; | |
968 | ||
969 | default: | |
970 | return -ENOPROTOOPT; | |
971 | }; | |
972 | ||
973 | if(put_user(len, optlen)) | |
974 | return -EFAULT; | |
975 | if(copy_to_user(optval, &val,len)) | |
976 | return -EFAULT; | |
977 | return 0; | |
978 | } | |
979 | ||
3fdadf7d DM |
980 | static int udpv6_getsockopt(struct sock *sk, int level, int optname, |
981 | char __user *optval, int __user *optlen) | |
982 | { | |
983 | if (level != SOL_UDP) | |
984 | return ipv6_getsockopt(sk, level, optname, optval, optlen); | |
985 | return do_udpv6_getsockopt(sk, level, optname, optval, optlen); | |
986 | } | |
987 | ||
988 | #ifdef CONFIG_COMPAT | |
989 | static int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, | |
543d9cfe | 990 | char __user *optval, int __user *optlen) |
3fdadf7d DM |
991 | { |
992 | if (level != SOL_UDP) | |
543d9cfe ACM |
993 | return compat_ipv6_getsockopt(sk, level, optname, |
994 | optval, optlen); | |
3fdadf7d DM |
995 | return do_udpv6_getsockopt(sk, level, optname, optval, optlen); |
996 | } | |
997 | #endif | |
998 | ||
1da177e4 LT |
999 | static struct inet6_protocol udpv6_protocol = { |
1000 | .handler = udpv6_rcv, | |
1001 | .err_handler = udpv6_err, | |
1002 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, | |
1003 | }; | |
1004 | ||
1005 | /* ------------------------------------------------------------------------ */ | |
1006 | #ifdef CONFIG_PROC_FS | |
1007 | ||
1008 | static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket) | |
1009 | { | |
1010 | struct inet_sock *inet = inet_sk(sp); | |
1011 | struct ipv6_pinfo *np = inet6_sk(sp); | |
1012 | struct in6_addr *dest, *src; | |
1013 | __u16 destp, srcp; | |
1014 | ||
1015 | dest = &np->daddr; | |
1016 | src = &np->rcv_saddr; | |
1017 | destp = ntohs(inet->dport); | |
1018 | srcp = ntohs(inet->sport); | |
1019 | seq_printf(seq, | |
1020 | "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " | |
1021 | "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n", | |
1022 | bucket, | |
1023 | src->s6_addr32[0], src->s6_addr32[1], | |
1024 | src->s6_addr32[2], src->s6_addr32[3], srcp, | |
1025 | dest->s6_addr32[0], dest->s6_addr32[1], | |
1026 | dest->s6_addr32[2], dest->s6_addr32[3], destp, | |
1027 | sp->sk_state, | |
1028 | atomic_read(&sp->sk_wmem_alloc), | |
1029 | atomic_read(&sp->sk_rmem_alloc), | |
1030 | 0, 0L, 0, | |
1031 | sock_i_uid(sp), 0, | |
1032 | sock_i_ino(sp), | |
1033 | atomic_read(&sp->sk_refcnt), sp); | |
1034 | } | |
1035 | ||
1036 | static int udp6_seq_show(struct seq_file *seq, void *v) | |
1037 | { | |
1038 | if (v == SEQ_START_TOKEN) | |
1039 | seq_printf(seq, | |
1040 | " sl " | |
1041 | "local_address " | |
1042 | "remote_address " | |
1043 | "st tx_queue rx_queue tr tm->when retrnsmt" | |
1044 | " uid timeout inode\n"); | |
1045 | else | |
1046 | udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket); | |
1047 | return 0; | |
1048 | } | |
1049 | ||
1050 | static struct file_operations udp6_seq_fops; | |
1051 | static struct udp_seq_afinfo udp6_seq_afinfo = { | |
1052 | .owner = THIS_MODULE, | |
1053 | .name = "udp6", | |
1054 | .family = AF_INET6, | |
1055 | .seq_show = udp6_seq_show, | |
1056 | .seq_fops = &udp6_seq_fops, | |
1057 | }; | |
1058 | ||
1059 | int __init udp6_proc_init(void) | |
1060 | { | |
1061 | return udp_proc_register(&udp6_seq_afinfo); | |
1062 | } | |
1063 | ||
1064 | void udp6_proc_exit(void) { | |
1065 | udp_proc_unregister(&udp6_seq_afinfo); | |
1066 | } | |
1067 | #endif /* CONFIG_PROC_FS */ | |
1068 | ||
1069 | /* ------------------------------------------------------------------------ */ | |
1070 | ||
1071 | struct proto udpv6_prot = { | |
543d9cfe ACM |
1072 | .name = "UDPv6", |
1073 | .owner = THIS_MODULE, | |
1074 | .close = udpv6_close, | |
1075 | .connect = ip6_datagram_connect, | |
1076 | .disconnect = udp_disconnect, | |
1077 | .ioctl = udp_ioctl, | |
1078 | .destroy = udpv6_destroy_sock, | |
1079 | .setsockopt = udpv6_setsockopt, | |
1080 | .getsockopt = udpv6_getsockopt, | |
1081 | .sendmsg = udpv6_sendmsg, | |
1082 | .recvmsg = udpv6_recvmsg, | |
1083 | .backlog_rcv = udpv6_queue_rcv_skb, | |
1084 | .hash = udp_v6_hash, | |
1085 | .unhash = udp_v6_unhash, | |
1086 | .get_port = udp_v6_get_port, | |
1087 | .obj_size = sizeof(struct udp6_sock), | |
3fdadf7d | 1088 | #ifdef CONFIG_COMPAT |
543d9cfe ACM |
1089 | .compat_setsockopt = compat_udpv6_setsockopt, |
1090 | .compat_getsockopt = compat_udpv6_getsockopt, | |
3fdadf7d | 1091 | #endif |
1da177e4 LT |
1092 | }; |
1093 | ||
1da177e4 LT |
1094 | static struct inet_protosw udpv6_protosw = { |
1095 | .type = SOCK_DGRAM, | |
1096 | .protocol = IPPROTO_UDP, | |
1097 | .prot = &udpv6_prot, | |
1098 | .ops = &inet6_dgram_ops, | |
1099 | .capability =-1, | |
1100 | .no_check = UDP_CSUM_DEFAULT, | |
1101 | .flags = INET_PROTOSW_PERMANENT, | |
1102 | }; | |
1103 | ||
1104 | ||
1105 | void __init udpv6_init(void) | |
1106 | { | |
1107 | if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0) | |
1108 | printk(KERN_ERR "udpv6_init: Could not register protocol\n"); | |
1109 | inet6_register_protosw(&udpv6_protosw); | |
1110 | } |