net: implement IP_RECVTOS for IP_PKTOPTIONS
authorJiri Benc <jbenc@redhat.com>
Thu, 9 Feb 2012 09:35:49 +0000 (09:35 +0000)
committerDavid S. Miller <davem@davemloft.net>
Mon, 13 Feb 2012 05:46:41 +0000 (00:46 -0500)
Currently, it is not easily possible to get TOS/DSCP value of packets from
an incoming TCP stream. The mechanism is there, IP_PKTOPTIONS getsockopt
with IP_RECVTOS set, the same way as incoming TTL can be queried. This is
not actually implemented for TOS, though.

This patch adds this functionality, both for IPv4 (IP_PKTOPTIONS) and IPv6
(IPV6_2292PKTOPTIONS). For IPv4, like in the IP_RECVTTL case, the value of
the TOS field is stored from the other party's ACK.

This is needed for proxies which require DSCP transparency. One such example
is at http://zph.bratcheda.org/.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/ipv6.h
include/net/inet_sock.h
net/ipv4/af_inet.c
net/ipv4/ip_sockglue.c
net/ipv4/tcp_ipv4.c
net/ipv6/af_inet6.c
net/ipv6/ipv6_sockglue.c
net/ipv6/tcp_ipv6.c

index 4847a64d3c0a68eca0de840e10a60af33b02a1fd..8260ef7797626196a5f8124faa3198cb4c269888 100644 (file)
@@ -366,7 +366,7 @@ struct ipv6_pinfo {
                                dontfrag:1;
        __u8                    min_hopcount;
        __u8                    tclass;
-       __u8                    padding;
+       __u8                    rcv_tclass;
 
        __u32                   dst_cookie;
 
index 022f772c0ebee8df9a7126720b044a1209e4a3d2..ae17e1352d7eb1c93f9c1b1ba5a67832d6e4c0c6 100644 (file)
@@ -168,6 +168,7 @@ struct inet_sock {
                                transparent:1,
                                mc_all:1,
                                nodefrag:1;
+       __u8                    rcv_tos;
        int                     uc_index;
        int                     mc_index;
        __be32                  mc_addr;
index f7b5670744f0f92991ddf10854309cb3592bed8d..e588a34e85c261e92c8d26694011cfd8f99e5a04 100644 (file)
@@ -381,6 +381,7 @@ lookup_protocol:
        inet->mc_all    = 1;
        inet->mc_index  = 0;
        inet->mc_list   = NULL;
+       inet->rcv_tos   = 0;
 
        sk_refcnt_debug_inc(sk);
 
index 9125529dab9593964b0529d31a96727e07d1a470..ca50d9f9f8c1a59b6968a067a38ff30ea5fc8a16 100644 (file)
@@ -1289,6 +1289,10 @@ static int do_ip_getsockopt(struct sock *sk, int level, int optname,
                        int hlim = inet->mc_ttl;
                        put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
                }
+               if (inet->cmsg_flags & IP_CMSG_TOS) {
+                       int tos = inet->rcv_tos;
+                       put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
+               }
                len -= msg.msg_controllen;
                return put_user(len, optlen);
        }
index 4d6f81c818dc869c951aa4192386f1b85b94c3ca..94abee8cf563d68184db984004caf75a91fa4dcc 100644 (file)
@@ -1463,6 +1463,7 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
        ireq->opt             = NULL;
        newinet->mc_index     = inet_iif(skb);
        newinet->mc_ttl       = ip_hdr(skb)->ttl;
+       newinet->rcv_tos      = ip_hdr(skb)->tos;
        inet_csk(newsk)->icsk_ext_hdr_len = 0;
        if (inet_opt)
                inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
index 273f48d1df2e974b5722e9133c3dd04ffd39473e..5605f9dca87e45cdd6b21dcc02864d2f5648160f 100644 (file)
@@ -214,6 +214,7 @@ lookup_protocol:
        inet->mc_ttl    = 1;
        inet->mc_index  = 0;
        inet->mc_list   = NULL;
+       inet->rcv_tos   = 0;
 
        if (ipv4_config.no_pmtu_disc)
                inet->pmtudisc = IP_PMTUDISC_DONT;
index 6d6b65fdaa1a47793eb5e13149dfbc07852c0760..63dd1f89ed7deaed484382e6a82b66233e60dd6b 100644 (file)
@@ -1017,6 +1017,10 @@ static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
                                int hlim = np->mcast_hops;
                                put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
                        }
+                       if (np->rxopt.bits.rxtclass) {
+                               int tclass = np->rcv_tclass;
+                               put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
+                       }
                        if (np->rxopt.bits.rxoinfo) {
                                struct in6_pktinfo src_info;
                                src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
index d16414cb3421231bd800de6ce7e593b3cbc0db59..12c6ece67f396a2d01f641b7a0e0d728fcab3337 100644 (file)
@@ -1282,6 +1282,7 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
                newnp->opt         = NULL;
                newnp->mcast_oif   = inet6_iif(skb);
                newnp->mcast_hops  = ipv6_hdr(skb)->hop_limit;
+               newnp->rcv_tclass  = ipv6_tclass(ipv6_hdr(skb));
 
                /*
                 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
@@ -1360,6 +1361,7 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
        newnp->opt        = NULL;
        newnp->mcast_oif  = inet6_iif(skb);
        newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
+       newnp->rcv_tclass = ipv6_tclass(ipv6_hdr(skb));
 
        /* Clone native IPv6 options from listening socket (if any)
 
@@ -1562,6 +1564,8 @@ ipv6_pktoptions:
                        np->mcast_oif = inet6_iif(opt_skb);
                if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
                        np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
+               if (np->rxopt.bits.rxtclass)
+                       np->rcv_tclass = ipv6_tclass(ipv6_hdr(skb));
                if (ipv6_opt_accepted(sk, opt_skb)) {
                        skb_set_owner_r(opt_skb, sk);
                        opt_skb = xchg(&np->pktoptions, opt_skb);