net: inet: Support UID-based routing
Subsystems (8)
PATCH v2 1/1 net: inet: Support UID-based routing
This patchset adds support for per-UID routing. It allows the administrator to configure rules such as:
ip rule add uidrange 100-200 lookup 123
This functionality has been in use by all Android devices since 5.0. It is primarily used to impose per-app routing policies (on Android, every app has its own UID) without having to resort to rerouting packets in iptables, which breaks getsockname() and MTU/MSS calculation, and generally disrupts end-to-end connectivity.
This patch series is similar to the code currently used on Android, but has better correctness and performance because it stores the UID in the socket instead of calling sock_i_uid. This avoids contention on sk->sk_callback_lock, and makes it possible to correctly route a socket on which userspace has called close(), for which sock_i_uid will return 0.
Changes from v1:
- Don't set the UID in sk_clone_lock, it's already set by sock_copy.
- For packets originated by kernel sockets, don't use the socket UID. This is the UID that created the namespace, but it might not be mapped in the namespace at all. Instead, use UID 0 in the namespace, which is less surprising and consistent with what happens in the root namespace.
- Fix UID routing of IPv4 and IPv6 SYN_RECV sockets.
- Fix UID routing of received IPv6 redirects.
PATCH v2 1/3 net: core: Add a UID field to struct sock.
Protocol sockets (struct sock) don't have UIDs, but most of the time, they map 1:1 to userspace sockets (struct socket) which do.
Various operations such as the iptables xt_owner match need access to the "UID of a socket", and do so by following the backpointer to the struct socket. This involves taking sk_callback_lock and doesn't work when there is no socket because userspace has already called close().
Simplify this by adding a sk_uid field to struct sock whose value matches the UID of the corresponding struct socket. The semantics are as follows:
1. Whenever sk_socket is non-null: sk_uid is the same as the UID in sk_socket, i.e., matches the return value of sock_i_uid. Specifically, the UID is set when userspace calls socket(), fchown(), or accept().
2. When sk_socket is NULL, sk_uid is defined as follows:
- For a socket that no longer has a sk_socket because userspace has called close(): the previous UID.
- For a cloned socket (e.g., an incoming connection that is established but on which userspace has not yet called accept): the UID of the socket it was cloned from.
- For a socket that has never had an sk_socket: UID 0 inside the user namespace corresponding to the network namespace the socket belongs to.
Kernel sockets created by sock_create_kern are a special case of #1 and sk_uid is the user that created them. For kernel sockets created at network namespace creation time, such as the per-processor ICMP and TCP sockets, this is the user that created the network namespace.
--- include/net/sock.h | 7 +++++++ net/core/sock.c | 5 ++++- net/socket.c | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-)
include/net/sock.h +7 -0
@@ -419,6 +419,7 @@ struct sock {u32 sk_max_ack_backlog;__u32 sk_priority;__u32 sk_mark; +kuid_t sk_uid;struct pid *sk_peer_pid;const struct cred *sk_peer_cred;long sk_rcvtimeo;
@@ -1664,6 +1665,7 @@ static inline void sock_graft(struct sock *sk, struct socket *parent) sk->sk_wq = parent->wq; parent->sk = sk;sk_set_socket (sk, parent); + sk->sk_uid =SOCK_INODE (parent)->i_uid;security_sock_graft (sk, parent);write_unlock_bh (&sk->sk_callback_lock); }
@@ -1671,6 +1673,11 @@ static inline void sock_graft(struct sock *sk, struct socket *parent)kuid_t sock_i_uid (struct sock *sk);unsigned long sock_i_ino (struct sock *sk); +static inline kuid_t sock_net_uid (const struct net *net,const struct sock *sk) +{ +return sk ? sk->sk_uid :make_kuid (net->user_ns,0 ); +} +static inline u32 net_tx_rndhash (void ) {u32 v =prandom_u32 ();
net/core/sock.c +4 -1
@@ -2460,8 +2460,11 @@ void sock_init_data(struct socket *sock, struct sock *sk) sk->sk_type = sock->type; sk->sk_wq = sock->wq; sock->sk = sk; - }else + sk->sk_uid =SOCK_INODE (sock)->i_uid; + }else { sk->sk_wq =NULL ; + sk->sk_uid =make_kuid (sock_net (sk)->user_ns,0 ); + }rwlock_init (&sk->sk_callback_lock); lockdep_set_class_and_name(&sk->sk_callback_lock,
net/socket.c +14 -0
@@ -518,8 +518,22 @@ static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,return used; } +int sockfs_setattr (struct dentry *dentry,struct iattr *iattr) +{ +int err =simple_setattr (dentry, iattr); + +if (!err) { +struct socket *sock =SOCKET_I (d_inode (dentry)); + + sock->sk->sk_uid = iattr->ia_uid; + } + +return err; +} +static const struct inode_operations sockfs_inode_ops = { .listxattr = sockfs_listxattr, + .setattr = sockfs_setattr, };/**
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 2/3 net: core: add UID to flows, rules, and routes
In reply to Lorenzo Colitti- Define a new FIB rule attributes, FRA_UID_RANGE, to describe a range of UIDs.
- Define a RTA_UID attribute for per-UID route lookups and dumps.
- Support passing these attributes to and from userspace via rtnetlink. The value INVALID_UID indicates no UID was specified.
- Add a UID field to the flow structures.
--- include/net/fib_rules.h | 9 ++++- include/net/flow.h | 5 +++ include/uapi/linux/fib_rules.h | 6 ++++ include/uapi/linux/rtnetlink.h | 1 + net/core/fib_rules.c | 74 ++++++++++++++++++++++++++++++++++++++++-- net/ipv4/fib_frontend.c | 1 + net/ipv4/route.c | 11 +++++++ net/ipv6/route.c | 7 ++++ 8 files changed, 111 insertions(+), 3 deletions(-)
include/net/fib_rules.h +8 -1
@@ -8,6 +8,11 @@#include <net/flow.h> #include <net/rtnetlink.h> +struct fib_kuid_range { +kuid_t start; +kuid_t end; +}; +struct fib_rule {struct list_head list;int iifindex;
@@ -30,6 +35,7 @@ struct fib_rule {int suppress_prefixlen;char iifname[IFNAMSIZ ];char oifname[IFNAMSIZ ]; +struct fib_kuid_range uid_range;struct rcu_head rcu; };
@@ -92,7 +98,8 @@ struct fib_rules_ops { [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \ [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \ [FRA_GOTO] = { .type = NLA_U32 }, \ - [FRA_L3MDEV] = { .type = NLA_U8 } + [FRA_L3MDEV] = { .type = NLA_U8 }, \ + [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }static inline void fib_rule_get (struct fib_rule *rule) {
include/net/flow.h +5 -0
@@ -11,6 +11,7 @@#include <linux/in6.h> #include <linux/atomic.h> #include <net/flow_dissector.h> +#include <linux/uidgid.h> /* * ifindex generation is per-net namespace, and loopback is
@@ -37,6 +38,7 @@ struct flowi_common {#define FLOWI_FLAG_SKIP_NH_OIF 0x04__u32 flowic_secid;struct flowi_tunnel flowic_tun_key; +kuid_t flowic_uid; };union flowi_uli {
@@ -74,6 +76,7 @@ struct flowi4 {#define flowi4_flags __fl_common.flowic_flags#define flowi4_secid __fl_common.flowic_secid#define flowi4_tun_key __fl_common.flowic_tun_key +#define flowi4_uid __fl_common.flowic_uid/* (saddr,daddr) must be grouped, same order as in IP header */ __be32 saddr;
@@ -131,6 +134,7 @@ struct flowi6 {#define flowi6_flags __fl_common.flowic_flags#define flowi6_secid __fl_common.flowic_secid#define flowi6_tun_key __fl_common.flowic_tun_key +#define flowi6_uid __fl_common.flowic_uidstruct in6_addr daddr;struct in6_addr saddr;/* Note: flowi6_tos is encoded in flowlabel, too. */
@@ -176,6 +180,7 @@ struct flowi {#define flowi_flags u.__fl_common.flowic_flags#define flowi_secid u.__fl_common.flowic_secid#define flowi_tun_key u.__fl_common.flowic_tun_key +#define flowi_uid u.__fl_common.flowic_uid } __attribute__((__aligned__(BITS_PER_LONG/8)));static inline struct flowi *flowi4_to_flowi (struct flowi4 *fl4)
include/uapi/linux/fib_rules.h +6 -0
@@ -29,6 +29,11 @@ struct fib_rule_hdr {__u32 flags; }; +struct fib_rule_uid_range { +__u32 start; +__u32 end; +}; +enum {FRA_UNSPEC ,FRA_DST ,/* destination address */
@@ -51,6 +56,7 @@ enum {FRA_OIFNAME ,FRA_PAD ,FRA_L3MDEV ,/* iif or oif is l3mdev goto its table */ +FRA_UID_RANGE ,/* UID range */ __FRA_MAX };
include/uapi/linux/rtnetlink.h +1 -0
@@ -318,6 +318,7 @@ enum rtattr_type_t {RTA_ENCAP ,RTA_EXPIRES ,RTA_PAD , +RTA_UID , __RTA_MAX };
net/core/fib_rules.c +72 -2
134 lines not on this page · raw message
net/ipv4/fib_frontend.c +1 -0
@@ -610,6 +610,7 @@ const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { [RTA_FLOW] = { .type = NLA_U32 }, [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, [RTA_ENCAP] = { .type = NLA_NESTED }, + [RTA_UID] = { .type = NLA_U32 }, }; static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
net/ipv4/route.c +11 -0
@@ -2504,6 +2504,11 @@ static int rt_fill_info(struct net *net, __be32 dst, __be32 src, u32 table_id, nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark)) goto nla_put_failure; +if (!uid_eq (fl4->flowi4_uid,INVALID_UID ) && +nla_put_u32 (skb,RTA_UID , +from_kuid_munged (current_user_ns (), fl4->flowi4_uid))) + goto nla_put_failure; + error = rt->dst.error;if (rt_is_input_route (rt)) {
@@ -2556,6 +2561,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)int mark;struct sk_buff *skb;u32 table_id =RT_TABLE_MAIN ; +kuid_t uid; err =nlmsg_parse (nlh,sizeof (*rtm), tb,RTA_MAX , rtm_ipv4_policy);if (err <0 )
@@ -2583,6 +2589,10 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh) dst = tb[RTA_DST ] ?nla_get_in_addr (tb[RTA_DST ]) :0 ; iif = tb[RTA_IIF ] ?nla_get_u32 (tb[RTA_IIF ]) :0 ; mark = tb[RTA_MARK ] ?nla_get_u32 (tb[RTA_MARK ]) :0 ; +if (tb[RTA_UID ]) + uid =make_kuid (current_user_ns (),nla_get_u32 (tb[RTA_UID ])); +else + uid = (iif ?INVALID_UID :current_uid ());memset (&fl4,0 ,sizeof (fl4)); fl4.daddr = dst;
@@ -2590,6 +2600,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh) fl4.flowi4_tos = rtm->rtm_tos; fl4.flowi4_oif = tb[RTA_OIF ] ?nla_get_u32 (tb[RTA_OIF ]) :0 ; fl4.flowi4_mark = mark; + fl4.flowi4_uid = uid;if (iif) {struct net_device *dev;
net/ipv6/route.c +7 -0
@@ -2797,6 +2797,7 @@ static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, [RTA_ENCAP] = { .type = NLA_NESTED }, [RTA_EXPIRES] = { .type = NLA_U32 }, + [RTA_UID] = { .type = NLA_U32 }, }; static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -3371,6 +3372,12 @@ static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)if (tb[RTA_MARK ]) fl6.flowi6_mark =nla_get_u32 (tb[RTA_MARK ]); +if (tb[RTA_UID ]) + fl6.flowi6_uid =make_kuid (current_user_ns (), +nla_get_u32 (tb[RTA_UID ])); +else + fl6.flowi6_uid = iif ?INVALID_UID :current_uid (); +if (iif) {struct net_device *dev;int flags =0 ;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 3/3 net: inet: Support UID-based routing in IP protocols.
In reply to Lorenzo Colitti- Use the UID in routing lookups made by protocol connect() and sendmsg() functions.
- Make sure that routing lookups triggered by incoming packets (e.g., Path MTU discovery) take the UID of the socket into account.
- For packets not associated with a userspace socket, (e.g., ping replies) use UID 0 inside the user namespace corresponding to the network namespace the socket belongs to. This allows all namespaces to apply routing and iptables rules to kernel-originated traffic in that namespaces by matching UID 0. This is better than using the UID of the kernel socket that is sending the traffic, because the UID of kernel sockets created at namespace creation time (e.g., the per-processor ICMP and TCP sockets) is the UID of the user that created the socket, which might not be mapped in the namespace.
Tested: compiles allnoconfig, allyesconfig, allmodconfig
Tested: https://android-review.googlesource.com/253302
--- include/net/flow.h | 4 +++- include/net/ip.h | 1 + include/net/ip6_route.h | 5 +++-- include/net/route.h | 5 +++-- net/ipv4/icmp.c | 2 ++ net/ipv4/inet_connection_sock.c | 4 ++-- net/ipv4/ip_output.c | 3 ++- net/ipv4/ping.c | 3 ++- net/ipv4/raw.c | 2 +- net/ipv4/route.c | 26 +++++++++++++++----------- net/ipv4/syncookies.c | 2 +- net/ipv4/tcp_ipv4.c | 9 ++++++--- net/ipv4/udp.c | 3 ++- net/ipv6/af_inet6.c | 1 + net/ipv6/ah6.c | 5 +++-- net/ipv6/datagram.c | 1 + net/ipv6/esp6.c | 5 +++-- net/ipv6/icmp.c | 7 +++++-- net/ipv6/inet6_connection_sock.c | 2 ++ net/ipv6/ip6_gre.c | 4 ++++ net/ipv6/ip6_tunnel.c | 4 ++++ net/ipv6/ip6_vti.c | 5 +++-- net/ipv6/ipcomp6.c | 5 +++-- net/ipv6/netfilter.c | 1 + net/ipv6/ping.c | 1 + net/ipv6/raw.c | 1 + net/ipv6/route.c | 13 +++++++++---- net/ipv6/syncookies.c | 1 + net/ipv6/tcp_ipv6.c | 2 ++ net/ipv6/udp.c | 1 + net/l2tp/l2tp_ip6.c | 1 + 31 files changed, 89 insertions(+), 40 deletions(-)
include/net/flow.h +3 -1
@@ -96,7 +96,8 @@ static inline void flowi4_init_output(struct flowi4 *fl4, int oif, __u32 mark, __u8 tos, __u8 scope, __u8 proto, __u8 flags, __be32 daddr, __be32 saddr, - __be16 dport, __be16 sport) + __be16 dport, __be16 sport, + kuid_t uid) { fl4->flowi4_oif = oif; fl4->flowi4_iif =LOOPBACK_IFINDEX ;
@@ -107,6 +108,7 @@ static inline void flowi4_init_output(struct flowi4 *fl4, int oif, fl4->flowi4_flags = flags; fl4->flowi4_secid =0 ; fl4->flowi4_tun_key.tun_id =0 ; + fl4->flowi4_uid = uid; fl4->daddr = daddr; fl4->saddr = saddr; fl4->fl4_dport = dport;
include/net/ip.h +1 -0
@@ -179,6 +179,7 @@ struct ip_reply_arg {/* -1 if not needed */ int bound_dev_if;u8 tos; +kuid_t uid; };#define IP_REPLY_ARG_NOSRCCHECK 1
include/net/ip6_route.h +3 -2
@@ -140,9 +140,10 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, const struct in6_addr *gwaddr);void ip6_update_pmtu (struct sk_buff *skb,struct net *net,__be32 mtu,int oif, -u32 mark); +u32 mark,kuid_t uid);void ip6_sk_update_pmtu (struct sk_buff *skb,struct sock *sk,__be32 mtu); -void ip6_redirect (struct sk_buff *skb,struct net *net,int oif,u32 mark); +void ip6_redirect (struct sk_buff *skb,struct net *net,int oif,u32 mark, +kuid_t uid);void ip6_redirect_no_header (struct sk_buff *skb,struct net *net,int oif,u32 mark);void ip6_sk_redirect (struct sk_buff *skb,struct sock *sk);
include/net/route.h +3 -2
@@ -153,7 +153,7 @@ static inline struct rtable *ip_route_output_ports(struct net *net, struct flowiflowi4_init_output (fl4, oif, sk ? sk->sk_mark :0 , tos,RT_SCOPE_UNIVERSE , proto, sk ?inet_sk_flowi_flags (sk) :0 , - daddr, saddr, dport, sport); + daddr, saddr, dport, sport,sock_net_uid (net, sk));if (sk)security_sk_classify_flow (sk,flowi4_to_flowi (fl4));return ip_route_output_flow (net, fl4, sk);
@@ -269,7 +269,8 @@ static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst, __be32 flow_flags |=FLOWI_FLAG_ANYSRC ;flowi4_init_output (fl4, oif, sk->sk_mark, tos,RT_SCOPE_UNIVERSE , - protocol, flow_flags, dst, src, dport, sport); + protocol, flow_flags, dst, src, dport, sport, + sk->sk_uid); } static inline struct rtable *ip_route_connect(struct flowi4 *fl4,
net/ipv4/icmp.c +2 -0
@@ -425,6 +425,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb) fl4.daddr = daddr; fl4.saddr = saddr; fl4.flowi4_mark = mark; + fl4.flowi4_uid =sock_net_uid (net,NULL ); fl4.flowi4_tos =RT_TOS (ip_hdr (skb)->tos); fl4.flowi4_proto =IPPROTO_ICMP ; fl4.flowi4_oif =l3mdev_master_ifindex (skb->dev);
@@ -473,6 +474,7 @@ static struct rtable *icmp_route_lookup(struct net *net, param->replyopts.opt.opt.faddr : iph->saddr); fl4->saddr = saddr; fl4->flowi4_mark = mark; + fl4->flowi4_uid =sock_net_uid (net,NULL ); fl4->flowi4_tos =RT_TOS (tos); fl4->flowi4_proto =IPPROTO_ICMP ; fl4->fl4_icmp_type = type;
net/ipv4/inet_connection_sock.c +2 -2
@@ -415,7 +415,7 @@ struct dst_entry *inet_csk_route_req(const struct sock *sk, sk->sk_protocol,inet_sk_flowi_flags (sk), (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr, ireq->ir_loc_addr, ireq->ir_rmt_port, - htons(ireq->ir_num)); + htons(ireq->ir_num), sk->sk_uid);security_req_classify_flow (req,flowi4_to_flowi (fl4)); rt =ip_route_output_flow (net, fl4, sk);if (IS_ERR (rt))
@@ -452,7 +452,7 @@ struct dst_entry *inet_csk_route_child_sock(const struct sock *sk, sk->sk_protocol,inet_sk_flowi_flags (sk), (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr, ireq->ir_loc_addr, ireq->ir_rmt_port, - htons(ireq->ir_num)); + htons(ireq->ir_num), sk->sk_uid);security_req_classify_flow (req,flowi4_to_flowi (fl4)); rt =ip_route_output_flow (net, fl4, sk);if (IS_ERR (rt))
net/ipv4/ip_output.c +2 -1
@@ -1587,7 +1587,8 @@ void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,RT_SCOPE_UNIVERSE ,ip_hdr (skb)->protocol,ip_reply_arg_flowi_flags (arg), daddr, saddr, - tcp_hdr(skb)->source, tcp_hdr(skb)->dest); +tcp_hdr (skb)->source,tcp_hdr (skb)->dest, + arg->uid);security_skb_classify_flow (skb,flowi4_to_flowi (&fl4)); rt =ip_route_output_key (net, &fl4);if (IS_ERR (rt))
net/ipv4/ping.c +2 -1
@@ -789,7 +789,8 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)flowi4_init_output (&fl4, ipc.oif, sk->sk_mark, tos,RT_SCOPE_UNIVERSE , sk->sk_protocol, -inet_sk_flowi_flags (sk), faddr, saddr,0 ,0 ); +inet_sk_flowi_flags (sk), faddr, saddr,0 ,0 , + sk->sk_uid);security_sk_classify_flow (sk,flowi4_to_flowi (&fl4)); rt =ip_route_output_flow (net, &fl4, sk);
net/ipv4/raw.c +1 -1
@@ -606,7 +606,7 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) inet->hdrincl ?IPPROTO_RAW : sk->sk_protocol,inet_sk_flowi_flags (sk) | (inet->hdrincl ?FLOWI_FLAG_KNOWN_NH :0 ), - daddr, saddr, 0, 0); + daddr, saddr, 0, 0, sk->sk_uid);if (!inet->hdrincl) { rfv.msg = msg;
net/ipv4/route.c +15 -11
@@ -507,7 +507,8 @@ void __ip_select_ident(struct net *net, struct iphdr *iph, int segs) }EXPORT_SYMBOL (__ip_select_ident); -static void __build_flow_key (struct flowi4 *fl4,const struct sock *sk, +static void __build_flow_key (const struct net *net,struct flowi4 *fl4, +const struct sock *sk,const struct iphdr *iph,int oif,u8 tos,u8 prot,u32 mark,int flow_flags)
@@ -523,7 +524,8 @@ static void __build_flow_key(struct flowi4 *fl4, const struct sock *sk,flowi4_init_output (fl4, oif, mark, tos,RT_SCOPE_UNIVERSE , prot, flow_flags, - iph->daddr, iph->saddr,0 ,0 ); + iph->daddr, iph->saddr,0 ,0 , +sock_net_uid (net, sk)); } static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,
@@ -535,7 +537,7 @@ static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,u8 prot = iph->protocol;u32 mark = skb->mark; -__build_flow_key (fl4, sk, iph, oif, tos, prot, mark,0 ); +__build_flow_key (sock_net (sk), fl4, sk, iph, oif, tos, prot, mark,0 ); }static void build_sk_flow_key (struct flowi4 *fl4,const struct sock *sk)
@@ -552,7 +554,7 @@ static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)RT_CONN_FLAGS (sk),RT_SCOPE_UNIVERSE , inet->hdrincl ?IPPROTO_RAW : sk->sk_protocol,inet_sk_flowi_flags (sk), - daddr, inet->inet_saddr, 0, 0); + daddr, inet->inet_saddr, 0, 0, sk->sk_uid);rcu_read_unlock (); }
@@ -800,7 +802,7 @@ static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buf rt = (struct rtable *) dst; -__build_flow_key (&fl4, sk, iph, oif, tos, prot, mark,0 ); +__build_flow_key (sock_net (sk), &fl4, sk, iph, oif, tos, prot, mark,0 );__ip_do_redirect (rt, skb, &fl4, true); }
@@ -1018,7 +1020,7 @@ void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,if (!mark) mark =IP4_REPLY_MARK (net, skb->mark); -__build_flow_key (&fl4,NULL , iph, oif, +__build_flow_key (net, &fl4,NULL , iph, oif,RT_TOS (iph->tos), protocol, mark, flow_flags); rt =__ip_route_output_key (net, &fl4);if (!IS_ERR (rt)) {
@@ -1034,7 +1036,7 @@ static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)struct flowi4 fl4;struct rtable *rt; -__build_flow_key (&fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); +__build_flow_key (sock_net (sk), &fl4, sk, iph,0 ,0 ,0 ,0 ,0 );if (!fl4.flowi4_mark) fl4.flowi4_mark =IP4_REPLY_MARK (sock_net (sk), skb->mark);
@@ -1053,6 +1055,7 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)struct rtable *rt;struct dst_entry *odst =NULL ;bool new = false; +struct net *net =sock_net (sk);bh_lock_sock (sk);
@@ -1066,7 +1069,7 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) goto out; } -__build_flow_key (&fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); +__build_flow_key (net, &fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); rt = (struct rtable *)odst;if (odst->obsolete && !odst->ops->check (odst,0 )) {
@@ -1106,7 +1109,7 @@ void ipv4_redirect(struct sk_buff *skb, struct net *net,struct flowi4 fl4;struct rtable *rt; -__build_flow_key (&fl4,NULL , iph, oif, +__build_flow_key (net, &fl4,NULL , iph, oif,RT_TOS (iph->tos), protocol, mark, flow_flags); rt =__ip_route_output_key (net, &fl4);if (!IS_ERR (rt)) {
@@ -1121,9 +1124,10 @@ void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)const struct iphdr *iph = (const struct iphdr *) skb->data;struct flowi4 fl4;struct rtable *rt; +struct net *net =sock_net (sk); -__build_flow_key (&fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); - rt =__ip_route_output_key (sock_net (sk), &fl4); +__build_flow_key (net, &fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); + rt =__ip_route_output_key (net, &fl4);if (!IS_ERR (rt)) {__ip_do_redirect (rt, skb, &fl4, false);ip_rt_put (rt);
net/ipv4/syncookies.c +1 -1
@@ -372,7 +372,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)RT_CONN_FLAGS (sk),RT_SCOPE_UNIVERSE ,IPPROTO_TCP ,inet_sk_flowi_flags (sk), opt->srr ? opt->faddr : ireq->ir_rmt_addr, - ireq->ir_loc_addr, th->source, th->dest); + ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);security_req_classify_flow (req,flowi4_to_flowi (&fl4)); rt =ip_route_output_key (sock_net (sk), &fl4);if (IS_ERR (rt)) {
net/ipv4/tcp_ipv4.c +6 -3
@@ -691,6 +691,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb) offsetof(struct inet_timewait_sock, tw_bound_dev_if)); arg.tos =ip_hdr (skb)->tos; + arg.uid =sock_net_uid (net, sk &&sk_fullsock (sk) ? sk :NULL );local_bh_disable (); ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk), skb, &TCP_SKB_CB(skb)->header.h4.opt,
@@ -711,7 +712,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)outside socket context is ugly, certainly. What can I do? */ -static void tcp_v4_send_ack(struct net *net, +static void tcp_v4_send_ack(const struct sock *sk, struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 tsval, u32 tsecr, int oif, struct tcp_md5sig_key *key,
@@ -726,6 +727,7 @@ static void tcp_v4_send_ack(struct net *net,#endif ]; } rep; +struct net *net =sock_net (sk);struct ip_reply_arg arg;memset (&rep.th,0 ,sizeof (struct tcphdr ));
@@ -775,6 +777,7 @@ static void tcp_v4_send_ack(struct net *net,if (oif) arg.bound_dev_if = oif; arg.tos = tos; + arg.uid =sock_net_uid (net,sk_fullsock (sk) ? sk :NULL );local_bh_disable (); ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk), skb, &TCP_SKB_CB(skb)->header.h4.opt,
@@ -790,7 +793,7 @@ static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)struct inet_timewait_sock *tw =inet_twsk (sk);struct tcp_timewait_sock *tcptw =tcp_twsk (sk); - tcp_v4_send_ack(sock_net(sk), skb, + tcp_v4_send_ack(sk, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt, tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale, tcp_time_stamp + tcptw->tw_ts_offset,
@@ -818,7 +821,7 @@ static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,* exception of <SYN> segments, MUST be right-shifted by * Rcv.Wind.Shift bits: */ - tcp_v4_send_ack(sock_net(sk), skb, seq, + tcp_v4_send_ack(sk, skb, seq, tcp_rsk(req)->rcv_nxt, req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale, tcp_time_stamp,
net/ipv4/udp.c +2 -1
@@ -1019,7 +1019,8 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)flowi4_init_output (fl4, ipc.oif, sk->sk_mark, tos,RT_SCOPE_UNIVERSE , sk->sk_protocol, flow_flags, - faddr, saddr, dport, inet->inet_sport); + faddr, saddr, dport, inet->inet_sport, + sk->sk_uid);security_sk_classify_flow (sk,flowi4_to_flowi (fl4)); rt =ip_route_output_flow (net, fl4, sk);
net/ipv6/af_inet6.c +1 -0
@@ -678,6 +678,7 @@ int inet6_sk_rebuild_header(struct sock *sk) fl6.flowi6_mark = sk->sk_mark; fl6.fl6_dport = inet->inet_dport; fl6.fl6_sport = inet->inet_sport; + fl6.flowi6_uid = sk->sk_uid;security_sk_classify_flow (sk,flowi6_to_flowi (&fl6));rcu_read_lock ();
net/ipv6/ah6.c +3 -2
@@ -662,9 +662,10 @@ static int ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,return 0 ;if (type ==NDISC_REDIRECT ) -ip6_redirect (skb, net, skb->dev->ifindex,0 ); +ip6_redirect (skb, net, skb->dev->ifindex,0 , +sock_net_uid (net,NULL ));else -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));xfrm_state_put (x);return 0 ;
net/ipv6/datagram.c +1 -0
@@ -54,6 +54,7 @@ static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk) fl6->fl6_dport = inet->inet_dport; fl6->fl6_sport = inet->inet_sport; fl6->flowlabel = np->flow_label; + fl6->flowi6_uid = sk->sk_uid;if (!fl6->flowi6_oif) fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
net/ipv6/esp6.c +3 -2
@@ -474,9 +474,10 @@ static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,return 0 ;if (type ==NDISC_REDIRECT ) -ip6_redirect (skb, net, skb->dev->ifindex,0 ); +ip6_redirect (skb, net, skb->dev->ifindex,0 , +sock_net_uid (net,NULL ));else -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));xfrm_state_put (x);return 0 ;
net/ipv6/icmp.c +5 -2
@@ -92,9 +92,10 @@ static void icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,struct net *net =dev_net (skb->dev);if (type ==ICMPV6_PKT_TOOBIG ) -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));else if (type ==NDISC_REDIRECT ) -ip6_redirect (skb, net, skb->dev->ifindex,0 ); +ip6_redirect (skb, net, skb->dev->ifindex,0 , +sock_net_uid (net,NULL ));if (!(type &ICMPV6_INFOMSG_MASK ))if (icmp6->icmp6_type ==ICMPV6_ECHO_REQUEST )
@@ -484,6 +485,7 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info, fl6.flowi6_oif = iif; fl6.fl6_icmp_type = type; fl6.fl6_icmp_code = code; + fl6.flowi6_uid =sock_net_uid (net,NULL );security_skb_classify_flow (skb,flowi6_to_flowi (&fl6)); sk =icmpv6_xmit_lock (net);
@@ -658,6 +660,7 @@ static void icmpv6_echo_reply(struct sk_buff *skb) fl6.flowi6_oif = skb->dev->ifindex; fl6.fl6_icmp_type =ICMPV6_ECHO_REPLY ; fl6.flowi6_mark = mark; + fl6.flowi6_uid =sock_net_uid (net,NULL );security_skb_classify_flow (skb,flowi6_to_flowi (&fl6)); sk =icmpv6_xmit_lock (net);
net/ipv6/inet6_connection_sock.c +2 -0
@@ -88,6 +88,7 @@ struct dst_entry *inet6_csk_route_req(const struct sock *sk, fl6->flowi6_mark = ireq->ir_mark; fl6->fl6_dport = ireq->ir_rmt_port; fl6->fl6_sport =htons (ireq->ir_num); + fl6->flowi6_uid = sk->sk_uid;security_req_classify_flow (req,flowi6_to_flowi (fl6)); dst =ip6_dst_lookup_flow (sk, fl6, final_p);
@@ -136,6 +137,7 @@ static struct dst_entry *inet6_csk_route_socket(struct sock *sk, fl6->flowi6_mark = sk->sk_mark; fl6->fl6_sport = inet->inet_sport; fl6->fl6_dport = inet->inet_dport; + fl6->flowi6_uid = sk->sk_uid;security_sk_classify_flow (sk,flowi6_to_flowi (fl6));rcu_read_lock ();
net/ipv6/ip6_gre.c +4 -0
@@ -548,6 +548,8 @@ static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)if (t->parms.flags &IP6_TNL_F_USE_ORIG_FWMARK ) fl6.flowi6_mark = skb->mark; + fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); + err =gre_handle_offloads (skb, !!(t->parms.o_flags &TUNNEL_CSUM ));if (err)return -1 ;
@@ -602,6 +604,8 @@ static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)if (t->parms.flags &IP6_TNL_F_USE_ORIG_FWMARK ) fl6.flowi6_mark = skb->mark; + fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +if (gre_handle_offloads (skb, !!(t->parms.o_flags &TUNNEL_CSUM )))return -1 ;
net/ipv6/ip6_tunnel.c +4 -0
@@ -1240,6 +1240,8 @@ ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) fl6.flowi6_mark = skb->mark; } + fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +if (iptunnel_handle_offloads (skb,SKB_GSO_IPXIP6 ))return -1 ;
@@ -1318,6 +1320,8 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) fl6.flowi6_mark = skb->mark; } + fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +if (iptunnel_handle_offloads (skb,SKB_GSO_IPXIP6 ))return -1 ;
net/ipv6/ip6_vti.c +3 -2
@@ -608,9 +608,10 @@ static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,return 0 ;if (type ==NDISC_REDIRECT ) -ip6_redirect (skb, net, skb->dev->ifindex,0 ); +ip6_redirect (skb, net, skb->dev->ifindex,0 , +sock_net_uid (net,NULL ));else -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));xfrm_state_put (x);return 0 ;
net/ipv6/ipcomp6.c +3 -2
@@ -74,9 +74,10 @@ static int ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,return 0 ;if (type ==NDISC_REDIRECT ) -ip6_redirect (skb, net, skb->dev->ifindex,0 ); +ip6_redirect (skb, net, skb->dev->ifindex,0 , +sock_net_uid (net,NULL ));else -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));xfrm_state_put (x);return 0 ;
net/ipv6/netfilter.c +1 -0
@@ -26,6 +26,7 @@ int ip6_route_me_harder(struct net *net, struct sk_buff *skb)struct flowi6 fl6 = { .flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if :0 , .flowi6_mark = skb->mark, + .flowi6_uid =sock_net_uid (net, skb->sk), .daddr = iph->daddr, .saddr = iph->saddr, };
net/ipv6/ping.c +1 -0
@@ -113,6 +113,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) fl6.daddr = *daddr; fl6.flowi6_oif = oif; fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_uid = sk->sk_uid; fl6.fl6_icmp_type = user_icmph.icmp6_type; fl6.fl6_icmp_code = user_icmph.icmp6_code;security_sk_classify_flow (sk,flowi6_to_flowi (&fl6));
net/ipv6/raw.c +1 -0
@@ -776,6 +776,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)memset (&fl6,0 ,sizeof (fl6)); fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_uid = sk->sk_uid; ipc6.hlimit =-1 ; ipc6.tclass =-1 ;
net/ipv6/route.c +9 -4
@@ -1405,7 +1405,7 @@ static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, }void ip6_update_pmtu (struct sk_buff *skb,struct net *net,__be32 mtu, -int oif,u32 mark) +int oif,u32 mark,kuid_t uid) {const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;struct dst_entry *dst;
@@ -1417,6 +1417,7 @@ void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, fl6.daddr = iph->daddr; fl6.saddr = iph->saddr; fl6.flowlabel =ip6_flowinfo (iph); + fl6.flowi6_uid = uid; dst =ip6_route_output (net,NULL , &fl6);if (!dst->error)
@@ -1430,7 +1431,7 @@ void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)struct dst_entry *dst;ip6_update_pmtu (skb,sock_net (sk), mtu, - sk->sk_bound_dev_if, sk->sk_mark); + sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); dst =__sk_dst_get (sk); if (!dst || !dst->obsolete ||
@@ -1522,7 +1523,8 @@ static struct dst_entry *ip6_route_redirect(struct net *net, flags, __ip6_route_redirect); } -void ip6_redirect (struct sk_buff *skb,struct net *net,int oif,u32 mark) +void ip6_redirect (struct sk_buff *skb,struct net *net,int oif,u32 mark, +kuid_t uid) {const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;struct dst_entry *dst;
@@ -1535,6 +1537,7 @@ void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark) fl6.daddr = iph->daddr; fl6.saddr = iph->saddr; fl6.flowlabel =ip6_flowinfo (iph); + fl6.flowi6_uid = uid; dst =ip6_route_redirect (net, &fl6, &ipv6_hdr (skb)->saddr);rt6_do_redirect (dst,NULL , skb);
@@ -1556,6 +1559,7 @@ void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, fl6.flowi6_mark = mark; fl6.daddr = msg->dest; fl6.saddr = iph->daddr; + fl6.flowi6_uid =sock_net_uid (net,NULL ); dst =ip6_route_redirect (net, &fl6, &iph->saddr);rt6_do_redirect (dst,NULL , skb);
@@ -1564,7 +1568,8 @@ void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,void ip6_sk_redirect (struct sk_buff *skb,struct sock *sk) { -ip6_redirect (skb,sock_net (sk), sk->sk_bound_dev_if, sk->sk_mark); +ip6_redirect (skb,sock_net (sk), sk->sk_bound_dev_if, sk->sk_mark, + sk->sk_uid); }EXPORT_SYMBOL_GPL (ip6_sk_redirect);
net/ipv6/syncookies.c +1 -0
@@ -227,6 +227,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) fl6.flowi6_mark = ireq->ir_mark; fl6.fl6_dport = ireq->ir_rmt_port; fl6.fl6_sport =inet_sk (sk)->inet_sport; + fl6.flowi6_uid = sk->sk_uid;security_req_classify_flow (req,flowi6_to_flowi (&fl6)); dst =ip6_dst_lookup_flow (sk, &fl6, final_p);
net/ipv6/tcp_ipv6.c +2 -0
@@ -233,6 +233,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr, fl6.flowi6_mark = sk->sk_mark; fl6.fl6_dport = usin->sin6_port; fl6.fl6_sport = inet->inet_sport; + fl6.flowi6_uid = sk->sk_uid; opt =rcu_dereference_protected (np->opt,lockdep_sock_is_held (sk)); final_p =fl6_update_dst (&fl6, opt, &final);
@@ -824,6 +825,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 fl6.flowi6_mark =IP6_REPLY_MARK (net, skb->mark); fl6.fl6_dport = t1->dest; fl6.fl6_sport = t1->source; + fl6.flowi6_uid =sock_net_uid (net, sk &&sk_fullsock (sk) ? sk :NULL );security_skb_classify_flow (skb,flowi6_to_flowi (&fl6));/* Pass a socket to ip6_dst_lookup either it is for RST
net/ipv6/udp.c +1 -0
@@ -1138,6 +1138,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_uid = sk->sk_uid; sockc.tsflags = sk->sk_tsflags;if (msg->msg_controllen) {
net/l2tp/l2tp_ip6.c +1 -0
@@ -519,6 +519,7 @@ static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)memset (&fl6,0 ,sizeof (fl6)); fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_uid = sk->sk_uid; ipc6.hlimit =-1 ; ipc6.tclass =-1 ;
Signature · 1 line
2.8.0.rc3.226.g39d4020
David Miller
maintainerFrom: Lorenzo Colitti <[email protected]>
Date: Fri, 4 Nov 2016 02:23:40 +0900
Quoted 1 line
This patchset adds support for per-UID routing.
Looks great, thanks for all of the hard work.
Series applied.
Please submit the necessary iproute2 patches, as needed.
Thanks again.