Introducing socket mark socket option
Subsystems (6)
PATCH 1/1 Introducing socket mark socket option
A userspace program may wish to set the mark for each packets its send without using the netfilter MARK target. Changing the mark can be used mark based routing without netfilter or for packet filtering.
It requires CAP_NET_ADMIN capability.
--- include/asm-alpha/socket.h | 2 ++ include/asm-arm/socket.h | 2 ++ include/asm-avr32/socket.h | 2 ++ include/asm-blackfin/socket.h | 3 +++ include/asm-cris/socket.h | 2 ++ include/asm-frv/socket.h | 2 ++ include/asm-h8300/socket.h | 2 ++ include/asm-ia64/socket.h | 2 ++ include/asm-m32r/socket.h | 2 ++ include/asm-m68k/socket.h | 2 ++ include/asm-mips/socket.h | 2 ++ include/asm-parisc/socket.h | 2 ++ include/asm-powerpc/socket.h | 2 ++ include/asm-s390/socket.h | 2 ++ include/asm-sh/socket.h | 2 ++ include/asm-sparc/socket.h | 2 ++ include/asm-sparc64/socket.h | 1 + include/asm-v850/socket.h | 2 ++ include/asm-x86/socket.h | 2 ++ include/asm-xtensa/socket.h | 2 ++ include/net/route.h | 2 ++ include/net/sock.h | 2 ++ net/core/sock.c | 11 +++++++++++ net/ipv4/icmp.c | 1 + net/ipv4/ip_output.c | 3 +++ net/ipv4/raw.c | 2 ++ 26 files changed, 61 insertions(+), 0 deletions(-)
include/asm-alpha/socket.h +2 -0
@@ -60,4 +60,6 @@#define SO_SECURITY_ENCRYPTION_TRANSPORT 20#define SO_SECURITY_ENCRYPTION_NETWORK 21 +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-arm/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-avr32/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* __ASM_AVR32_SOCKET_H */
include/asm-blackfin/socket.h +3 -0
@@ -50,4 +50,7 @@#define SO_PASSSEC 34#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS + +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-cris/socket.h +2 -0
@@ -54,6 +54,8 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-frv/socket.h +2 -0
@@ -52,5 +52,7 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-h8300/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-ia64/socket.h +2 -0
@@ -61,4 +61,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_IA64_SOCKET_H */
include/asm-m32r/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_M32R_SOCKET_H */
include/asm-m68k/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-mips/socket.h +2 -0
@@ -73,6 +73,8 @@ To add: #define SO_REUSEPORT 0x0200 /* Allow local address and port reuse. */#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#ifdef __KERNEL__/** sock_type - Socket types
include/asm-parisc/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_PEERSEC 0x401d#define SO_PASSSEC 0x401e +#define SO_MARK 0x401f +#endif /* _ASM_SOCKET_H */
include/asm-powerpc/socket.h +2 -0
@@ -59,4 +59,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_POWERPC_SOCKET_H */
include/asm-s390/socket.h +2 -0
@@ -60,4 +60,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-sh/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* __ASM_SH_SOCKET_H */
include/asm-sparc/socket.h +2 -0
@@ -52,6 +52,8 @@#define SO_TIMESTAMPNS 0x0021#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 0x0022 +/* Security levels - as per NRL IPv6 - don't actually do anything */ #define SO_SECURITY_AUTHENTICATION 0x5001#define SO_SECURITY_ENCRYPTION_TRANSPORT 0x5002
include/asm-sparc64/socket.h +1 -0
@@ -57,4 +57,5 @@#define SO_SECURITY_ENCRYPTION_TRANSPORT 0x5002#define SO_SECURITY_ENCRYPTION_NETWORK 0x5004 +#define SO_MARK 0x0022#endif /* _ASM_SOCKET_H */
include/asm-v850/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* __V850_SOCKET_H__ */
include/asm-x86/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-xtensa/socket.h +2 -0
@@ -63,4 +63,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _XTENSA_SOCKET_H */
include/net/route.h +2 -0
@@ -27,6 +27,7 @@#include <net/dst.h> #include <net/inetpeer.h> #include <net/flow.h> +#include <net/sock.h> #include <linux/in_route.h> #include <linux/rtnetlink.h> #include <linux/route.h>
@@ -148,6 +149,7 @@ static inline int ip_route_connect(struct rtable **rp, __be32 dst, int flags) {struct flowi fl = { .oif = oif, + .mark = sk->sk_mark, .nl_u = { .ip4_u = { .daddr = dst, .saddr = src, .tos = tos } },
include/net/sock.h +2 -0
@@ -262,6 +262,8 @@ struct sock {__u32 sk_sndmsg_off;int sk_write_pending;void *sk_security; +__u32 sk_mark; +/* XXX 4 bytes hole on 64 bit */ void (*sk_state_change)(struct sock *sk);void (*sk_data_ready)(struct sock *sk,int bytes);void (*sk_write_space)(struct sock *sk);
net/core/sock.c +11 -0
@@ -667,6 +667,13 @@ set_rcvbuf:else clear_bit (SOCK_PASSSEC , &sock->flags);break ; +case SO_MARK : +if (!capable (CAP_NET_ADMIN )) + ret = -EPERM ; +else { + sk->sk_mark = val; + } +break ;/* We implement the SO_SNDLOWAT etc to not be settable (1003.1g 5.3) */
@@ -836,6 +843,10 @@ int sock_getsockopt(struct socket *sock, int level, int optname,case SO_PEERSEC :return security_socket_getpeersec_stream (sock, optval, optlen, len); +case SO_MARK : + v.val = sk->sk_mark; +break ; +default :return -ENOPROTOOPT ; }
net/ipv4/icmp.c +1 -0
@@ -403,6 +403,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb) { .daddr = daddr, .saddr = rt->rt_spec_dst, .tos = RT_TOS(ip_hdr(skb)->tos) } }, + .mark = sk->sk_mark, .proto = IPPROTO_ICMP };security_skb_classify_flow (skb, &fl);if (ip_route_output_key (&rt, &fl))
net/ipv4/ip_output.c +3 -0
@@ -168,6 +168,7 @@ int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk, } skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark;/* Send it out. */ return ip_local_out (skb);
@@ -385,6 +386,7 @@ packet_routed: (skb_shinfo(skb)->gso_segs ?: 1) - 1); skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark;return ip_local_out (skb);
@@ -1282,6 +1284,7 @@ int ip_push_pending_frames(struct sock *sk) iph->daddr = rt->rt_dst; skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark; skb->dst =dst_clone (&rt->u.dst);if (iph->protocol ==IPPROTO_ICMP )
net/ipv4/raw.c +2 -0
@@ -352,6 +352,7 @@ static int raw_send_hdrinc(struct sock *sk, void *from, size_t length,skb_reserve (skb, hh_len); skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark; skb->dst =dst_clone (&rt->u.dst);skb_reset_network_header (skb);
@@ -544,6 +545,7 @@ static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, {struct flowi fl = { .oif = ipc.oif, + .mark = sk->sk_mark, .nl_u = { .ip4_u = { .daddr = daddr, .saddr = saddr,
Signature · 1 line
1.5.2.5
net/ipv4/icmp.c static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb) · line 406
Quoted 12 lines · Laszlo Attila Toth
A userspace program may wish to set the mark for each packets its send without using the netfilter MARK target. Changing the mark can be used mark based routing without netfilter or for packet filtering.
It requires
CAP_NET_ADMINcapability.net/ipv4/icmp.c +1
-0@@ -403,6 +403,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb) { .daddr = daddr, .saddr = rt->rt_spec_dst, .tos = RT_TOS(ip_hdr(skb)->tos) } }, + .mark = sk->sk_mark,
This is useless, the icmp socket is not visible to userspace.
net/ipv4/ip_output.c
Quoted 3 lines
net/ipv4/ip_output.c +0
-0
What about IPv6?
PATCH RESEND 1/1 Introducing socket mark socket option
A userspace program may wish to set the mark for each packets its send without using the netfilter MARK target. Changing the mark can be used for mark based routing without netfilter or for packet filtering.
It requires CAP_NET_ADMIN capability.
--- include/asm-alpha/socket.h | 2 ++ include/asm-arm/socket.h | 2 ++ include/asm-avr32/socket.h | 2 ++ include/asm-blackfin/socket.h | 3 +++ include/asm-cris/socket.h | 2 ++ include/asm-frv/socket.h | 2 ++ include/asm-h8300/socket.h | 2 ++ include/asm-ia64/socket.h | 2 ++ include/asm-m32r/socket.h | 2 ++ include/asm-m68k/socket.h | 2 ++ include/asm-mips/socket.h | 2 ++ include/asm-parisc/socket.h | 2 ++ include/asm-powerpc/socket.h | 2 ++ include/asm-s390/socket.h | 2 ++ include/asm-sh/socket.h | 2 ++ include/asm-sparc/socket.h | 2 ++ include/asm-sparc64/socket.h | 1 + include/asm-v850/socket.h | 2 ++ include/asm-x86/socket.h | 2 ++ include/asm-xtensa/socket.h | 2 ++ include/net/route.h | 2 ++ include/net/sock.h | 2 ++ net/core/sock.c | 11 +++++++++++ net/ipv4/ip_output.c | 3 +++ net/ipv4/raw.c | 2 ++ net/ipv6/ip6_output.c | 2 ++ net/ipv6/raw.c | 3 +++ 27 files changed, 65 insertions(+), 0 deletions(-)
include/asm-alpha/socket.h +2 -0
@@ -60,4 +60,6 @@#define SO_SECURITY_ENCRYPTION_TRANSPORT 20#define SO_SECURITY_ENCRYPTION_NETWORK 21 +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-arm/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-avr32/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* __ASM_AVR32_SOCKET_H */
include/asm-blackfin/socket.h +3 -0
@@ -50,4 +50,7 @@#define SO_PASSSEC 34#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS + +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-cris/socket.h +2 -0
@@ -54,6 +54,8 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-frv/socket.h +2 -0
@@ -52,5 +52,7 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-h8300/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-ia64/socket.h +2 -0
@@ -61,4 +61,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_IA64_SOCKET_H */
include/asm-m32r/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_M32R_SOCKET_H */
include/asm-m68k/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-mips/socket.h +2 -0
@@ -73,6 +73,8 @@ To add: #define SO_REUSEPORT 0x0200 /* Allow local address and port reuse. */#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#ifdef __KERNEL__/** sock_type - Socket types
include/asm-parisc/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_PEERSEC 0x401d#define SO_PASSSEC 0x401e +#define SO_MARK 0x401f +#endif /* _ASM_SOCKET_H */
include/asm-powerpc/socket.h +2 -0
@@ -59,4 +59,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_POWERPC_SOCKET_H */
include/asm-s390/socket.h +2 -0
@@ -60,4 +60,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-sh/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* __ASM_SH_SOCKET_H */
include/asm-sparc/socket.h +2 -0
@@ -52,6 +52,8 @@#define SO_TIMESTAMPNS 0x0021#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 0x0022 +/* Security levels - as per NRL IPv6 - don't actually do anything */ #define SO_SECURITY_AUTHENTICATION 0x5001#define SO_SECURITY_ENCRYPTION_TRANSPORT 0x5002
include/asm-sparc64/socket.h +1 -0
@@ -57,4 +57,5 @@#define SO_SECURITY_ENCRYPTION_TRANSPORT 0x5002#define SO_SECURITY_ENCRYPTION_NETWORK 0x5004 +#define SO_MARK 0x0022#endif /* _ASM_SOCKET_H */
include/asm-v850/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* __V850_SOCKET_H__ */
include/asm-x86/socket.h +2 -0
@@ -52,4 +52,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _ASM_SOCKET_H */
include/asm-xtensa/socket.h +2 -0
@@ -63,4 +63,6 @@#define SO_TIMESTAMPNS 35#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SO_MARK 36 +#endif /* _XTENSA_SOCKET_H */
include/net/route.h +2 -0
@@ -27,6 +27,7 @@#include <net/dst.h> #include <net/inetpeer.h> #include <net/flow.h> +#include <net/sock.h> #include <linux/in_route.h> #include <linux/rtnetlink.h> #include <linux/route.h>
@@ -149,6 +150,7 @@ static inline int ip_route_connect(struct rtable **rp, __be32 dst, int flags) {struct flowi fl = { .oif = oif, + .mark = sk->sk_mark, .nl_u = { .ip4_u = { .daddr = dst, .saddr = src, .tos = tos } },
include/net/sock.h +2 -0
@@ -262,6 +262,8 @@ struct sock {__u32 sk_sndmsg_off;int sk_write_pending;void *sk_security; +__u32 sk_mark; +/* XXX 4 bytes hole on 64 bit */ void (*sk_state_change)(struct sock *sk);void (*sk_data_ready)(struct sock *sk,int bytes);void (*sk_write_space)(struct sock *sk);
net/core/sock.c +11 -0
@@ -667,6 +667,13 @@ set_rcvbuf:else clear_bit (SOCK_PASSSEC , &sock->flags);break ; +case SO_MARK : +if (!capable (CAP_NET_ADMIN )) + ret = -EPERM ; +else { + sk->sk_mark = val; + } +break ;/* We implement the SO_SNDLOWAT etc to not be settable (1003.1g 5.3) */
@@ -836,6 +843,10 @@ int sock_getsockopt(struct socket *sock, int level, int optname,case SO_PEERSEC :return security_socket_getpeersec_stream (sock, optval, optlen, len); +case SO_MARK : + v.val = sk->sk_mark; +break ; +default :return -ENOPROTOOPT ; }
net/ipv4/ip_output.c +3 -0
@@ -168,6 +168,7 @@ int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk, } skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark;/* Send it out. */ return ip_local_out (skb);
@@ -385,6 +386,7 @@ packet_routed: (skb_shinfo(skb)->gso_segs ?: 1) - 1); skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark;return ip_local_out (skb);
@@ -1282,6 +1284,7 @@ int ip_push_pending_frames(struct sock *sk) iph->daddr = rt->rt_dst; skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark; skb->dst =dst_clone (&rt->u.dst);if (iph->protocol ==IPPROTO_ICMP )
net/ipv4/raw.c +2 -0
@@ -352,6 +352,7 @@ static int raw_send_hdrinc(struct sock *sk, void *from, size_t length,skb_reserve (skb, hh_len); skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark; skb->dst =dst_clone (&rt->u.dst);skb_reset_network_header (skb);
@@ -544,6 +545,7 @@ static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, {struct flowi fl = { .oif = ipc.oif, + .mark = sk->sk_mark, .nl_u = { .ip4_u = { .daddr = daddr, .saddr = saddr,
net/ipv6/ip6_output.c +2 -0
@@ -257,6 +257,7 @@ int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,ipv6_addr_copy (&hdr->daddr, first_hop); skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark; mtu =dst_mtu (dst);if ((skb->len <= mtu) || ipfragok ||skb_is_gso (skb)) {
@@ -1437,6 +1438,7 @@ int ip6_push_pending_frames(struct sock *sk)ipv6_addr_copy (&hdr->daddr, final_dst); skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark; skb->dst =dst_clone (&rt->u.dst);IP6_INC_STATS (rt->rt6i_idev,IPSTATS_MIB_OUTREQUESTS );
net/ipv6/raw.c +3 -0
@@ -641,6 +641,7 @@ static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,skb_reserve (skb, hh_len); skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark; skb->dst =dst_clone (&rt->u.dst);skb_put (skb, length);
@@ -767,6 +768,8 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,*/ memset (&fl,0 ,sizeof (fl)); + fl.mark = sk->sk_mark; +if (sin6) {if (addr_len <SIN6_LEN_RFC2133 )return -EINVAL ;
Signature · 1 line
1.5.2.5
Quoted 5 lines · Laszlo Attila Toth
A userspace program may wish to set the mark for each packets its send without using the netfilter MARK target. Changing the mark can be used for mark based routing without netfilter or for packet filtering.
It requires
CAP_NET_ADMINcapability.
Looks good to me.
From: Patrick McHardy <[email protected]>
Date: Thu, 24 Jan 2008 10:43:48 +0100
Quoted 9 lines
Quoted 5 lines · Laszlo Attila Toth
A userspace program may wish to set the mark for each packets its send without using the netfilter MARK target. Changing the mark can be used for mark based routing without netfilter or for packet filtering.
It requires
CAP_NET_ADMINcapability.Looks good to me.
Applied, thanks.