ipv6: Implement IPV6_UNICAST_IF socket option.
authorErich E. Hoover <ehoover@mines.edu>
Wed, 8 Feb 2012 09:11:08 +0000 (09:11 +0000)
committerDavid S. Miller <davem@davemloft.net>
Wed, 8 Feb 2012 20:52:45 +0000 (15:52 -0500)
The IPV6_UNICAST_IF feature is the IPv6 compliment to IP_UNICAST_IF.

Signed-off-by: Erich E. Hoover <ehoover@mines.edu>
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/in6.h
include/linux/ipv6.h
net/ipv6/icmp.c
net/ipv6/ipv6_sockglue.c
net/ipv6/raw.c
net/ipv6/udp.c

index 097a34b55560a9d4434f61f78b85f5fbbef8e2e7..5c83d9e3eb8f1bbc14cb7e906394080fb1760686 100644 (file)
@@ -271,6 +271,7 @@ struct in6_flowlabel_req {
 #define IPV6_ORIGDSTADDR        74
 #define IPV6_RECVORIGDSTADDR    IPV6_ORIGDSTADDR
 #define IPV6_TRANSPARENT        75
+#define IPV6_UNICAST_IF         76
 
 /*
  * Multicast Routing:
index 6318268dcaf5c93cef691ea922c5d598fcbda749..743a16a41040347dcabf9069cfa810fcea903eaf 100644 (file)
@@ -324,6 +324,7 @@ struct ipv6_pinfo {
                                __unused_2:6;
        __s16                   mcast_hops:9;
 #endif
+       int                     ucast_oif;
        int                     mcast_oif;
 
        /* pktoption flags */
index 01d46bff63c312b7804c8132d6860a1a749eb043..af88934e4d79aebe751d420737866656418397af 100644 (file)
@@ -468,6 +468,8 @@ void icmpv6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
 
        if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
                fl6.flowi6_oif = np->mcast_oif;
+       else if (!fl6.flowi6_oif)
+               fl6.flowi6_oif = np->ucast_oif;
 
        dst = icmpv6_route_lookup(net, skb, sk, &fl6);
        if (IS_ERR(dst))
@@ -553,6 +555,8 @@ static void icmpv6_echo_reply(struct sk_buff *skb)
 
        if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
                fl6.flowi6_oif = np->mcast_oif;
+       else if (!fl6.flowi6_oif)
+               fl6.flowi6_oif = np->ucast_oif;
 
        err = ip6_dst_lookup(sk, &dst, &fl6);
        if (err)
index 18a2719003c378e95a02dacf5f71023380244739..6d6b65fdaa1a47793eb5e13149dfbc07852c0760 100644 (file)
@@ -516,6 +516,36 @@ done:
                retv = 0;
                break;
 
+       case IPV6_UNICAST_IF:
+       {
+               struct net_device *dev = NULL;
+               int ifindex;
+
+               if (optlen != sizeof(int))
+                       goto e_inval;
+
+               ifindex = (__force int)ntohl((__force __be32)val);
+               if (ifindex == 0) {
+                       np->ucast_oif = 0;
+                       retv = 0;
+                       break;
+               }
+
+               dev = dev_get_by_index(net, ifindex);
+               retv = -EADDRNOTAVAIL;
+               if (!dev)
+                       break;
+               dev_put(dev);
+
+               retv = -EINVAL;
+               if (sk->sk_bound_dev_if)
+                       break;
+
+               np->ucast_oif = ifindex;
+               retv = 0;
+               break;
+       }
+
        case IPV6_MULTICAST_IF:
                if (sk->sk_type == SOCK_STREAM)
                        break;
@@ -1160,6 +1190,10 @@ static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
                val = np->mcast_oif;
                break;
 
+       case IPV6_UNICAST_IF:
+               val = (__force int)htonl((__u32) np->ucast_oif);
+               break;
+
        case IPV6_MTU_DISCOVER:
                val = np->pmtudisc;
                break;
index d02f7e4dd6113c8c23cb8a3f7ee9be9cbae37685..5bddea778840721b01d2aea417ed96475518f918 100644 (file)
@@ -856,6 +856,8 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
 
        if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
                fl6.flowi6_oif = np->mcast_oif;
+       else if (!fl6.flowi6_oif)
+               fl6.flowi6_oif = np->ucast_oif;
        security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
 
        dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
index 4f96b5c636856eef46c0ec3a0dd990ae997ee7b0..8aebf8f90436b2f9b77e3bb964396150dc4fa6e8 100644 (file)
@@ -1130,7 +1130,8 @@ do_udp_sendmsg:
        if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
                fl6.flowi6_oif = np->mcast_oif;
                connected = 0;
-       }
+       } else if (!fl6.flowi6_oif)
+               fl6.flowi6_oif = np->ucast_oif;
 
        security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));