]> Git Repo - linux.git/blob - net/ipv6/udp.c
net: inet: Support UID-based routing in IP protocols.
[linux.git] / net / ipv6 / udp.c
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  *      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
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
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>
35 #include <linux/module.h>
36 #include <linux/skbuff.h>
37 #include <linux/slab.h>
38 #include <asm/uaccess.h>
39
40 #include <net/addrconf.h>
41 #include <net/ndisc.h>
42 #include <net/protocol.h>
43 #include <net/transp_v6.h>
44 #include <net/ip6_route.h>
45 #include <net/raw.h>
46 #include <net/tcp_states.h>
47 #include <net/ip6_checksum.h>
48 #include <net/xfrm.h>
49 #include <net/inet6_hashtables.h>
50 #include <net/busy_poll.h>
51 #include <net/sock_reuseport.h>
52
53 #include <linux/proc_fs.h>
54 #include <linux/seq_file.h>
55 #include <trace/events/skb.h>
56 #include "udp_impl.h"
57
58 static u32 udp6_ehashfn(const struct net *net,
59                         const struct in6_addr *laddr,
60                         const u16 lport,
61                         const struct in6_addr *faddr,
62                         const __be16 fport)
63 {
64         static u32 udp6_ehash_secret __read_mostly;
65         static u32 udp_ipv6_hash_secret __read_mostly;
66
67         u32 lhash, fhash;
68
69         net_get_random_once(&udp6_ehash_secret,
70                             sizeof(udp6_ehash_secret));
71         net_get_random_once(&udp_ipv6_hash_secret,
72                             sizeof(udp_ipv6_hash_secret));
73
74         lhash = (__force u32)laddr->s6_addr32[3];
75         fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
76
77         return __inet6_ehashfn(lhash, lport, fhash, fport,
78                                udp_ipv6_hash_secret + net_hash_mix(net));
79 }
80
81 static u32 udp6_portaddr_hash(const struct net *net,
82                               const struct in6_addr *addr6,
83                               unsigned int port)
84 {
85         unsigned int hash, mix = net_hash_mix(net);
86
87         if (ipv6_addr_any(addr6))
88                 hash = jhash_1word(0, mix);
89         else if (ipv6_addr_v4mapped(addr6))
90                 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
91         else
92                 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
93
94         return hash ^ port;
95 }
96
97 int udp_v6_get_port(struct sock *sk, unsigned short snum)
98 {
99         unsigned int hash2_nulladdr =
100                 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
101         unsigned int hash2_partial =
102                 udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
103
104         /* precompute partial secondary hash */
105         udp_sk(sk)->udp_portaddr_hash = hash2_partial;
106         return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
107 }
108
109 static void udp_v6_rehash(struct sock *sk)
110 {
111         u16 new_hash = udp6_portaddr_hash(sock_net(sk),
112                                           &sk->sk_v6_rcv_saddr,
113                                           inet_sk(sk)->inet_num);
114
115         udp_lib_rehash(sk, new_hash);
116 }
117
118 static int compute_score(struct sock *sk, struct net *net,
119                          const struct in6_addr *saddr, __be16 sport,
120                          const struct in6_addr *daddr, unsigned short hnum,
121                          int dif)
122 {
123         int score;
124         struct inet_sock *inet;
125
126         if (!net_eq(sock_net(sk), net) ||
127             udp_sk(sk)->udp_port_hash != hnum ||
128             sk->sk_family != PF_INET6)
129                 return -1;
130
131         score = 0;
132         inet = inet_sk(sk);
133
134         if (inet->inet_dport) {
135                 if (inet->inet_dport != sport)
136                         return -1;
137                 score++;
138         }
139
140         if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
141                 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
142                         return -1;
143                 score++;
144         }
145
146         if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
147                 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
148                         return -1;
149                 score++;
150         }
151
152         if (sk->sk_bound_dev_if) {
153                 if (sk->sk_bound_dev_if != dif)
154                         return -1;
155                 score++;
156         }
157
158         if (sk->sk_incoming_cpu == raw_smp_processor_id())
159                 score++;
160
161         return score;
162 }
163
164 /* called with rcu_read_lock() */
165 static struct sock *udp6_lib_lookup2(struct net *net,
166                 const struct in6_addr *saddr, __be16 sport,
167                 const struct in6_addr *daddr, unsigned int hnum, int dif,
168                 struct udp_hslot *hslot2,
169                 struct sk_buff *skb)
170 {
171         struct sock *sk, *result;
172         int score, badness, matches = 0, reuseport = 0;
173         u32 hash = 0;
174
175         result = NULL;
176         badness = -1;
177         udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
178                 score = compute_score(sk, net, saddr, sport,
179                                       daddr, hnum, dif);
180                 if (score > badness) {
181                         reuseport = sk->sk_reuseport;
182                         if (reuseport) {
183                                 hash = udp6_ehashfn(net, daddr, hnum,
184                                                     saddr, sport);
185
186                                 result = reuseport_select_sock(sk, hash, skb,
187                                                         sizeof(struct udphdr));
188                                 if (result)
189                                         return result;
190                                 matches = 1;
191                         }
192                         result = sk;
193                         badness = score;
194                 } else if (score == badness && reuseport) {
195                         matches++;
196                         if (reciprocal_scale(hash, matches) == 0)
197                                 result = sk;
198                         hash = next_pseudo_random32(hash);
199                 }
200         }
201         return result;
202 }
203
204 /* rcu_read_lock() must be held */
205 struct sock *__udp6_lib_lookup(struct net *net,
206                                       const struct in6_addr *saddr, __be16 sport,
207                                       const struct in6_addr *daddr, __be16 dport,
208                                       int dif, struct udp_table *udptable,
209                                       struct sk_buff *skb)
210 {
211         struct sock *sk, *result;
212         unsigned short hnum = ntohs(dport);
213         unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
214         struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
215         int score, badness, matches = 0, reuseport = 0;
216         u32 hash = 0;
217
218         if (hslot->count > 10) {
219                 hash2 = udp6_portaddr_hash(net, daddr, hnum);
220                 slot2 = hash2 & udptable->mask;
221                 hslot2 = &udptable->hash2[slot2];
222                 if (hslot->count < hslot2->count)
223                         goto begin;
224
225                 result = udp6_lib_lookup2(net, saddr, sport,
226                                           daddr, hnum, dif,
227                                           hslot2, skb);
228                 if (!result) {
229                         unsigned int old_slot2 = slot2;
230                         hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
231                         slot2 = hash2 & udptable->mask;
232                         /* avoid searching the same slot again. */
233                         if (unlikely(slot2 == old_slot2))
234                                 return result;
235
236                         hslot2 = &udptable->hash2[slot2];
237                         if (hslot->count < hslot2->count)
238                                 goto begin;
239
240                         result = udp6_lib_lookup2(net, saddr, sport,
241                                                   daddr, hnum, dif,
242                                                   hslot2, skb);
243                 }
244                 return result;
245         }
246 begin:
247         result = NULL;
248         badness = -1;
249         sk_for_each_rcu(sk, &hslot->head) {
250                 score = compute_score(sk, net, saddr, sport, daddr, hnum, dif);
251                 if (score > badness) {
252                         reuseport = sk->sk_reuseport;
253                         if (reuseport) {
254                                 hash = udp6_ehashfn(net, daddr, hnum,
255                                                     saddr, sport);
256                                 result = reuseport_select_sock(sk, hash, skb,
257                                                         sizeof(struct udphdr));
258                                 if (result)
259                                         return result;
260                                 matches = 1;
261                         }
262                         result = sk;
263                         badness = score;
264                 } else if (score == badness && reuseport) {
265                         matches++;
266                         if (reciprocal_scale(hash, matches) == 0)
267                                 result = sk;
268                         hash = next_pseudo_random32(hash);
269                 }
270         }
271         return result;
272 }
273 EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
274
275 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
276                                           __be16 sport, __be16 dport,
277                                           struct udp_table *udptable)
278 {
279         const struct ipv6hdr *iph = ipv6_hdr(skb);
280         struct sock *sk;
281
282         sk = skb_steal_sock(skb);
283         if (unlikely(sk))
284                 return sk;
285         return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
286                                  &iph->daddr, dport, inet6_iif(skb),
287                                  udptable, skb);
288 }
289
290 struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
291                                  __be16 sport, __be16 dport)
292 {
293         const struct ipv6hdr *iph = ipv6_hdr(skb);
294
295         return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
296                                  &iph->daddr, dport, inet6_iif(skb),
297                                  &udp_table, skb);
298 }
299 EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
300
301 /* Must be called under rcu_read_lock().
302  * Does increment socket refcount.
303  */
304 #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \
305     IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY)
306 struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
307                              const struct in6_addr *daddr, __be16 dport, int dif)
308 {
309         struct sock *sk;
310
311         sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
312                                 dif, &udp_table, NULL);
313         if (sk && !atomic_inc_not_zero(&sk->sk_refcnt))
314                 sk = NULL;
315         return sk;
316 }
317 EXPORT_SYMBOL_GPL(udp6_lib_lookup);
318 #endif
319
320 /*
321  *      This should be easy, if there is something there we
322  *      return it, otherwise we block.
323  */
324
325 int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
326                   int noblock, int flags, int *addr_len)
327 {
328         struct ipv6_pinfo *np = inet6_sk(sk);
329         struct inet_sock *inet = inet_sk(sk);
330         struct sk_buff *skb;
331         unsigned int ulen, copied;
332         int peeked, peeking, off;
333         int err;
334         int is_udplite = IS_UDPLITE(sk);
335         bool checksum_valid = false;
336         int is_udp4;
337
338         if (flags & MSG_ERRQUEUE)
339                 return ipv6_recv_error(sk, msg, len, addr_len);
340
341         if (np->rxpmtu && np->rxopt.bits.rxpmtu)
342                 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
343
344 try_again:
345         peeking = off = sk_peek_offset(sk, flags);
346         skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
347                                   &peeked, &off, &err);
348         if (!skb)
349                 return err;
350
351         ulen = skb->len;
352         copied = len;
353         if (copied > ulen - off)
354                 copied = ulen - off;
355         else if (copied < ulen)
356                 msg->msg_flags |= MSG_TRUNC;
357
358         is_udp4 = (skb->protocol == htons(ETH_P_IP));
359
360         /*
361          * If checksum is needed at all, try to do it while copying the
362          * data.  If the data is truncated, or if we only want a partial
363          * coverage checksum (UDP-Lite), do it before the copy.
364          */
365
366         if (copied < ulen || UDP_SKB_CB(skb)->partial_cov || peeking) {
367                 checksum_valid = !udp_lib_checksum_complete(skb);
368                 if (!checksum_valid)
369                         goto csum_copy_err;
370         }
371
372         if (checksum_valid || skb_csum_unnecessary(skb))
373                 err = skb_copy_datagram_msg(skb, off, msg, copied);
374         else {
375                 err = skb_copy_and_csum_datagram_msg(skb, off, msg);
376                 if (err == -EINVAL)
377                         goto csum_copy_err;
378         }
379         if (unlikely(err)) {
380                 if (!peeked) {
381                         atomic_inc(&sk->sk_drops);
382                         if (is_udp4)
383                                 UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
384                                               is_udplite);
385                         else
386                                 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
387                                                is_udplite);
388                 }
389                 kfree_skb(skb);
390                 return err;
391         }
392         if (!peeked) {
393                 if (is_udp4)
394                         UDP_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
395                                       is_udplite);
396                 else
397                         UDP6_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
398                                        is_udplite);
399         }
400
401         sock_recv_ts_and_drops(msg, sk, skb);
402
403         /* Copy the address. */
404         if (msg->msg_name) {
405                 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
406                 sin6->sin6_family = AF_INET6;
407                 sin6->sin6_port = udp_hdr(skb)->source;
408                 sin6->sin6_flowinfo = 0;
409
410                 if (is_udp4) {
411                         ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
412                                                &sin6->sin6_addr);
413                         sin6->sin6_scope_id = 0;
414                 } else {
415                         sin6->sin6_addr = ipv6_hdr(skb)->saddr;
416                         sin6->sin6_scope_id =
417                                 ipv6_iface_scope_id(&sin6->sin6_addr,
418                                                     inet6_iif(skb));
419                 }
420                 *addr_len = sizeof(*sin6);
421         }
422
423         if (np->rxopt.all)
424                 ip6_datagram_recv_common_ctl(sk, msg, skb);
425
426         if (is_udp4) {
427                 if (inet->cmsg_flags)
428                         ip_cmsg_recv_offset(msg, skb,
429                                             sizeof(struct udphdr), off);
430         } else {
431                 if (np->rxopt.all)
432                         ip6_datagram_recv_specific_ctl(sk, msg, skb);
433         }
434
435         err = copied;
436         if (flags & MSG_TRUNC)
437                 err = ulen;
438
439         skb_consume_udp(sk, skb, peeking ? -err : err);
440         return err;
441
442 csum_copy_err:
443         if (!__sk_queue_drop_skb(sk, skb, flags)) {
444                 if (is_udp4) {
445                         UDP_INC_STATS(sock_net(sk),
446                                       UDP_MIB_CSUMERRORS, is_udplite);
447                         UDP_INC_STATS(sock_net(sk),
448                                       UDP_MIB_INERRORS, is_udplite);
449                 } else {
450                         UDP6_INC_STATS(sock_net(sk),
451                                        UDP_MIB_CSUMERRORS, is_udplite);
452                         UDP6_INC_STATS(sock_net(sk),
453                                        UDP_MIB_INERRORS, is_udplite);
454                 }
455         }
456         kfree_skb(skb);
457
458         /* starting over for a new packet, but check if we need to yield */
459         cond_resched();
460         msg->msg_flags &= ~MSG_TRUNC;
461         goto try_again;
462 }
463
464 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
465                     u8 type, u8 code, int offset, __be32 info,
466                     struct udp_table *udptable)
467 {
468         struct ipv6_pinfo *np;
469         const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
470         const struct in6_addr *saddr = &hdr->saddr;
471         const struct in6_addr *daddr = &hdr->daddr;
472         struct udphdr *uh = (struct udphdr *)(skb->data+offset);
473         struct sock *sk;
474         int harderr;
475         int err;
476         struct net *net = dev_net(skb->dev);
477
478         sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
479                                inet6_iif(skb), udptable, skb);
480         if (!sk) {
481                 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
482                                   ICMP6_MIB_INERRORS);
483                 return;
484         }
485
486         harderr = icmpv6_err_convert(type, code, &err);
487         np = inet6_sk(sk);
488
489         if (type == ICMPV6_PKT_TOOBIG) {
490                 if (!ip6_sk_accept_pmtu(sk))
491                         goto out;
492                 ip6_sk_update_pmtu(skb, sk, info);
493                 if (np->pmtudisc != IPV6_PMTUDISC_DONT)
494                         harderr = 1;
495         }
496         if (type == NDISC_REDIRECT) {
497                 ip6_sk_redirect(skb, sk);
498                 goto out;
499         }
500
501         if (!np->recverr) {
502                 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
503                         goto out;
504         } else {
505                 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
506         }
507
508         sk->sk_err = err;
509         sk->sk_error_report(sk);
510 out:
511         return;
512 }
513
514 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
515 {
516         int rc;
517
518         if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
519                 sock_rps_save_rxhash(sk, skb);
520                 sk_mark_napi_id(sk, skb);
521                 sk_incoming_cpu_update(sk);
522         }
523
524         rc = __udp_enqueue_schedule_skb(sk, skb);
525         if (rc < 0) {
526                 int is_udplite = IS_UDPLITE(sk);
527
528                 /* Note that an ENOMEM error is charged twice */
529                 if (rc == -ENOMEM)
530                         UDP6_INC_STATS(sock_net(sk),
531                                          UDP_MIB_RCVBUFERRORS, is_udplite);
532                 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
533                 kfree_skb(skb);
534                 return -1;
535         }
536
537         return 0;
538 }
539
540 static __inline__ void udpv6_err(struct sk_buff *skb,
541                                  struct inet6_skb_parm *opt, u8 type,
542                                  u8 code, int offset, __be32 info)
543 {
544         __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
545 }
546
547 static struct static_key udpv6_encap_needed __read_mostly;
548 void udpv6_encap_enable(void)
549 {
550         if (!static_key_enabled(&udpv6_encap_needed))
551                 static_key_slow_inc(&udpv6_encap_needed);
552 }
553 EXPORT_SYMBOL(udpv6_encap_enable);
554
555 int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
556 {
557         struct udp_sock *up = udp_sk(sk);
558         int is_udplite = IS_UDPLITE(sk);
559
560         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
561                 goto drop;
562
563         if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
564                 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
565
566                 /*
567                  * This is an encapsulation socket so pass the skb to
568                  * the socket's udp_encap_rcv() hook. Otherwise, just
569                  * fall through and pass this up the UDP socket.
570                  * up->encap_rcv() returns the following value:
571                  * =0 if skb was successfully passed to the encap
572                  *    handler or was discarded by it.
573                  * >0 if skb should be passed on to UDP.
574                  * <0 if skb should be resubmitted as proto -N
575                  */
576
577                 /* if we're overly short, let UDP handle it */
578                 encap_rcv = ACCESS_ONCE(up->encap_rcv);
579                 if (encap_rcv) {
580                         int ret;
581
582                         /* Verify checksum before giving to encap */
583                         if (udp_lib_checksum_complete(skb))
584                                 goto csum_error;
585
586                         ret = encap_rcv(sk, skb);
587                         if (ret <= 0) {
588                                 __UDP_INC_STATS(sock_net(sk),
589                                                 UDP_MIB_INDATAGRAMS,
590                                                 is_udplite);
591                                 return -ret;
592                         }
593                 }
594
595                 /* FALLTHROUGH -- it's a UDP Packet */
596         }
597
598         /*
599          * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
600          */
601         if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
602
603                 if (up->pcrlen == 0) {          /* full coverage was set  */
604                         net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
605                                             UDP_SKB_CB(skb)->cscov, skb->len);
606                         goto drop;
607                 }
608                 if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
609                         net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
610                                             UDP_SKB_CB(skb)->cscov, up->pcrlen);
611                         goto drop;
612                 }
613         }
614
615         if (rcu_access_pointer(sk->sk_filter) &&
616             udp_lib_checksum_complete(skb))
617                 goto csum_error;
618
619         if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
620                 goto drop;
621
622         udp_csum_pull_header(skb);
623
624         skb_dst_drop(skb);
625
626         return __udpv6_queue_rcv_skb(sk, skb);
627
628 csum_error:
629         __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
630 drop:
631         __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
632         atomic_inc(&sk->sk_drops);
633         kfree_skb(skb);
634         return -1;
635 }
636
637 static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
638                                    __be16 loc_port, const struct in6_addr *loc_addr,
639                                    __be16 rmt_port, const struct in6_addr *rmt_addr,
640                                    int dif, unsigned short hnum)
641 {
642         struct inet_sock *inet = inet_sk(sk);
643
644         if (!net_eq(sock_net(sk), net))
645                 return false;
646
647         if (udp_sk(sk)->udp_port_hash != hnum ||
648             sk->sk_family != PF_INET6 ||
649             (inet->inet_dport && inet->inet_dport != rmt_port) ||
650             (!ipv6_addr_any(&sk->sk_v6_daddr) &&
651                     !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
652             (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
653             (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
654                     !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
655                 return false;
656         if (!inet6_mc_check(sk, loc_addr, rmt_addr))
657                 return false;
658         return true;
659 }
660
661 static void udp6_csum_zero_error(struct sk_buff *skb)
662 {
663         /* RFC 2460 section 8.1 says that we SHOULD log
664          * this error. Well, it is reasonable.
665          */
666         net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
667                             &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
668                             &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
669 }
670
671 /*
672  * Note: called only from the BH handler context,
673  * so we don't need to lock the hashes.
674  */
675 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
676                 const struct in6_addr *saddr, const struct in6_addr *daddr,
677                 struct udp_table *udptable, int proto)
678 {
679         struct sock *sk, *first = NULL;
680         const struct udphdr *uh = udp_hdr(skb);
681         unsigned short hnum = ntohs(uh->dest);
682         struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
683         unsigned int offset = offsetof(typeof(*sk), sk_node);
684         unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
685         int dif = inet6_iif(skb);
686         struct hlist_node *node;
687         struct sk_buff *nskb;
688
689         if (use_hash2) {
690                 hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
691                             udp_table.mask;
692                 hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask;
693 start_lookup:
694                 hslot = &udp_table.hash2[hash2];
695                 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
696         }
697
698         sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
699                 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
700                                             uh->source, saddr, dif, hnum))
701                         continue;
702                 /* If zero checksum and no_check is not on for
703                  * the socket then skip it.
704                  */
705                 if (!uh->check && !udp_sk(sk)->no_check6_rx)
706                         continue;
707                 if (!first) {
708                         first = sk;
709                         continue;
710                 }
711                 nskb = skb_clone(skb, GFP_ATOMIC);
712                 if (unlikely(!nskb)) {
713                         atomic_inc(&sk->sk_drops);
714                         __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
715                                          IS_UDPLITE(sk));
716                         __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
717                                          IS_UDPLITE(sk));
718                         continue;
719                 }
720
721                 if (udpv6_queue_rcv_skb(sk, nskb) > 0)
722                         consume_skb(nskb);
723         }
724
725         /* Also lookup *:port if we are using hash2 and haven't done so yet. */
726         if (use_hash2 && hash2 != hash2_any) {
727                 hash2 = hash2_any;
728                 goto start_lookup;
729         }
730
731         if (first) {
732                 if (udpv6_queue_rcv_skb(first, skb) > 0)
733                         consume_skb(skb);
734         } else {
735                 kfree_skb(skb);
736                 __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
737                                  proto == IPPROTO_UDPLITE);
738         }
739         return 0;
740 }
741
742 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
743                    int proto)
744 {
745         const struct in6_addr *saddr, *daddr;
746         struct net *net = dev_net(skb->dev);
747         struct udphdr *uh;
748         struct sock *sk;
749         u32 ulen = 0;
750
751         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
752                 goto discard;
753
754         saddr = &ipv6_hdr(skb)->saddr;
755         daddr = &ipv6_hdr(skb)->daddr;
756         uh = udp_hdr(skb);
757
758         ulen = ntohs(uh->len);
759         if (ulen > skb->len)
760                 goto short_packet;
761
762         if (proto == IPPROTO_UDP) {
763                 /* UDP validates ulen. */
764
765                 /* Check for jumbo payload */
766                 if (ulen == 0)
767                         ulen = skb->len;
768
769                 if (ulen < sizeof(*uh))
770                         goto short_packet;
771
772                 if (ulen < skb->len) {
773                         if (pskb_trim_rcsum(skb, ulen))
774                                 goto short_packet;
775                         saddr = &ipv6_hdr(skb)->saddr;
776                         daddr = &ipv6_hdr(skb)->daddr;
777                         uh = udp_hdr(skb);
778                 }
779         }
780
781         if (udp6_csum_init(skb, uh, proto))
782                 goto csum_error;
783
784         /*
785          *      Multicast receive code
786          */
787         if (ipv6_addr_is_multicast(daddr))
788                 return __udp6_lib_mcast_deliver(net, skb,
789                                 saddr, daddr, udptable, proto);
790
791         /* Unicast */
792
793         /*
794          * check socket cache ... must talk to Alan about his plans
795          * for sock caches... i'll skip this for now.
796          */
797         sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
798         if (sk) {
799                 int ret;
800
801                 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
802                         udp6_csum_zero_error(skb);
803                         goto csum_error;
804                 }
805
806                 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
807                         skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
808                                                  ip6_compute_pseudo);
809
810                 ret = udpv6_queue_rcv_skb(sk, skb);
811
812                 /* a return value > 0 means to resubmit the input */
813                 if (ret > 0)
814                         return ret;
815
816                 return 0;
817         }
818
819         if (!uh->check) {
820                 udp6_csum_zero_error(skb);
821                 goto csum_error;
822         }
823
824         if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
825                 goto discard;
826
827         if (udp_lib_checksum_complete(skb))
828                 goto csum_error;
829
830         __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
831         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
832
833         kfree_skb(skb);
834         return 0;
835
836 short_packet:
837         net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
838                             proto == IPPROTO_UDPLITE ? "-Lite" : "",
839                             saddr, ntohs(uh->source),
840                             ulen, skb->len,
841                             daddr, ntohs(uh->dest));
842         goto discard;
843 csum_error:
844         __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
845 discard:
846         __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
847         kfree_skb(skb);
848         return 0;
849 }
850
851 static __inline__ int udpv6_rcv(struct sk_buff *skb)
852 {
853         return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
854 }
855
856 /*
857  * Throw away all pending data and cancel the corking. Socket is locked.
858  */
859 static void udp_v6_flush_pending_frames(struct sock *sk)
860 {
861         struct udp_sock *up = udp_sk(sk);
862
863         if (up->pending == AF_INET)
864                 udp_flush_pending_frames(sk);
865         else if (up->pending) {
866                 up->len = 0;
867                 up->pending = 0;
868                 ip6_flush_pending_frames(sk);
869         }
870 }
871
872 /**
873  *      udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
874  *      @sk:    socket we are sending on
875  *      @skb:   sk_buff containing the filled-in UDP header
876  *              (checksum field must be zeroed out)
877  */
878 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
879                                  const struct in6_addr *saddr,
880                                  const struct in6_addr *daddr, int len)
881 {
882         unsigned int offset;
883         struct udphdr *uh = udp_hdr(skb);
884         struct sk_buff *frags = skb_shinfo(skb)->frag_list;
885         __wsum csum = 0;
886
887         if (!frags) {
888                 /* Only one fragment on the socket.  */
889                 skb->csum_start = skb_transport_header(skb) - skb->head;
890                 skb->csum_offset = offsetof(struct udphdr, check);
891                 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
892         } else {
893                 /*
894                  * HW-checksum won't work as there are two or more
895                  * fragments on the socket so that all csums of sk_buffs
896                  * should be together
897                  */
898                 offset = skb_transport_offset(skb);
899                 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
900
901                 skb->ip_summed = CHECKSUM_NONE;
902
903                 do {
904                         csum = csum_add(csum, frags->csum);
905                 } while ((frags = frags->next));
906
907                 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
908                                             csum);
909                 if (uh->check == 0)
910                         uh->check = CSUM_MANGLED_0;
911         }
912 }
913
914 /*
915  *      Sending
916  */
917
918 static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
919 {
920         struct sock *sk = skb->sk;
921         struct udphdr *uh;
922         int err = 0;
923         int is_udplite = IS_UDPLITE(sk);
924         __wsum csum = 0;
925         int offset = skb_transport_offset(skb);
926         int len = skb->len - offset;
927
928         /*
929          * Create a UDP header
930          */
931         uh = udp_hdr(skb);
932         uh->source = fl6->fl6_sport;
933         uh->dest = fl6->fl6_dport;
934         uh->len = htons(len);
935         uh->check = 0;
936
937         if (is_udplite)
938                 csum = udplite_csum(skb);
939         else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
940                 skb->ip_summed = CHECKSUM_NONE;
941                 goto send;
942         } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
943                 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
944                 goto send;
945         } else
946                 csum = udp_csum(skb);
947
948         /* add protocol-dependent pseudo-header */
949         uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
950                                     len, fl6->flowi6_proto, csum);
951         if (uh->check == 0)
952                 uh->check = CSUM_MANGLED_0;
953
954 send:
955         err = ip6_send_skb(skb);
956         if (err) {
957                 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
958                         UDP6_INC_STATS(sock_net(sk),
959                                        UDP_MIB_SNDBUFERRORS, is_udplite);
960                         err = 0;
961                 }
962         } else {
963                 UDP6_INC_STATS(sock_net(sk),
964                                UDP_MIB_OUTDATAGRAMS, is_udplite);
965         }
966         return err;
967 }
968
969 static int udp_v6_push_pending_frames(struct sock *sk)
970 {
971         struct sk_buff *skb;
972         struct udp_sock  *up = udp_sk(sk);
973         struct flowi6 fl6;
974         int err = 0;
975
976         if (up->pending == AF_INET)
977                 return udp_push_pending_frames(sk);
978
979         /* ip6_finish_skb will release the cork, so make a copy of
980          * fl6 here.
981          */
982         fl6 = inet_sk(sk)->cork.fl.u.ip6;
983
984         skb = ip6_finish_skb(sk);
985         if (!skb)
986                 goto out;
987
988         err = udp_v6_send_skb(skb, &fl6);
989
990 out:
991         up->len = 0;
992         up->pending = 0;
993         return err;
994 }
995
996 int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
997 {
998         struct ipv6_txoptions opt_space;
999         struct udp_sock *up = udp_sk(sk);
1000         struct inet_sock *inet = inet_sk(sk);
1001         struct ipv6_pinfo *np = inet6_sk(sk);
1002         DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1003         struct in6_addr *daddr, *final_p, final;
1004         struct ipv6_txoptions *opt = NULL;
1005         struct ipv6_txoptions *opt_to_free = NULL;
1006         struct ip6_flowlabel *flowlabel = NULL;
1007         struct flowi6 fl6;
1008         struct dst_entry *dst;
1009         struct ipcm6_cookie ipc6;
1010         int addr_len = msg->msg_namelen;
1011         int ulen = len;
1012         int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1013         int err;
1014         int connected = 0;
1015         int is_udplite = IS_UDPLITE(sk);
1016         int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1017         struct sockcm_cookie sockc;
1018
1019         ipc6.hlimit = -1;
1020         ipc6.tclass = -1;
1021         ipc6.dontfrag = -1;
1022
1023         /* destination address check */
1024         if (sin6) {
1025                 if (addr_len < offsetof(struct sockaddr, sa_data))
1026                         return -EINVAL;
1027
1028                 switch (sin6->sin6_family) {
1029                 case AF_INET6:
1030                         if (addr_len < SIN6_LEN_RFC2133)
1031                                 return -EINVAL;
1032                         daddr = &sin6->sin6_addr;
1033                         break;
1034                 case AF_INET:
1035                         goto do_udp_sendmsg;
1036                 case AF_UNSPEC:
1037                         msg->msg_name = sin6 = NULL;
1038                         msg->msg_namelen = addr_len = 0;
1039                         daddr = NULL;
1040                         break;
1041                 default:
1042                         return -EINVAL;
1043                 }
1044         } else if (!up->pending) {
1045                 if (sk->sk_state != TCP_ESTABLISHED)
1046                         return -EDESTADDRREQ;
1047                 daddr = &sk->sk_v6_daddr;
1048         } else
1049                 daddr = NULL;
1050
1051         if (daddr) {
1052                 if (ipv6_addr_v4mapped(daddr)) {
1053                         struct sockaddr_in sin;
1054                         sin.sin_family = AF_INET;
1055                         sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1056                         sin.sin_addr.s_addr = daddr->s6_addr32[3];
1057                         msg->msg_name = &sin;
1058                         msg->msg_namelen = sizeof(sin);
1059 do_udp_sendmsg:
1060                         if (__ipv6_only_sock(sk))
1061                                 return -ENETUNREACH;
1062                         return udp_sendmsg(sk, msg, len);
1063                 }
1064         }
1065
1066         if (up->pending == AF_INET)
1067                 return udp_sendmsg(sk, msg, len);
1068
1069         /* Rough check on arithmetic overflow,
1070            better check is made in ip6_append_data().
1071            */
1072         if (len > INT_MAX - sizeof(struct udphdr))
1073                 return -EMSGSIZE;
1074
1075         getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1076         if (up->pending) {
1077                 /*
1078                  * There are pending frames.
1079                  * The socket lock must be held while it's corked.
1080                  */
1081                 lock_sock(sk);
1082                 if (likely(up->pending)) {
1083                         if (unlikely(up->pending != AF_INET6)) {
1084                                 release_sock(sk);
1085                                 return -EAFNOSUPPORT;
1086                         }
1087                         dst = NULL;
1088                         goto do_append_data;
1089                 }
1090                 release_sock(sk);
1091         }
1092         ulen += sizeof(struct udphdr);
1093
1094         memset(&fl6, 0, sizeof(fl6));
1095
1096         if (sin6) {
1097                 if (sin6->sin6_port == 0)
1098                         return -EINVAL;
1099
1100                 fl6.fl6_dport = sin6->sin6_port;
1101                 daddr = &sin6->sin6_addr;
1102
1103                 if (np->sndflow) {
1104                         fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1105                         if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1106                                 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1107                                 if (!flowlabel)
1108                                         return -EINVAL;
1109                         }
1110                 }
1111
1112                 /*
1113                  * Otherwise it will be difficult to maintain
1114                  * sk->sk_dst_cache.
1115                  */
1116                 if (sk->sk_state == TCP_ESTABLISHED &&
1117                     ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1118                         daddr = &sk->sk_v6_daddr;
1119
1120                 if (addr_len >= sizeof(struct sockaddr_in6) &&
1121                     sin6->sin6_scope_id &&
1122                     __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
1123                         fl6.flowi6_oif = sin6->sin6_scope_id;
1124         } else {
1125                 if (sk->sk_state != TCP_ESTABLISHED)
1126                         return -EDESTADDRREQ;
1127
1128                 fl6.fl6_dport = inet->inet_dport;
1129                 daddr = &sk->sk_v6_daddr;
1130                 fl6.flowlabel = np->flow_label;
1131                 connected = 1;
1132         }
1133
1134         if (!fl6.flowi6_oif)
1135                 fl6.flowi6_oif = sk->sk_bound_dev_if;
1136
1137         if (!fl6.flowi6_oif)
1138                 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1139
1140         fl6.flowi6_mark = sk->sk_mark;
1141         fl6.flowi6_uid = sk->sk_uid;
1142         sockc.tsflags = sk->sk_tsflags;
1143
1144         if (msg->msg_controllen) {
1145                 opt = &opt_space;
1146                 memset(opt, 0, sizeof(struct ipv6_txoptions));
1147                 opt->tot_len = sizeof(*opt);
1148                 ipc6.opt = opt;
1149
1150                 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc);
1151                 if (err < 0) {
1152                         fl6_sock_release(flowlabel);
1153                         return err;
1154                 }
1155                 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1156                         flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1157                         if (!flowlabel)
1158                                 return -EINVAL;
1159                 }
1160                 if (!(opt->opt_nflen|opt->opt_flen))
1161                         opt = NULL;
1162                 connected = 0;
1163         }
1164         if (!opt) {
1165                 opt = txopt_get(np);
1166                 opt_to_free = opt;
1167         }
1168         if (flowlabel)
1169                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1170         opt = ipv6_fixup_options(&opt_space, opt);
1171         ipc6.opt = opt;
1172
1173         fl6.flowi6_proto = sk->sk_protocol;
1174         if (!ipv6_addr_any(daddr))
1175                 fl6.daddr = *daddr;
1176         else
1177                 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1178         if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
1179                 fl6.saddr = np->saddr;
1180         fl6.fl6_sport = inet->inet_sport;
1181
1182         final_p = fl6_update_dst(&fl6, opt, &final);
1183         if (final_p)
1184                 connected = 0;
1185
1186         if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1187                 fl6.flowi6_oif = np->mcast_oif;
1188                 connected = 0;
1189         } else if (!fl6.flowi6_oif)
1190                 fl6.flowi6_oif = np->ucast_oif;
1191
1192         security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1193
1194         if (ipc6.tclass < 0)
1195                 ipc6.tclass = np->tclass;
1196
1197         fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
1198
1199         dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
1200         if (IS_ERR(dst)) {
1201                 err = PTR_ERR(dst);
1202                 dst = NULL;
1203                 goto out;
1204         }
1205
1206         if (ipc6.hlimit < 0)
1207                 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1208
1209         if (msg->msg_flags&MSG_CONFIRM)
1210                 goto do_confirm;
1211 back_from_confirm:
1212
1213         /* Lockless fast path for the non-corking case */
1214         if (!corkreq) {
1215                 struct sk_buff *skb;
1216
1217                 skb = ip6_make_skb(sk, getfrag, msg, ulen,
1218                                    sizeof(struct udphdr), &ipc6,
1219                                    &fl6, (struct rt6_info *)dst,
1220                                    msg->msg_flags, &sockc);
1221                 err = PTR_ERR(skb);
1222                 if (!IS_ERR_OR_NULL(skb))
1223                         err = udp_v6_send_skb(skb, &fl6);
1224                 goto release_dst;
1225         }
1226
1227         lock_sock(sk);
1228         if (unlikely(up->pending)) {
1229                 /* The socket is already corked while preparing it. */
1230                 /* ... which is an evident application bug. --ANK */
1231                 release_sock(sk);
1232
1233                 net_dbg_ratelimited("udp cork app bug 2\n");
1234                 err = -EINVAL;
1235                 goto out;
1236         }
1237
1238         up->pending = AF_INET6;
1239
1240 do_append_data:
1241         if (ipc6.dontfrag < 0)
1242                 ipc6.dontfrag = np->dontfrag;
1243         up->len += ulen;
1244         err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
1245                               &ipc6, &fl6, (struct rt6_info *)dst,
1246                               corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, &sockc);
1247         if (err)
1248                 udp_v6_flush_pending_frames(sk);
1249         else if (!corkreq)
1250                 err = udp_v6_push_pending_frames(sk);
1251         else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1252                 up->pending = 0;
1253
1254         if (err > 0)
1255                 err = np->recverr ? net_xmit_errno(err) : 0;
1256         release_sock(sk);
1257
1258 release_dst:
1259         if (dst) {
1260                 if (connected) {
1261                         ip6_dst_store(sk, dst,
1262                                       ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1263                                       &sk->sk_v6_daddr : NULL,
1264 #ifdef CONFIG_IPV6_SUBTREES
1265                                       ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1266                                       &np->saddr :
1267 #endif
1268                                       NULL);
1269                 } else {
1270                         dst_release(dst);
1271                 }
1272                 dst = NULL;
1273         }
1274
1275 out:
1276         dst_release(dst);
1277         fl6_sock_release(flowlabel);
1278         txopt_put(opt_to_free);
1279         if (!err)
1280                 return len;
1281         /*
1282          * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1283          * ENOBUFS might not be good (it's not tunable per se), but otherwise
1284          * we don't have a good statistic (IpOutDiscards but it can be too many
1285          * things).  We could add another new stat but at least for now that
1286          * seems like overkill.
1287          */
1288         if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1289                 UDP6_INC_STATS(sock_net(sk),
1290                                UDP_MIB_SNDBUFERRORS, is_udplite);
1291         }
1292         return err;
1293
1294 do_confirm:
1295         dst_confirm(dst);
1296         if (!(msg->msg_flags&MSG_PROBE) || len)
1297                 goto back_from_confirm;
1298         err = 0;
1299         goto out;
1300 }
1301
1302 void udpv6_destroy_sock(struct sock *sk)
1303 {
1304         struct udp_sock *up = udp_sk(sk);
1305         lock_sock(sk);
1306         udp_v6_flush_pending_frames(sk);
1307         release_sock(sk);
1308
1309         if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
1310                 void (*encap_destroy)(struct sock *sk);
1311                 encap_destroy = ACCESS_ONCE(up->encap_destroy);
1312                 if (encap_destroy)
1313                         encap_destroy(sk);
1314         }
1315
1316         inet6_destroy_sock(sk);
1317 }
1318
1319 /*
1320  *      Socket option code for UDP
1321  */
1322 int udpv6_setsockopt(struct sock *sk, int level, int optname,
1323                      char __user *optval, unsigned int optlen)
1324 {
1325         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1326                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1327                                           udp_v6_push_pending_frames);
1328         return ipv6_setsockopt(sk, level, optname, optval, optlen);
1329 }
1330
1331 #ifdef CONFIG_COMPAT
1332 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1333                             char __user *optval, unsigned int optlen)
1334 {
1335         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1336                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1337                                           udp_v6_push_pending_frames);
1338         return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1339 }
1340 #endif
1341
1342 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1343                      char __user *optval, int __user *optlen)
1344 {
1345         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1346                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1347         return ipv6_getsockopt(sk, level, optname, optval, optlen);
1348 }
1349
1350 #ifdef CONFIG_COMPAT
1351 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1352                             char __user *optval, int __user *optlen)
1353 {
1354         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1355                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1356         return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1357 }
1358 #endif
1359
1360 static const struct inet6_protocol udpv6_protocol = {
1361         .handler        =       udpv6_rcv,
1362         .err_handler    =       udpv6_err,
1363         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1364 };
1365
1366 /* ------------------------------------------------------------------------ */
1367 #ifdef CONFIG_PROC_FS
1368 int udp6_seq_show(struct seq_file *seq, void *v)
1369 {
1370         if (v == SEQ_START_TOKEN) {
1371                 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1372         } else {
1373                 int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1374                 struct inet_sock *inet = inet_sk(v);
1375                 __u16 srcp = ntohs(inet->inet_sport);
1376                 __u16 destp = ntohs(inet->inet_dport);
1377                 ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
1378         }
1379         return 0;
1380 }
1381
1382 static const struct file_operations udp6_afinfo_seq_fops = {
1383         .owner    = THIS_MODULE,
1384         .open     = udp_seq_open,
1385         .read     = seq_read,
1386         .llseek   = seq_lseek,
1387         .release  = seq_release_net
1388 };
1389
1390 static struct udp_seq_afinfo udp6_seq_afinfo = {
1391         .name           = "udp6",
1392         .family         = AF_INET6,
1393         .udp_table      = &udp_table,
1394         .seq_fops       = &udp6_afinfo_seq_fops,
1395         .seq_ops        = {
1396                 .show           = udp6_seq_show,
1397         },
1398 };
1399
1400 int __net_init udp6_proc_init(struct net *net)
1401 {
1402         return udp_proc_register(net, &udp6_seq_afinfo);
1403 }
1404
1405 void udp6_proc_exit(struct net *net)
1406 {
1407         udp_proc_unregister(net, &udp6_seq_afinfo);
1408 }
1409 #endif /* CONFIG_PROC_FS */
1410
1411 /* ------------------------------------------------------------------------ */
1412
1413 struct proto udpv6_prot = {
1414         .name              = "UDPv6",
1415         .owner             = THIS_MODULE,
1416         .close             = udp_lib_close,
1417         .connect           = ip6_datagram_connect,
1418         .disconnect        = udp_disconnect,
1419         .ioctl             = udp_ioctl,
1420         .init              = udp_init_sock,
1421         .destroy           = udpv6_destroy_sock,
1422         .setsockopt        = udpv6_setsockopt,
1423         .getsockopt        = udpv6_getsockopt,
1424         .sendmsg           = udpv6_sendmsg,
1425         .recvmsg           = udpv6_recvmsg,
1426         .release_cb        = ip6_datagram_release_cb,
1427         .hash              = udp_lib_hash,
1428         .unhash            = udp_lib_unhash,
1429         .rehash            = udp_v6_rehash,
1430         .get_port          = udp_v6_get_port,
1431         .memory_allocated  = &udp_memory_allocated,
1432         .sysctl_mem        = sysctl_udp_mem,
1433         .sysctl_wmem       = &sysctl_udp_wmem_min,
1434         .sysctl_rmem       = &sysctl_udp_rmem_min,
1435         .obj_size          = sizeof(struct udp6_sock),
1436         .h.udp_table       = &udp_table,
1437 #ifdef CONFIG_COMPAT
1438         .compat_setsockopt = compat_udpv6_setsockopt,
1439         .compat_getsockopt = compat_udpv6_getsockopt,
1440 #endif
1441         .diag_destroy      = udp_abort,
1442 };
1443
1444 static struct inet_protosw udpv6_protosw = {
1445         .type =      SOCK_DGRAM,
1446         .protocol =  IPPROTO_UDP,
1447         .prot =      &udpv6_prot,
1448         .ops =       &inet6_dgram_ops,
1449         .flags =     INET_PROTOSW_PERMANENT,
1450 };
1451
1452 int __init udpv6_init(void)
1453 {
1454         int ret;
1455
1456         ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1457         if (ret)
1458                 goto out;
1459
1460         ret = inet6_register_protosw(&udpv6_protosw);
1461         if (ret)
1462                 goto out_udpv6_protocol;
1463 out:
1464         return ret;
1465
1466 out_udpv6_protocol:
1467         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1468         goto out;
1469 }
1470
1471 void udpv6_exit(void)
1472 {
1473         inet6_unregister_protosw(&udpv6_protosw);
1474         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1475 }
This page took 0.117678 seconds and 4 git commands to generate.