Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * UDP over IPv6 |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Based on linux/ipv4/udp.c |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Fixes: |
| 11 | * Hideaki YOSHIFUJI : sin6_scope_id support |
| 12 | * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which |
| 13 | * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind |
| 14 | * a single port at the same time. |
| 15 | * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data |
| 16 | * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or |
| 19 | * modify it under the terms of the GNU General Public License |
| 20 | * as published by the Free Software Foundation; either version |
| 21 | * 2 of the License, or (at your option) any later version. |
| 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/errno.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/socket.h> |
| 27 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/net.h> |
| 29 | #include <linux/in6.h> |
| 30 | #include <linux/netdevice.h> |
| 31 | #include <linux/if_arp.h> |
| 32 | #include <linux/ipv6.h> |
| 33 | #include <linux/icmpv6.h> |
| 34 | #include <linux/init.h> |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 35 | #include <linux/module.h> |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 36 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/uaccess.h> |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <net/ndisc.h> |
| 40 | #include <net/protocol.h> |
| 41 | #include <net/transp_v6.h> |
| 42 | #include <net/ip6_route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <net/raw.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 44 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <net/ip6_checksum.h> |
| 46 | #include <net/xfrm.h> |
| 47 | |
| 48 | #include <linux/proc_fs.h> |
| 49 | #include <linux/seq_file.h> |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 50 | #include "udp_impl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Vlad Yasevich | b2f5e7c | 2009-03-24 16:24:51 +0000 | [diff] [blame] | 52 | int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) |
| 53 | { |
| 54 | const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr; |
| 55 | const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); |
Vlad Yasevich | 499923c | 2009-04-09 17:37:33 +0000 | [diff] [blame] | 56 | __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr; |
| 57 | __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2); |
Vlad Yasevich | b2f5e7c | 2009-03-24 16:24:51 +0000 | [diff] [blame] | 58 | int sk_ipv6only = ipv6_only_sock(sk); |
| 59 | int sk2_ipv6only = inet_v6_ipv6only(sk2); |
| 60 | int addr_type = ipv6_addr_type(sk_rcv_saddr6); |
| 61 | int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; |
| 62 | |
| 63 | /* if both are mapped, treat as IPv4 */ |
| 64 | if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED) |
Vlad Yasevich | 499923c | 2009-04-09 17:37:33 +0000 | [diff] [blame] | 65 | return (!sk2_ipv6only && |
| 66 | (!sk_rcv_saddr || !sk2_rcv_saddr || |
| 67 | sk_rcv_saddr == sk2_rcv_saddr)); |
Vlad Yasevich | b2f5e7c | 2009-03-24 16:24:51 +0000 | [diff] [blame] | 68 | |
| 69 | if (addr_type2 == IPV6_ADDR_ANY && |
| 70 | !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) |
| 71 | return 1; |
| 72 | |
| 73 | if (addr_type == IPV6_ADDR_ANY && |
| 74 | !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED)) |
| 75 | return 1; |
| 76 | |
| 77 | if (sk2_rcv_saddr6 && |
| 78 | ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6)) |
| 79 | return 1; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 84 | int udp_v6_get_port(struct sock *sk, unsigned short snum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 86 | return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 89 | static inline int compute_score(struct sock *sk, struct net *net, |
| 90 | unsigned short hnum, |
| 91 | struct in6_addr *saddr, __be16 sport, |
| 92 | struct in6_addr *daddr, __be16 dport, |
| 93 | int dif) |
| 94 | { |
| 95 | int score = -1; |
| 96 | |
| 97 | if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && |
| 98 | sk->sk_family == PF_INET6) { |
| 99 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 100 | struct inet_sock *inet = inet_sk(sk); |
| 101 | |
| 102 | score = 0; |
| 103 | if (inet->dport) { |
| 104 | if (inet->dport != sport) |
| 105 | return -1; |
| 106 | score++; |
| 107 | } |
| 108 | if (!ipv6_addr_any(&np->rcv_saddr)) { |
| 109 | if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) |
| 110 | return -1; |
| 111 | score++; |
| 112 | } |
| 113 | if (!ipv6_addr_any(&np->daddr)) { |
| 114 | if (!ipv6_addr_equal(&np->daddr, saddr)) |
| 115 | return -1; |
| 116 | score++; |
| 117 | } |
| 118 | if (sk->sk_bound_dev_if) { |
| 119 | if (sk->sk_bound_dev_if != dif) |
| 120 | return -1; |
| 121 | score++; |
| 122 | } |
| 123 | } |
| 124 | return score; |
| 125 | } |
| 126 | |
Pavel Emelyanov | fa4d3c6 | 2008-01-31 05:07:57 -0800 | [diff] [blame] | 127 | static struct sock *__udp6_lib_lookup(struct net *net, |
| 128 | struct in6_addr *saddr, __be16 sport, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 129 | struct in6_addr *daddr, __be16 dport, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 130 | int dif, struct udp_table *udptable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Eric Dumazet | 271b72c | 2008-10-29 02:11:14 -0700 | [diff] [blame] | 132 | struct sock *sk, *result; |
Eric Dumazet | 88ab193 | 2008-11-16 19:39:21 -0800 | [diff] [blame] | 133 | struct hlist_nulls_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | unsigned short hnum = ntohs(dport); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 135 | unsigned int hash = udp_hashfn(net, hnum); |
| 136 | struct udp_hslot *hslot = &udptable->hash[hash]; |
Eric Dumazet | 271b72c | 2008-10-29 02:11:14 -0700 | [diff] [blame] | 137 | int score, badness; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Eric Dumazet | 271b72c | 2008-10-29 02:11:14 -0700 | [diff] [blame] | 139 | rcu_read_lock(); |
| 140 | begin: |
| 141 | result = NULL; |
| 142 | badness = -1; |
Eric Dumazet | 88ab193 | 2008-11-16 19:39:21 -0800 | [diff] [blame] | 143 | sk_nulls_for_each_rcu(sk, node, &hslot->head) { |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 144 | score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif); |
| 145 | if (score > badness) { |
| 146 | result = sk; |
| 147 | badness = score; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
Eric Dumazet | 88ab193 | 2008-11-16 19:39:21 -0800 | [diff] [blame] | 150 | /* |
| 151 | * if the nulls value we got at the end of this lookup is |
| 152 | * not the expected one, we must restart lookup. |
| 153 | * We probably met an item that was moved to another chain. |
| 154 | */ |
| 155 | if (get_nulls_value(node) != hash) |
| 156 | goto begin; |
| 157 | |
Eric Dumazet | 271b72c | 2008-10-29 02:11:14 -0700 | [diff] [blame] | 158 | if (result) { |
| 159 | if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt))) |
| 160 | result = NULL; |
| 161 | else if (unlikely(compute_score(result, net, hnum, saddr, sport, |
| 162 | daddr, dport, dif) < badness)) { |
| 163 | sock_put(result); |
| 164 | goto begin; |
| 165 | } |
| 166 | } |
| 167 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return result; |
| 169 | } |
| 170 | |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 171 | static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, |
| 172 | __be16 sport, __be16 dport, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 173 | struct udp_table *udptable) |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 174 | { |
KOVACS Krisztian | 2354261 | 2008-10-07 12:41:01 -0700 | [diff] [blame] | 175 | struct sock *sk; |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 176 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 177 | |
KOVACS Krisztian | 2354261 | 2008-10-07 12:41:01 -0700 | [diff] [blame] | 178 | if (unlikely(sk = skb_steal_sock(skb))) |
| 179 | return sk; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 180 | return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport, |
| 181 | &iph->daddr, dport, inet6_iif(skb), |
| 182 | udptable); |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * This should be easy, if there is something there we |
| 187 | * return it, otherwise we block. |
| 188 | */ |
| 189 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 190 | int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | struct msghdr *msg, size_t len, |
| 192 | int noblock, int flags, int *addr_len) |
| 193 | { |
| 194 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 195 | struct inet_sock *inet = inet_sk(sk); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 196 | struct sk_buff *skb; |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 197 | unsigned int ulen, copied; |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 198 | int peeked; |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 199 | int err; |
| 200 | int is_udplite = IS_UDPLITE(sk); |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame] | 201 | int is_udp4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 203 | if (addr_len) |
| 204 | *addr_len=sizeof(struct sockaddr_in6); |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (flags & MSG_ERRQUEUE) |
| 207 | return ipv6_recv_error(sk, msg, len); |
| 208 | |
| 209 | try_again: |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 210 | skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), |
| 211 | &peeked, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (!skb) |
| 213 | goto out; |
| 214 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 215 | ulen = skb->len - sizeof(struct udphdr); |
| 216 | copied = len; |
| 217 | if (copied > ulen) |
| 218 | copied = ulen; |
| 219 | else if (copied < ulen) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 220 | msg->msg_flags |= MSG_TRUNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame] | 222 | is_udp4 = (skb->protocol == htons(ETH_P_IP)); |
| 223 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 224 | /* |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 225 | * If checksum is needed at all, try to do it while copying the |
| 226 | * data. If the data is truncated, or if we only want a partial |
| 227 | * coverage checksum (UDP-Lite), do it before the copy. |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 228 | */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 229 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 230 | if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { |
| 231 | if (udp_lib_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | goto csum_copy_err; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 235 | if (skb_csum_unnecessary(skb)) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 236 | err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), |
| 237 | msg->msg_iov, copied ); |
| 238 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); |
| 240 | if (err == -EINVAL) |
| 241 | goto csum_copy_err; |
| 242 | } |
| 243 | if (err) |
| 244 | goto out_free; |
| 245 | |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame] | 246 | if (!peeked) { |
| 247 | if (is_udp4) |
| 248 | UDP_INC_STATS_USER(sock_net(sk), |
| 249 | UDP_MIB_INDATAGRAMS, is_udplite); |
| 250 | else |
| 251 | UDP6_INC_STATS_USER(sock_net(sk), |
| 252 | UDP_MIB_INDATAGRAMS, is_udplite); |
| 253 | } |
Wang Chen | cb75994 | 2007-12-03 22:33:28 +1100 | [diff] [blame] | 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | sock_recv_timestamp(msg, sk, skb); |
| 256 | |
| 257 | /* Copy the address. */ |
| 258 | if (msg->msg_name) { |
| 259 | struct sockaddr_in6 *sin6; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | sin6 = (struct sockaddr_in6 *) msg->msg_name; |
| 262 | sin6->sin6_family = AF_INET6; |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 263 | sin6->sin6_port = udp_hdr(skb)->source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | sin6->sin6_flowinfo = 0; |
| 265 | sin6->sin6_scope_id = 0; |
| 266 | |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame] | 267 | if (is_udp4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | ipv6_addr_set(&sin6->sin6_addr, 0, 0, |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 269 | htonl(0xffff), ip_hdr(skb)->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | else { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 271 | ipv6_addr_copy(&sin6->sin6_addr, |
| 272 | &ipv6_hdr(skb)->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) |
| 274 | sin6->sin6_scope_id = IP6CB(skb)->iif; |
| 275 | } |
| 276 | |
| 277 | } |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame] | 278 | if (is_udp4) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (inet->cmsg_flags) |
| 280 | ip_cmsg_recv(msg, skb); |
| 281 | } else { |
| 282 | if (np->rxopt.all) |
| 283 | datagram_recv_ctl(sk, msg, skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | err = copied; |
| 287 | if (flags & MSG_TRUNC) |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 288 | err = ulen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | out_free: |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 291 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | skb_free_datagram(sk, skb); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 293 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | out: |
| 295 | return err; |
| 296 | |
| 297 | csum_copy_err: |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 298 | lock_sock(sk); |
Wei Yongjun | 0856f93 | 2008-11-02 16:14:27 +0000 | [diff] [blame] | 299 | if (!skb_kill_datagram(sk, skb, flags)) { |
| 300 | if (is_udp4) |
| 301 | UDP_INC_STATS_USER(sock_net(sk), |
| 302 | UDP_MIB_INERRORS, is_udplite); |
| 303 | else |
| 304 | UDP6_INC_STATS_USER(sock_net(sk), |
| 305 | UDP_MIB_INERRORS, is_udplite); |
| 306 | } |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 307 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Mitsuru Chinen | 7a0ff716 | 2007-11-05 21:29:17 -0800 | [diff] [blame] | 309 | if (flags & MSG_DONTWAIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | goto try_again; |
| 312 | } |
| 313 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 314 | void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 315 | u8 type, u8 code, int offset, __be32 info, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 316 | struct udp_table *udptable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
| 318 | struct ipv6_pinfo *np; |
| 319 | struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | struct in6_addr *saddr = &hdr->saddr; |
| 321 | struct in6_addr *daddr = &hdr->daddr; |
| 322 | struct udphdr *uh = (struct udphdr*)(skb->data+offset); |
| 323 | struct sock *sk; |
| 324 | int err; |
| 325 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 326 | sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 327 | saddr, uh->source, inet6_iif(skb), udptable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | if (sk == NULL) |
| 329 | return; |
| 330 | |
| 331 | np = inet6_sk(sk); |
| 332 | |
| 333 | if (!icmpv6_err_convert(type, code, &err) && !np->recverr) |
| 334 | goto out; |
| 335 | |
| 336 | if (sk->sk_state != TCP_ESTABLISHED && !np->recverr) |
| 337 | goto out; |
| 338 | |
| 339 | if (np->recverr) |
| 340 | ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); |
| 341 | |
| 342 | sk->sk_err = err; |
| 343 | sk->sk_error_report(sk); |
| 344 | out: |
| 345 | sock_put(sk); |
| 346 | } |
| 347 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 348 | static __inline__ void udpv6_err(struct sk_buff *skb, |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 349 | struct inet6_skb_parm *opt, u8 type, |
| 350 | u8 code, int offset, __be32 info ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 352 | __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) |
| 356 | { |
| 357 | struct udp_sock *up = udp_sk(sk); |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 358 | int rc; |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 359 | int is_udplite = IS_UDPLITE(sk); |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 360 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 361 | if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) |
| 362 | goto drop; |
| 363 | |
| 364 | /* |
| 365 | * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). |
| 366 | */ |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 367 | if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 368 | |
| 369 | if (up->pcrlen == 0) { /* full coverage was set */ |
| 370 | LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage" |
| 371 | " %d while full coverage %d requested\n", |
| 372 | UDP_SKB_CB(skb)->cscov, skb->len); |
| 373 | goto drop; |
| 374 | } |
| 375 | if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { |
| 376 | LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d " |
| 377 | "too small, need min %d\n", |
| 378 | UDP_SKB_CB(skb)->cscov, up->pcrlen); |
| 379 | goto drop; |
| 380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Herbert Xu | 1ab6eb6 | 2007-03-06 20:29:58 -0800 | [diff] [blame] | 383 | if (sk->sk_filter) { |
| 384 | if (udp_lib_checksum_complete(skb)) |
| 385 | goto drop; |
| 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 388 | if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { |
| 389 | /* Note that an ENOMEM error is charged twice */ |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 390 | if (rc == -ENOMEM) { |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 391 | UDP6_INC_STATS_BH(sock_net(sk), |
| 392 | UDP_MIB_RCVBUFERRORS, is_udplite); |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 393 | atomic_inc(&sk->sk_drops); |
| 394 | } |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 395 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
Wang Chen | cb75994 | 2007-12-03 22:33:28 +1100 | [diff] [blame] | 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | return 0; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 399 | drop: |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 400 | UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 401 | kfree_skb(skb); |
| 402 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Eric Dumazet | 920a461 | 2008-11-01 21:22:23 -0700 | [diff] [blame] | 405 | static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk, |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 406 | __be16 loc_port, struct in6_addr *loc_addr, |
| 407 | __be16 rmt_port, struct in6_addr *rmt_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | int dif) |
| 409 | { |
Eric Dumazet | 88ab193 | 2008-11-16 19:39:21 -0800 | [diff] [blame] | 410 | struct hlist_nulls_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | struct sock *s = sk; |
| 412 | unsigned short num = ntohs(loc_port); |
| 413 | |
Eric Dumazet | 88ab193 | 2008-11-16 19:39:21 -0800 | [diff] [blame] | 414 | sk_nulls_for_each_from(s, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | struct inet_sock *inet = inet_sk(s); |
| 416 | |
Eric Dumazet | 920a461 | 2008-11-01 21:22:23 -0700 | [diff] [blame] | 417 | if (!net_eq(sock_net(s), net)) |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 418 | continue; |
| 419 | |
Eric Dumazet | 95f30b3 | 2007-02-09 15:44:52 -0800 | [diff] [blame] | 420 | if (s->sk_hash == num && s->sk_family == PF_INET6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | struct ipv6_pinfo *np = inet6_sk(s); |
| 422 | if (inet->dport) { |
| 423 | if (inet->dport != rmt_port) |
| 424 | continue; |
| 425 | } |
| 426 | if (!ipv6_addr_any(&np->daddr) && |
| 427 | !ipv6_addr_equal(&np->daddr, rmt_addr)) |
| 428 | continue; |
| 429 | |
| 430 | if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) |
| 431 | continue; |
| 432 | |
| 433 | if (!ipv6_addr_any(&np->rcv_saddr)) { |
David L Stevens | 40796c5 | 2005-09-14 21:10:20 -0700 | [diff] [blame] | 434 | if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) |
| 435 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 437 | if (!inet6_mc_check(s, loc_addr, rmt_addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | continue; |
| 439 | return s; |
| 440 | } |
| 441 | } |
| 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Note: called only from the BH handler context, |
| 447 | * so we don't need to lock the hashes. |
| 448 | */ |
Pavel Emelyanov | e316349 | 2008-06-16 17:12:11 -0700 | [diff] [blame] | 449 | static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, |
| 450 | struct in6_addr *saddr, struct in6_addr *daddr, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 451 | struct udp_table *udptable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
| 453 | struct sock *sk, *sk2; |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 454 | const struct udphdr *uh = udp_hdr(skb); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 455 | struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | int dif; |
| 457 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 458 | spin_lock(&hslot->lock); |
Eric Dumazet | 88ab193 | 2008-11-16 19:39:21 -0800 | [diff] [blame] | 459 | sk = sk_nulls_head(&hslot->head); |
YOSHIFUJI Hideaki | f2776ff | 2006-11-21 17:41:56 -0800 | [diff] [blame] | 460 | dif = inet6_iif(skb); |
Eric Dumazet | 920a461 | 2008-11-01 21:22:23 -0700 | [diff] [blame] | 461 | sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (!sk) { |
| 463 | kfree_skb(skb); |
| 464 | goto out; |
| 465 | } |
| 466 | |
| 467 | sk2 = sk; |
Eric Dumazet | 88ab193 | 2008-11-16 19:39:21 -0800 | [diff] [blame] | 468 | while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | uh->source, saddr, dif))) { |
| 470 | struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 471 | if (buff) { |
Herbert Xu | d97106e | 2008-08-09 00:35:05 -0700 | [diff] [blame] | 472 | bh_lock_sock(sk2); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 473 | if (!sock_owned_by_user(sk2)) |
| 474 | udpv6_queue_rcv_skb(sk2, buff); |
| 475 | else |
| 476 | sk_add_backlog(sk2, buff); |
| 477 | bh_unlock_sock(sk2); |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
Herbert Xu | d97106e | 2008-08-09 00:35:05 -0700 | [diff] [blame] | 480 | bh_lock_sock(sk); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 481 | if (!sock_owned_by_user(sk)) |
| 482 | udpv6_queue_rcv_skb(sk, skb); |
| 483 | else |
| 484 | sk_add_backlog(sk, skb); |
| 485 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | out: |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 487 | spin_unlock(&hslot->lock); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 488 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 491 | static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, |
| 492 | int proto) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 493 | { |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 494 | int err; |
| 495 | |
| 496 | UDP_SKB_CB(skb)->partial_cov = 0; |
| 497 | UDP_SKB_CB(skb)->cscov = skb->len; |
| 498 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 499 | if (proto == IPPROTO_UDPLITE) { |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 500 | err = udplite_checksum_init(skb, uh); |
| 501 | if (err) |
| 502 | return err; |
| 503 | } |
| 504 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 505 | if (uh->check == 0) { |
| 506 | /* RFC 2460 section 8.1 says that we SHOULD log |
| 507 | this error. Well, it is reasonable. |
| 508 | */ |
| 509 | LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n"); |
| 510 | return 1; |
| 511 | } |
| 512 | if (skb->ip_summed == CHECKSUM_COMPLETE && |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 513 | !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 514 | skb->len, proto, skb->csum)) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 515 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 516 | |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 517 | if (!skb_csum_unnecessary(skb)) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 518 | skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 519 | &ipv6_hdr(skb)->daddr, |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 520 | skb->len, proto, 0)); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 521 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 522 | return 0; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 525 | int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 526 | int proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct sock *sk; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 529 | struct udphdr *uh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | struct net_device *dev = skb->dev; |
| 531 | struct in6_addr *saddr, *daddr; |
| 532 | u32 ulen = 0; |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 533 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
| 535 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
| 536 | goto short_packet; |
| 537 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 538 | saddr = &ipv6_hdr(skb)->saddr; |
| 539 | daddr = &ipv6_hdr(skb)->daddr; |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 540 | uh = udp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | ulen = ntohs(uh->len); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 543 | if (ulen > skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | goto short_packet; |
| 545 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 546 | if (proto == IPPROTO_UDP) { |
| 547 | /* UDP validates ulen. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 549 | /* Check for jumbo payload */ |
| 550 | if (ulen == 0) |
| 551 | ulen = skb->len; |
| 552 | |
| 553 | if (ulen < sizeof(*uh)) |
| 554 | goto short_packet; |
| 555 | |
| 556 | if (ulen < skb->len) { |
| 557 | if (pskb_trim_rcsum(skb, ulen)) |
| 558 | goto short_packet; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 559 | saddr = &ipv6_hdr(skb)->saddr; |
| 560 | daddr = &ipv6_hdr(skb)->daddr; |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 561 | uh = udp_hdr(skb); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 565 | if (udp6_csum_init(skb, uh, proto)) |
| 566 | goto discard; |
| 567 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 568 | /* |
| 569 | * Multicast receive code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 571 | if (ipv6_addr_is_multicast(daddr)) |
Pavel Emelyanov | e316349 | 2008-06-16 17:12:11 -0700 | [diff] [blame] | 572 | return __udp6_lib_mcast_deliver(net, skb, |
| 573 | saddr, daddr, udptable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | /* Unicast */ |
| 576 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 577 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | * check socket cache ... must talk to Alan about his plans |
| 579 | * for sock caches... i'll skip this for now. |
| 580 | */ |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 581 | sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | if (sk == NULL) { |
| 584 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 585 | goto discard; |
| 586 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 587 | if (udp_lib_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | goto discard; |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 589 | UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, |
| 590 | proto == IPPROTO_UDPLITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev); |
| 593 | |
| 594 | kfree_skb(skb); |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 595 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | /* deliver */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 599 | |
Herbert Xu | d97106e | 2008-08-09 00:35:05 -0700 | [diff] [blame] | 600 | bh_lock_sock(sk); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 601 | if (!sock_owned_by_user(sk)) |
| 602 | udpv6_queue_rcv_skb(sk, skb); |
| 603 | else |
| 604 | sk_add_backlog(sk, skb); |
| 605 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | sock_put(sk); |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 607 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 609 | short_packet: |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 610 | LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n", |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 611 | proto == IPPROTO_UDPLITE ? "-Lite" : "", |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 612 | ulen, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | discard: |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 615 | UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | kfree_skb(skb); |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 617 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 619 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 620 | static __inline__ int udpv6_rcv(struct sk_buff *skb) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 621 | { |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 622 | return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | /* |
| 626 | * Throw away all pending data and cancel the corking. Socket is locked. |
| 627 | */ |
| 628 | static void udp_v6_flush_pending_frames(struct sock *sk) |
| 629 | { |
| 630 | struct udp_sock *up = udp_sk(sk); |
| 631 | |
Denis V. Lunev | 36d926b | 2008-06-04 15:49:07 +0400 | [diff] [blame] | 632 | if (up->pending == AF_INET) |
| 633 | udp_flush_pending_frames(sk); |
| 634 | else if (up->pending) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | up->len = 0; |
| 636 | up->pending = 0; |
| 637 | ip6_flush_pending_frames(sk); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Sridhar Samudrala | 493c6be | 2009-07-09 08:09:54 +0000 | [diff] [blame] | 641 | /** |
| 642 | * udp6_hwcsum_outgoing - handle outgoing HW checksumming |
| 643 | * @sk: socket we are sending on |
| 644 | * @skb: sk_buff containing the filled-in UDP header |
| 645 | * (checksum field must be zeroed out) |
| 646 | */ |
| 647 | static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, |
| 648 | const struct in6_addr *saddr, |
| 649 | const struct in6_addr *daddr, int len) |
| 650 | { |
| 651 | unsigned int offset; |
| 652 | struct udphdr *uh = udp_hdr(skb); |
| 653 | __wsum csum = 0; |
| 654 | |
| 655 | if (skb_queue_len(&sk->sk_write_queue) == 1) { |
| 656 | /* Only one fragment on the socket. */ |
| 657 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 658 | skb->csum_offset = offsetof(struct udphdr, check); |
| 659 | uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); |
| 660 | } else { |
| 661 | /* |
| 662 | * HW-checksum won't work as there are two or more |
| 663 | * fragments on the socket so that all csums of sk_buffs |
| 664 | * should be together |
| 665 | */ |
| 666 | offset = skb_transport_offset(skb); |
| 667 | skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); |
| 668 | |
| 669 | skb->ip_summed = CHECKSUM_NONE; |
| 670 | |
| 671 | skb_queue_walk(&sk->sk_write_queue, skb) { |
| 672 | csum = csum_add(csum, skb->csum); |
| 673 | } |
| 674 | |
| 675 | uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, |
| 676 | csum); |
| 677 | if (uh->check == 0) |
| 678 | uh->check = CSUM_MANGLED_0; |
| 679 | } |
| 680 | } |
| 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | /* |
| 683 | * Sending |
| 684 | */ |
| 685 | |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 686 | static int udp_v6_push_pending_frames(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
| 688 | struct sk_buff *skb; |
| 689 | struct udphdr *uh; |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 690 | struct udp_sock *up = udp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | struct inet_sock *inet = inet_sk(sk); |
| 692 | struct flowi *fl = &inet->cork.fl; |
| 693 | int err = 0; |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 694 | int is_udplite = IS_UDPLITE(sk); |
Al Viro | 868c86b | 2006-11-14 21:35:48 -0800 | [diff] [blame] | 695 | __wsum csum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | /* Grab the skbuff where UDP header space exists. */ |
| 698 | if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) |
| 699 | goto out; |
| 700 | |
| 701 | /* |
| 702 | * Create a UDP header |
| 703 | */ |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 704 | uh = udp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | uh->source = fl->fl_ip_sport; |
| 706 | uh->dest = fl->fl_ip_dport; |
| 707 | uh->len = htons(up->len); |
| 708 | uh->check = 0; |
| 709 | |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 710 | if (is_udplite) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 711 | csum = udplite_csum_outgoing(sk, skb); |
Sridhar Samudrala | 493c6be | 2009-07-09 08:09:54 +0000 | [diff] [blame] | 712 | else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ |
| 713 | udp6_hwcsum_outgoing(sk, skb, &fl->fl6_src, &fl->fl6_dst, |
| 714 | up->len); |
| 715 | goto send; |
| 716 | } else |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 717 | csum = udp_csum_outgoing(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 719 | /* add protocol-dependent pseudo-header */ |
| 720 | uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst, |
| 721 | up->len, fl->proto, csum ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | if (uh->check == 0) |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 723 | uh->check = CSUM_MANGLED_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Sridhar Samudrala | 493c6be | 2009-07-09 08:09:54 +0000 | [diff] [blame] | 725 | send: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | err = ip6_push_pending_frames(sk); |
| 727 | out: |
| 728 | up->len = 0; |
| 729 | up->pending = 0; |
YOSHIFUJI Hideaki | cd562c9 | 2007-09-14 17:15:01 -0700 | [diff] [blame] | 730 | if (!err) |
Pavel Emelyanov | 235b9f7 | 2008-07-05 21:19:20 -0700 | [diff] [blame] | 731 | UDP6_INC_STATS_USER(sock_net(sk), |
| 732 | UDP_MIB_OUTDATAGRAMS, is_udplite); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | return err; |
| 734 | } |
| 735 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 736 | int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | struct msghdr *msg, size_t len) |
| 738 | { |
| 739 | struct ipv6_txoptions opt_space; |
| 740 | struct udp_sock *up = udp_sk(sk); |
| 741 | struct inet_sock *inet = inet_sk(sk); |
| 742 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 743 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name; |
| 744 | struct in6_addr *daddr, *final_p = NULL, final; |
| 745 | struct ipv6_txoptions *opt = NULL; |
| 746 | struct ip6_flowlabel *flowlabel = NULL; |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 747 | struct flowi fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | struct dst_entry *dst; |
| 749 | int addr_len = msg->msg_namelen; |
| 750 | int ulen = len; |
| 751 | int hlimit = -1; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 752 | int tclass = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; |
| 754 | int err; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 755 | int connected = 0; |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 756 | int is_udplite = IS_UDPLITE(sk); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 757 | int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | /* destination address check */ |
| 760 | if (sin6) { |
| 761 | if (addr_len < offsetof(struct sockaddr, sa_data)) |
| 762 | return -EINVAL; |
| 763 | |
| 764 | switch (sin6->sin6_family) { |
| 765 | case AF_INET6: |
| 766 | if (addr_len < SIN6_LEN_RFC2133) |
| 767 | return -EINVAL; |
| 768 | daddr = &sin6->sin6_addr; |
| 769 | break; |
| 770 | case AF_INET: |
| 771 | goto do_udp_sendmsg; |
| 772 | case AF_UNSPEC: |
| 773 | msg->msg_name = sin6 = NULL; |
| 774 | msg->msg_namelen = addr_len = 0; |
| 775 | daddr = NULL; |
| 776 | break; |
| 777 | default: |
| 778 | return -EINVAL; |
| 779 | } |
| 780 | } else if (!up->pending) { |
| 781 | if (sk->sk_state != TCP_ESTABLISHED) |
| 782 | return -EDESTADDRREQ; |
| 783 | daddr = &np->daddr; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 784 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | daddr = NULL; |
| 786 | |
| 787 | if (daddr) { |
Brian Haley | e773e4f | 2007-08-24 23:16:08 -0700 | [diff] [blame] | 788 | if (ipv6_addr_v4mapped(daddr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | struct sockaddr_in sin; |
| 790 | sin.sin_family = AF_INET; |
| 791 | sin.sin_port = sin6 ? sin6->sin6_port : inet->dport; |
| 792 | sin.sin_addr.s_addr = daddr->s6_addr32[3]; |
| 793 | msg->msg_name = &sin; |
| 794 | msg->msg_namelen = sizeof(sin); |
| 795 | do_udp_sendmsg: |
| 796 | if (__ipv6_only_sock(sk)) |
| 797 | return -ENETUNREACH; |
| 798 | return udp_sendmsg(iocb, sk, msg, len); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | if (up->pending == AF_INET) |
| 803 | return udp_sendmsg(iocb, sk, msg, len); |
| 804 | |
| 805 | /* Rough check on arithmetic overflow, |
YOSHIFUJI Hideaki | b59e139 | 2007-03-30 14:45:35 -0700 | [diff] [blame] | 806 | better check is made in ip6_append_data(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | */ |
| 808 | if (len > INT_MAX - sizeof(struct udphdr)) |
| 809 | return -EMSGSIZE; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | if (up->pending) { |
| 812 | /* |
| 813 | * There are pending frames. |
| 814 | * The socket lock must be held while it's corked. |
| 815 | */ |
| 816 | lock_sock(sk); |
| 817 | if (likely(up->pending)) { |
| 818 | if (unlikely(up->pending != AF_INET6)) { |
| 819 | release_sock(sk); |
| 820 | return -EAFNOSUPPORT; |
| 821 | } |
| 822 | dst = NULL; |
| 823 | goto do_append_data; |
| 824 | } |
| 825 | release_sock(sk); |
| 826 | } |
| 827 | ulen += sizeof(struct udphdr); |
| 828 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 829 | memset(&fl, 0, sizeof(fl)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | if (sin6) { |
| 832 | if (sin6->sin6_port == 0) |
| 833 | return -EINVAL; |
| 834 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 835 | fl.fl_ip_dport = sin6->sin6_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | daddr = &sin6->sin6_addr; |
| 837 | |
| 838 | if (np->sndflow) { |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 839 | fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; |
| 840 | if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) { |
| 841 | flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | if (flowlabel == NULL) |
| 843 | return -EINVAL; |
| 844 | daddr = &flowlabel->dst; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * Otherwise it will be difficult to maintain |
| 850 | * sk->sk_dst_cache. |
| 851 | */ |
| 852 | if (sk->sk_state == TCP_ESTABLISHED && |
| 853 | ipv6_addr_equal(daddr, &np->daddr)) |
| 854 | daddr = &np->daddr; |
| 855 | |
| 856 | if (addr_len >= sizeof(struct sockaddr_in6) && |
| 857 | sin6->sin6_scope_id && |
| 858 | ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 859 | fl.oif = sin6->sin6_scope_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } else { |
| 861 | if (sk->sk_state != TCP_ESTABLISHED) |
| 862 | return -EDESTADDRREQ; |
| 863 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 864 | fl.fl_ip_dport = inet->dport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | daddr = &np->daddr; |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 866 | fl.fl6_flowlabel = np->flow_label; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 867 | connected = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 870 | if (!fl.oif) |
| 871 | fl.oif = sk->sk_bound_dev_if; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Yang Hongyang | 9f690db | 2008-12-16 02:08:29 -0800 | [diff] [blame] | 873 | if (!fl.oif) |
| 874 | fl.oif = np->sticky_pktinfo.ipi6_ifindex; |
| 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | if (msg->msg_controllen) { |
| 877 | opt = &opt_space; |
| 878 | memset(opt, 0, sizeof(struct ipv6_txoptions)); |
| 879 | opt->tot_len = sizeof(*opt); |
| 880 | |
YOSHIFUJI Hideaki | 91e1908 | 2008-06-04 13:02:49 +0900 | [diff] [blame] | 881 | err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | if (err < 0) { |
| 883 | fl6_sock_release(flowlabel); |
| 884 | return err; |
| 885 | } |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 886 | if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { |
| 887 | flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | if (flowlabel == NULL) |
| 889 | return -EINVAL; |
| 890 | } |
| 891 | if (!(opt->opt_nflen|opt->opt_flen)) |
| 892 | opt = NULL; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 893 | connected = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | if (opt == NULL) |
| 896 | opt = np->opt; |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 897 | if (flowlabel) |
| 898 | opt = fl6_merge_options(&opt_space, flowlabel, opt); |
| 899 | opt = ipv6_fixup_options(&opt_space, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 901 | fl.proto = sk->sk_protocol; |
Brian Haley | 876c7f4 | 2008-04-11 00:38:24 -0400 | [diff] [blame] | 902 | if (!ipv6_addr_any(daddr)) |
| 903 | ipv6_addr_copy(&fl.fl6_dst, daddr); |
| 904 | else |
| 905 | fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 906 | if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr)) |
| 907 | ipv6_addr_copy(&fl.fl6_src, &np->saddr); |
| 908 | fl.fl_ip_sport = inet->sport; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | /* merge ip6_build_xmit from ip6_output */ |
| 911 | if (opt && opt->srcrt) { |
| 912 | struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt; |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 913 | ipv6_addr_copy(&final, &fl.fl6_dst); |
| 914 | ipv6_addr_copy(&fl.fl6_dst, rt0->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | final_p = &final; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 916 | connected = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 919 | if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) { |
| 920 | fl.oif = np->mcast_oif; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 921 | connected = 0; |
| 922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 924 | security_sk_classify_flow(sk, &fl); |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 925 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 926 | err = ip6_sk_dst_lookup(sk, &dst, &fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | if (err) |
| 928 | goto out; |
| 929 | if (final_p) |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 930 | ipv6_addr_copy(&fl.fl6_dst, final_p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 932 | err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT); |
| 933 | if (err < 0) { |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 934 | if (err == -EREMOTE) |
| 935 | err = ip6_dst_blackhole(sk, &dst, &fl); |
| 936 | if (err < 0) |
| 937 | goto out; |
| 938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
| 940 | if (hlimit < 0) { |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 941 | if (ipv6_addr_is_multicast(&fl.fl6_dst)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | hlimit = np->mcast_hops; |
| 943 | else |
| 944 | hlimit = np->hop_limit; |
| 945 | if (hlimit < 0) |
YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 946 | hlimit = ip6_dst_hoplimit(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Gerrit Renker | e651f03 | 2009-08-09 08:12:48 +0000 | [diff] [blame] | 949 | if (tclass < 0) |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 950 | tclass = np->tclass; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | if (msg->msg_flags&MSG_CONFIRM) |
| 953 | goto do_confirm; |
| 954 | back_from_confirm: |
| 955 | |
| 956 | lock_sock(sk); |
| 957 | if (unlikely(up->pending)) { |
| 958 | /* The socket is already corked while preparing it. */ |
| 959 | /* ... which is an evident application bug. --ANK */ |
| 960 | release_sock(sk); |
| 961 | |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 962 | LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | err = -EINVAL; |
| 964 | goto out; |
| 965 | } |
| 966 | |
| 967 | up->pending = AF_INET6; |
| 968 | |
| 969 | do_append_data: |
| 970 | up->len += ulen; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 971 | getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; |
| 972 | err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen, |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 973 | sizeof(struct udphdr), hlimit, tclass, opt, &fl, |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 974 | (struct rt6_info*)dst, |
| 975 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | if (err) |
| 977 | udp_v6_flush_pending_frames(sk); |
| 978 | else if (!corkreq) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 979 | err = udp_v6_push_pending_frames(sk); |
Herbert Xu | 1e0c14f | 2006-10-03 14:35:49 -0700 | [diff] [blame] | 980 | else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) |
| 981 | up->pending = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
David S. Miller | a5e7c21 | 2005-10-03 14:21:58 -0700 | [diff] [blame] | 983 | if (dst) { |
| 984 | if (connected) { |
| 985 | ip6_dst_store(sk, dst, |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 986 | ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ? |
YOSHIFUJI Hideaki | 8e1ef0a | 2006-08-29 17:15:09 -0700 | [diff] [blame] | 987 | &np->daddr : NULL, |
| 988 | #ifdef CONFIG_IPV6_SUBTREES |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 989 | ipv6_addr_equal(&fl.fl6_src, &np->saddr) ? |
YOSHIFUJI Hideaki | 8e1ef0a | 2006-08-29 17:15:09 -0700 | [diff] [blame] | 990 | &np->saddr : |
| 991 | #endif |
| 992 | NULL); |
David S. Miller | a5e7c21 | 2005-10-03 14:21:58 -0700 | [diff] [blame] | 993 | } else { |
| 994 | dst_release(dst); |
| 995 | } |
YOSHIFUJI Hideaki | a3c9608 | 2008-06-04 01:30:25 +0900 | [diff] [blame] | 996 | dst = NULL; |
David S. Miller | a5e7c21 | 2005-10-03 14:21:58 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | if (err > 0) |
| 1000 | err = np->recverr ? net_xmit_errno(err) : 0; |
| 1001 | release_sock(sk); |
| 1002 | out: |
YOSHIFUJI Hideaki | a3c9608 | 2008-06-04 01:30:25 +0900 | [diff] [blame] | 1003 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | fl6_sock_release(flowlabel); |
YOSHIFUJI Hideaki | cd562c9 | 2007-09-14 17:15:01 -0700 | [diff] [blame] | 1005 | if (!err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | return len; |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 1007 | /* |
| 1008 | * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting |
| 1009 | * ENOBUFS might not be good (it's not tunable per se), but otherwise |
| 1010 | * we don't have a good statistic (IpOutDiscards but it can be too many |
| 1011 | * things). We could add another new stat but at least for now that |
| 1012 | * seems like overkill. |
| 1013 | */ |
| 1014 | if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { |
Pavel Emelyanov | 235b9f7 | 2008-07-05 21:19:20 -0700 | [diff] [blame] | 1015 | UDP6_INC_STATS_USER(sock_net(sk), |
| 1016 | UDP_MIB_SNDBUFERRORS, is_udplite); |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 1017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | return err; |
| 1019 | |
| 1020 | do_confirm: |
| 1021 | dst_confirm(dst); |
| 1022 | if (!(msg->msg_flags&MSG_PROBE) || len) |
| 1023 | goto back_from_confirm; |
| 1024 | err = 0; |
| 1025 | goto out; |
| 1026 | } |
| 1027 | |
Brian Haley | 7d06b2e | 2008-06-14 17:04:49 -0700 | [diff] [blame] | 1028 | void udpv6_destroy_sock(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | { |
| 1030 | lock_sock(sk); |
| 1031 | udp_v6_flush_pending_frames(sk); |
| 1032 | release_sock(sk); |
| 1033 | |
| 1034 | inet6_destroy_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Socket option code for UDP |
| 1039 | */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1040 | int udpv6_setsockopt(struct sock *sk, int level, int optname, |
| 1041 | char __user *optval, int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1042 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1043 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 1044 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, |
| 1045 | udp_v6_push_pending_frames); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1046 | return ipv6_setsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | #ifdef CONFIG_COMPAT |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1050 | int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, |
| 1051 | char __user *optval, int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1052 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1053 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 1054 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, |
| 1055 | udp_v6_push_pending_frames); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1056 | return compat_ipv6_setsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1057 | } |
| 1058 | #endif |
| 1059 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1060 | int udpv6_getsockopt(struct sock *sk, int level, int optname, |
| 1061 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1062 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1063 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 1064 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1065 | return ipv6_getsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | #ifdef CONFIG_COMPAT |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1069 | int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, |
| 1070 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1071 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1072 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 1073 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1074 | return compat_ipv6_getsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1075 | } |
| 1076 | #endif |
| 1077 | |
Sridhar Samudrala | ba73542 | 2009-07-09 08:10:04 +0000 | [diff] [blame] | 1078 | static int udp6_ufo_send_check(struct sk_buff *skb) |
| 1079 | { |
| 1080 | struct ipv6hdr *ipv6h; |
| 1081 | struct udphdr *uh; |
| 1082 | |
| 1083 | if (!pskb_may_pull(skb, sizeof(*uh))) |
| 1084 | return -EINVAL; |
| 1085 | |
| 1086 | ipv6h = ipv6_hdr(skb); |
| 1087 | uh = udp_hdr(skb); |
| 1088 | |
| 1089 | uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len, |
| 1090 | IPPROTO_UDP, 0); |
| 1091 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 1092 | skb->csum_offset = offsetof(struct udphdr, check); |
| 1093 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, int features) |
| 1098 | { |
| 1099 | struct sk_buff *segs = ERR_PTR(-EINVAL); |
| 1100 | unsigned int mss; |
| 1101 | unsigned int unfrag_ip6hlen, unfrag_len; |
| 1102 | struct frag_hdr *fptr; |
| 1103 | u8 *mac_start, *prevhdr; |
| 1104 | u8 nexthdr; |
| 1105 | u8 frag_hdr_sz = sizeof(struct frag_hdr); |
| 1106 | int offset; |
| 1107 | __wsum csum; |
| 1108 | |
| 1109 | mss = skb_shinfo(skb)->gso_size; |
| 1110 | if (unlikely(skb->len <= mss)) |
| 1111 | goto out; |
| 1112 | |
| 1113 | if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) { |
| 1114 | /* Packet is from an untrusted source, reset gso_segs. */ |
| 1115 | int type = skb_shinfo(skb)->gso_type; |
| 1116 | |
| 1117 | if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) || |
| 1118 | !(type & (SKB_GSO_UDP)))) |
| 1119 | goto out; |
| 1120 | |
| 1121 | skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss); |
| 1122 | |
| 1123 | segs = NULL; |
| 1124 | goto out; |
| 1125 | } |
| 1126 | |
| 1127 | /* Do software UFO. Complete and fill in the UDP checksum as HW cannot |
| 1128 | * do checksum of UDP packets sent as multiple IP fragments. |
| 1129 | */ |
| 1130 | offset = skb->csum_start - skb_headroom(skb); |
| 1131 | csum = skb_checksum(skb, offset, skb->len- offset, 0); |
| 1132 | offset += skb->csum_offset; |
| 1133 | *(__sum16 *)(skb->data + offset) = csum_fold(csum); |
| 1134 | skb->ip_summed = CHECKSUM_NONE; |
| 1135 | |
| 1136 | /* Check if there is enough headroom to insert fragment header. */ |
| 1137 | if ((skb_headroom(skb) < frag_hdr_sz) && |
| 1138 | pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC)) |
| 1139 | goto out; |
| 1140 | |
| 1141 | /* Find the unfragmentable header and shift it left by frag_hdr_sz |
| 1142 | * bytes to insert fragment header. |
| 1143 | */ |
| 1144 | unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); |
| 1145 | nexthdr = *prevhdr; |
| 1146 | *prevhdr = NEXTHDR_FRAGMENT; |
| 1147 | unfrag_len = skb_network_header(skb) - skb_mac_header(skb) + |
| 1148 | unfrag_ip6hlen; |
| 1149 | mac_start = skb_mac_header(skb); |
| 1150 | memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len); |
| 1151 | |
| 1152 | skb->mac_header -= frag_hdr_sz; |
| 1153 | skb->network_header -= frag_hdr_sz; |
| 1154 | |
| 1155 | fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen); |
| 1156 | fptr->nexthdr = nexthdr; |
| 1157 | fptr->reserved = 0; |
| 1158 | ipv6_select_ident(fptr); |
| 1159 | |
| 1160 | /* Fragment the skb. ipv6 header and the remaining fields of the |
| 1161 | * fragment header are updated in ipv6_gso_segment() |
| 1162 | */ |
| 1163 | segs = skb_segment(skb, features); |
| 1164 | |
| 1165 | out: |
| 1166 | return segs; |
| 1167 | } |
| 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | static struct inet6_protocol udpv6_protocol = { |
| 1170 | .handler = udpv6_rcv, |
| 1171 | .err_handler = udpv6_err, |
Sridhar Samudrala | ba73542 | 2009-07-09 08:10:04 +0000 | [diff] [blame] | 1172 | .gso_send_check = udp6_ufo_send_check, |
| 1173 | .gso_segment = udp6_ufo_fragment, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 1175 | }; |
| 1176 | |
| 1177 | /* ------------------------------------------------------------------------ */ |
| 1178 | #ifdef CONFIG_PROC_FS |
| 1179 | |
| 1180 | static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket) |
| 1181 | { |
| 1182 | struct inet_sock *inet = inet_sk(sp); |
| 1183 | struct ipv6_pinfo *np = inet6_sk(sp); |
| 1184 | struct in6_addr *dest, *src; |
| 1185 | __u16 destp, srcp; |
| 1186 | |
| 1187 | dest = &np->daddr; |
| 1188 | src = &np->rcv_saddr; |
| 1189 | destp = ntohs(inet->dport); |
| 1190 | srcp = ntohs(inet->sport); |
| 1191 | seq_printf(seq, |
| 1192 | "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 1193 | "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | bucket, |
| 1195 | src->s6_addr32[0], src->s6_addr32[1], |
| 1196 | src->s6_addr32[2], src->s6_addr32[3], srcp, |
| 1197 | dest->s6_addr32[0], dest->s6_addr32[1], |
| 1198 | dest->s6_addr32[2], dest->s6_addr32[3], destp, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1199 | sp->sk_state, |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 1200 | sk_wmem_alloc_get(sp), |
| 1201 | sk_rmem_alloc_get(sp), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | 0, 0L, 0, |
| 1203 | sock_i_uid(sp), 0, |
| 1204 | sock_i_ino(sp), |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 1205 | atomic_read(&sp->sk_refcnt), sp, |
| 1206 | atomic_read(&sp->sk_drops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1209 | int udp6_seq_show(struct seq_file *seq, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | { |
| 1211 | if (v == SEQ_START_TOKEN) |
| 1212 | seq_printf(seq, |
| 1213 | " sl " |
| 1214 | "local_address " |
| 1215 | "remote_address " |
| 1216 | "st tx_queue rx_queue tr tm->when retrnsmt" |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 1217 | " uid timeout inode ref pointer drops\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | else |
| 1219 | udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket); |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | static struct udp_seq_afinfo udp6_seq_afinfo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | .name = "udp6", |
| 1225 | .family = AF_INET6, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 1226 | .udp_table = &udp_table, |
Denis V. Lunev | 4ad96d3 | 2008-03-28 18:25:53 -0700 | [diff] [blame] | 1227 | .seq_fops = { |
| 1228 | .owner = THIS_MODULE, |
| 1229 | }, |
Denis V. Lunev | dda6192 | 2008-03-28 18:24:26 -0700 | [diff] [blame] | 1230 | .seq_ops = { |
| 1231 | .show = udp6_seq_show, |
| 1232 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | }; |
| 1234 | |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1235 | int udp6_proc_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | { |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1237 | return udp_proc_register(net, &udp6_seq_afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1240 | void udp6_proc_exit(struct net *net) { |
| 1241 | udp_proc_unregister(net, &udp6_seq_afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
| 1243 | #endif /* CONFIG_PROC_FS */ |
| 1244 | |
| 1245 | /* ------------------------------------------------------------------------ */ |
| 1246 | |
| 1247 | struct proto udpv6_prot = { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1248 | .name = "UDPv6", |
| 1249 | .owner = THIS_MODULE, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1250 | .close = udp_lib_close, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1251 | .connect = ip6_datagram_connect, |
| 1252 | .disconnect = udp_disconnect, |
| 1253 | .ioctl = udp_ioctl, |
| 1254 | .destroy = udpv6_destroy_sock, |
| 1255 | .setsockopt = udpv6_setsockopt, |
| 1256 | .getsockopt = udpv6_getsockopt, |
| 1257 | .sendmsg = udpv6_sendmsg, |
| 1258 | .recvmsg = udpv6_recvmsg, |
| 1259 | .backlog_rcv = udpv6_queue_rcv_skb, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1260 | .hash = udp_lib_hash, |
| 1261 | .unhash = udp_lib_unhash, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1262 | .get_port = udp_v6_get_port, |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 1263 | .memory_allocated = &udp_memory_allocated, |
| 1264 | .sysctl_mem = sysctl_udp_mem, |
| 1265 | .sysctl_wmem = &sysctl_udp_wmem_min, |
| 1266 | .sysctl_rmem = &sysctl_udp_rmem_min, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1267 | .obj_size = sizeof(struct udp6_sock), |
Eric Dumazet | 271b72c | 2008-10-29 02:11:14 -0700 | [diff] [blame] | 1268 | .slab_flags = SLAB_DESTROY_BY_RCU, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 1269 | .h.udp_table = &udp_table, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1270 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1271 | .compat_setsockopt = compat_udpv6_setsockopt, |
| 1272 | .compat_getsockopt = compat_udpv6_getsockopt, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1273 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | }; |
| 1275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | static struct inet_protosw udpv6_protosw = { |
| 1277 | .type = SOCK_DGRAM, |
| 1278 | .protocol = IPPROTO_UDP, |
| 1279 | .prot = &udpv6_prot, |
| 1280 | .ops = &inet6_dgram_ops, |
| 1281 | .capability =-1, |
| 1282 | .no_check = UDP_CSUM_DEFAULT, |
| 1283 | .flags = INET_PROTOSW_PERMANENT, |
| 1284 | }; |
| 1285 | |
| 1286 | |
Daniel Lezcano | 7f4e486 | 2007-12-11 02:25:35 -0800 | [diff] [blame] | 1287 | int __init udpv6_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | { |
Daniel Lezcano | 7f4e486 | 2007-12-11 02:25:35 -0800 | [diff] [blame] | 1289 | int ret; |
| 1290 | |
| 1291 | ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); |
| 1292 | if (ret) |
| 1293 | goto out; |
| 1294 | |
| 1295 | ret = inet6_register_protosw(&udpv6_protosw); |
| 1296 | if (ret) |
| 1297 | goto out_udpv6_protocol; |
| 1298 | out: |
| 1299 | return ret; |
| 1300 | |
| 1301 | out_udpv6_protocol: |
| 1302 | inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); |
| 1303 | goto out; |
| 1304 | } |
| 1305 | |
Daniel Lezcano | 09f7709 | 2007-12-13 05:34:58 -0800 | [diff] [blame] | 1306 | void udpv6_exit(void) |
Daniel Lezcano | 7f4e486 | 2007-12-11 02:25:35 -0800 | [diff] [blame] | 1307 | { |
| 1308 | inet6_unregister_protosw(&udpv6_protosw); |
| 1309 | inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | } |