diff options
author | Erich E. Hoover <ehoover@mines.edu> | 2012-02-08 04:11:07 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2012-02-08 15:52:45 -0500 |
commit | 76e21053b5bf33a07c76f99d27a74238310e3c71 (patch) | |
tree | 70ac2a67be8f19da96896950447567e156975149 /net/ipv4 | |
parent | 43480aecb1f538d4f6dd8b2c5d2b71fb98659072 (diff) |
ipv4: Implement IP_UNICAST_IF socket option.
The IP_UNICAST_IF feature is needed by the Wine project. This patch
implements the feature by setting the outgoing interface in a similar
fashion to that of IP_MULTICAST_IF. A separate option is needed to
handle this feature since the existing options do not provide all of
the characteristics required by IP_UNICAST_IF, a summary is provided
below.
SO_BINDTODEVICE:
* SO_BINDTODEVICE requires administrative privileges, IP_UNICAST_IF
does not. From reading some old mailing list articles my
understanding is that SO_BINDTODEVICE requires administrative
privileges because it can override the administrator's routing
settings.
* The SO_BINDTODEVICE option restricts both outbound and inbound
traffic, IP_UNICAST_IF only impacts outbound traffic.
IP_PKTINFO:
* Since IP_PKTINFO and IP_UNICAST_IF are independent options,
implementing IP_UNICAST_IF with IP_PKTINFO will likely break some
applications.
* Implementing IP_UNICAST_IF on top of IP_PKTINFO significantly
complicates the Wine codebase and reduces the socket performance
(doing this requires a lot of extra communication between the
"server" and "user" layers).
bind():
* bind() does not work on broadcast packets, IP_UNICAST_IF is
specifically intended to work with broadcast packets.
* Like SO_BINDTODEVICE, bind() restricts both outbound and inbound
traffic.
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>
Diffstat (limited to 'net/ipv4')
-rw-r--r-- | net/ipv4/ip_sockglue.c | 33 | ||||
-rw-r--r-- | net/ipv4/ping.c | 3 | ||||
-rw-r--r-- | net/ipv4/raw.c | 3 | ||||
-rw-r--r-- | net/ipv4/udp.c | 3 |
4 files changed, 39 insertions, 3 deletions
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c index 8aa87c19fa00..9125529dab95 100644 --- a/net/ipv4/ip_sockglue.c +++ b/net/ipv4/ip_sockglue.c | |||
@@ -469,6 +469,7 @@ static int do_ip_setsockopt(struct sock *sk, int level, | |||
469 | (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) | | 469 | (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) | |
470 | (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) | | 470 | (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) | |
471 | (1<<IP_MINTTL) | (1<<IP_NODEFRAG))) || | 471 | (1<<IP_MINTTL) | (1<<IP_NODEFRAG))) || |
472 | optname == IP_UNICAST_IF || | ||
472 | optname == IP_MULTICAST_TTL || | 473 | optname == IP_MULTICAST_TTL || |
473 | optname == IP_MULTICAST_ALL || | 474 | optname == IP_MULTICAST_ALL || |
474 | optname == IP_MULTICAST_LOOP || | 475 | optname == IP_MULTICAST_LOOP || |
@@ -628,6 +629,35 @@ static int do_ip_setsockopt(struct sock *sk, int level, | |||
628 | goto e_inval; | 629 | goto e_inval; |
629 | inet->mc_loop = !!val; | 630 | inet->mc_loop = !!val; |
630 | break; | 631 | break; |
632 | case IP_UNICAST_IF: | ||
633 | { | ||
634 | struct net_device *dev = NULL; | ||
635 | int ifindex; | ||
636 | |||
637 | if (optlen != sizeof(int)) | ||
638 | goto e_inval; | ||
639 | |||
640 | ifindex = (__force int)ntohl((__force __be32)val); | ||
641 | if (ifindex == 0) { | ||
642 | inet->uc_index = 0; | ||
643 | err = 0; | ||
644 | break; | ||
645 | } | ||
646 | |||
647 | dev = dev_get_by_index(sock_net(sk), ifindex); | ||
648 | err = -EADDRNOTAVAIL; | ||
649 | if (!dev) | ||
650 | break; | ||
651 | dev_put(dev); | ||
652 | |||
653 | err = -EINVAL; | ||
654 | if (sk->sk_bound_dev_if) | ||
655 | break; | ||
656 | |||
657 | inet->uc_index = ifindex; | ||
658 | err = 0; | ||
659 | break; | ||
660 | } | ||
631 | case IP_MULTICAST_IF: | 661 | case IP_MULTICAST_IF: |
632 | { | 662 | { |
633 | struct ip_mreqn mreq; | 663 | struct ip_mreqn mreq; |
@@ -1178,6 +1208,9 @@ static int do_ip_getsockopt(struct sock *sk, int level, int optname, | |||
1178 | case IP_MULTICAST_LOOP: | 1208 | case IP_MULTICAST_LOOP: |
1179 | val = inet->mc_loop; | 1209 | val = inet->mc_loop; |
1180 | break; | 1210 | break; |
1211 | case IP_UNICAST_IF: | ||
1212 | val = (__force int)htonl((__u32) inet->uc_index); | ||
1213 | break; | ||
1181 | case IP_MULTICAST_IF: | 1214 | case IP_MULTICAST_IF: |
1182 | { | 1215 | { |
1183 | struct in_addr addr; | 1216 | struct in_addr addr; |
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c index aea5a199c37a..cfc82cf339f6 100644 --- a/net/ipv4/ping.c +++ b/net/ipv4/ping.c | |||
@@ -556,7 +556,8 @@ static int ping_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, | |||
556 | ipc.oif = inet->mc_index; | 556 | ipc.oif = inet->mc_index; |
557 | if (!saddr) | 557 | if (!saddr) |
558 | saddr = inet->mc_addr; | 558 | saddr = inet->mc_addr; |
559 | } | 559 | } else if (!ipc.oif) |
560 | ipc.oif = inet->uc_index; | ||
560 | 561 | ||
561 | flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos, | 562 | flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos, |
562 | RT_SCOPE_UNIVERSE, sk->sk_protocol, | 563 | RT_SCOPE_UNIVERSE, sk->sk_protocol, |
diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c index 3ccda5ae8a27..ab466305b629 100644 --- a/net/ipv4/raw.c +++ b/net/ipv4/raw.c | |||
@@ -563,7 +563,8 @@ static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, | |||
563 | ipc.oif = inet->mc_index; | 563 | ipc.oif = inet->mc_index; |
564 | if (!saddr) | 564 | if (!saddr) |
565 | saddr = inet->mc_addr; | 565 | saddr = inet->mc_addr; |
566 | } | 566 | } else if (!ipc.oif) |
567 | ipc.oif = inet->uc_index; | ||
567 | 568 | ||
568 | flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos, | 569 | flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos, |
569 | RT_SCOPE_UNIVERSE, | 570 | RT_SCOPE_UNIVERSE, |
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 5d075b5f70fc..cd99f1a0f59f 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c | |||
@@ -917,7 +917,8 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, | |||
917 | if (!saddr) | 917 | if (!saddr) |
918 | saddr = inet->mc_addr; | 918 | saddr = inet->mc_addr; |
919 | connected = 0; | 919 | connected = 0; |
920 | } | 920 | } else if (!ipc.oif) |
921 | ipc.oif = inet->uc_index; | ||
921 | 922 | ||
922 | if (connected) | 923 | if (connected) |
923 | rt = (struct rtable *)sk_dst_check(sk, 0); | 924 | rt = (struct rtable *)sk_dst_check(sk, 0); |