Path: blob/master/net/ipv6/inet6_connection_sock.c
15109 views
/*1* INET An implementation of the TCP/IP protocol suite for the LINUX2* operating system. INET is implemented using the BSD Socket3* interface as the means of communication with the user level.4*5* Support for INET6 connection oriented protocols.6*7* Authors: See the TCPv6 sources8*9* This program is free software; you can redistribute it and/or10* modify it under the terms of the GNU General Public License11* as published by the Free Software Foundation; either version12* 2 of the License, or(at your option) any later version.13*/1415#include <linux/module.h>16#include <linux/in6.h>17#include <linux/ipv6.h>18#include <linux/jhash.h>19#include <linux/slab.h>2021#include <net/addrconf.h>22#include <net/inet_connection_sock.h>23#include <net/inet_ecn.h>24#include <net/inet_hashtables.h>25#include <net/ip6_route.h>26#include <net/sock.h>27#include <net/inet6_connection_sock.h>2829int inet6_csk_bind_conflict(const struct sock *sk,30const struct inet_bind_bucket *tb)31{32const struct sock *sk2;33const struct hlist_node *node;3435/* We must walk the whole port owner list in this case. -DaveM */36/*37* See comment in inet_csk_bind_conflict about sock lookup38* vs net namespaces issues.39*/40sk_for_each_bound(sk2, node, &tb->owners) {41if (sk != sk2 &&42(!sk->sk_bound_dev_if ||43!sk2->sk_bound_dev_if ||44sk->sk_bound_dev_if == sk2->sk_bound_dev_if) &&45(!sk->sk_reuse || !sk2->sk_reuse ||46sk2->sk_state == TCP_LISTEN) &&47ipv6_rcv_saddr_equal(sk, sk2))48break;49}5051return node != NULL;52}5354EXPORT_SYMBOL_GPL(inet6_csk_bind_conflict);5556struct dst_entry *inet6_csk_route_req(struct sock *sk,57const struct request_sock *req)58{59struct inet6_request_sock *treq = inet6_rsk(req);60struct ipv6_pinfo *np = inet6_sk(sk);61struct in6_addr *final_p, final;62struct dst_entry *dst;63struct flowi6 fl6;6465memset(&fl6, 0, sizeof(fl6));66fl6.flowi6_proto = IPPROTO_TCP;67ipv6_addr_copy(&fl6.daddr, &treq->rmt_addr);68final_p = fl6_update_dst(&fl6, np->opt, &final);69ipv6_addr_copy(&fl6.saddr, &treq->loc_addr);70fl6.flowi6_oif = sk->sk_bound_dev_if;71fl6.flowi6_mark = sk->sk_mark;72fl6.fl6_dport = inet_rsk(req)->rmt_port;73fl6.fl6_sport = inet_rsk(req)->loc_port;74security_req_classify_flow(req, flowi6_to_flowi(&fl6));7576dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);77if (IS_ERR(dst))78return NULL;7980return dst;81}8283/*84* request_sock (formerly open request) hash tables.85*/86static u32 inet6_synq_hash(const struct in6_addr *raddr, const __be16 rport,87const u32 rnd, const u16 synq_hsize)88{89u32 c;9091c = jhash_3words((__force u32)raddr->s6_addr32[0],92(__force u32)raddr->s6_addr32[1],93(__force u32)raddr->s6_addr32[2],94rnd);9596c = jhash_2words((__force u32)raddr->s6_addr32[3],97(__force u32)rport,98c);99100return c & (synq_hsize - 1);101}102103struct request_sock *inet6_csk_search_req(const struct sock *sk,104struct request_sock ***prevp,105const __be16 rport,106const struct in6_addr *raddr,107const struct in6_addr *laddr,108const int iif)109{110const struct inet_connection_sock *icsk = inet_csk(sk);111struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;112struct request_sock *req, **prev;113114for (prev = &lopt->syn_table[inet6_synq_hash(raddr, rport,115lopt->hash_rnd,116lopt->nr_table_entries)];117(req = *prev) != NULL;118prev = &req->dl_next) {119const struct inet6_request_sock *treq = inet6_rsk(req);120121if (inet_rsk(req)->rmt_port == rport &&122req->rsk_ops->family == AF_INET6 &&123ipv6_addr_equal(&treq->rmt_addr, raddr) &&124ipv6_addr_equal(&treq->loc_addr, laddr) &&125(!treq->iif || treq->iif == iif)) {126WARN_ON(req->sk != NULL);127*prevp = prev;128return req;129}130}131132return NULL;133}134135EXPORT_SYMBOL_GPL(inet6_csk_search_req);136137void inet6_csk_reqsk_queue_hash_add(struct sock *sk,138struct request_sock *req,139const unsigned long timeout)140{141struct inet_connection_sock *icsk = inet_csk(sk);142struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;143const u32 h = inet6_synq_hash(&inet6_rsk(req)->rmt_addr,144inet_rsk(req)->rmt_port,145lopt->hash_rnd, lopt->nr_table_entries);146147reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);148inet_csk_reqsk_queue_added(sk, timeout);149}150151EXPORT_SYMBOL_GPL(inet6_csk_reqsk_queue_hash_add);152153void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr * uaddr)154{155struct ipv6_pinfo *np = inet6_sk(sk);156struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;157158sin6->sin6_family = AF_INET6;159ipv6_addr_copy(&sin6->sin6_addr, &np->daddr);160sin6->sin6_port = inet_sk(sk)->inet_dport;161/* We do not store received flowlabel for TCP */162sin6->sin6_flowinfo = 0;163sin6->sin6_scope_id = 0;164if (sk->sk_bound_dev_if &&165ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)166sin6->sin6_scope_id = sk->sk_bound_dev_if;167}168169EXPORT_SYMBOL_GPL(inet6_csk_addr2sockaddr);170171static inline172void __inet6_csk_dst_store(struct sock *sk, struct dst_entry *dst,173struct in6_addr *daddr, struct in6_addr *saddr)174{175__ip6_dst_store(sk, dst, daddr, saddr);176177#ifdef CONFIG_XFRM178{179struct rt6_info *rt = (struct rt6_info *)dst;180rt->rt6i_flow_cache_genid = atomic_read(&flow_cache_genid);181}182#endif183}184185static inline186struct dst_entry *__inet6_csk_dst_check(struct sock *sk, u32 cookie)187{188struct dst_entry *dst;189190dst = __sk_dst_check(sk, cookie);191192#ifdef CONFIG_XFRM193if (dst) {194struct rt6_info *rt = (struct rt6_info *)dst;195if (rt->rt6i_flow_cache_genid != atomic_read(&flow_cache_genid)) {196__sk_dst_reset(sk);197dst = NULL;198}199}200#endif201202return dst;203}204205int inet6_csk_xmit(struct sk_buff *skb, struct flowi *fl_unused)206{207struct sock *sk = skb->sk;208struct inet_sock *inet = inet_sk(sk);209struct ipv6_pinfo *np = inet6_sk(sk);210struct flowi6 fl6;211struct dst_entry *dst;212struct in6_addr *final_p, final;213214memset(&fl6, 0, sizeof(fl6));215fl6.flowi6_proto = sk->sk_protocol;216ipv6_addr_copy(&fl6.daddr, &np->daddr);217ipv6_addr_copy(&fl6.saddr, &np->saddr);218fl6.flowlabel = np->flow_label;219IP6_ECN_flow_xmit(sk, fl6.flowlabel);220fl6.flowi6_oif = sk->sk_bound_dev_if;221fl6.flowi6_mark = sk->sk_mark;222fl6.fl6_sport = inet->inet_sport;223fl6.fl6_dport = inet->inet_dport;224security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));225226final_p = fl6_update_dst(&fl6, np->opt, &final);227228dst = __inet6_csk_dst_check(sk, np->dst_cookie);229230if (dst == NULL) {231dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);232233if (IS_ERR(dst)) {234sk->sk_err_soft = -PTR_ERR(dst);235sk->sk_route_caps = 0;236kfree_skb(skb);237return PTR_ERR(dst);238}239240__inet6_csk_dst_store(sk, dst, NULL, NULL);241}242243skb_dst_set(skb, dst_clone(dst));244245/* Restore final destination back after routing done */246ipv6_addr_copy(&fl6.daddr, &np->daddr);247248return ip6_xmit(sk, skb, &fl6, np->opt);249}250251EXPORT_SYMBOL_GPL(inet6_csk_xmit);252253254