netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Accessing packet marking functions
@ 2010-06-19 16:42 Andrew Beverley
  2010-06-19 20:01 ` Jan Engelhardt
  0 siblings, 1 reply; 17+ messages in thread
From: Andrew Beverley @ 2010-06-19 16:42 UTC (permalink / raw)
  To: netfilter-devel

I am considering patching Squid proxy so that it retains a packet's mark
value if it could not be fetched from the cache. Squid already has
similar functionality for the TOS field, but I would like to extend this
to netfilter's mark feature.

Can somebody point me in the right direction for the correct way of
setting and accessing the mark value of a packet? The TOS feature in
Squid uses setsockopt(). Is there an equivalent for mark? Should I be
using libnetfilter_queue?

Thanks in advance,

Andy



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-19 16:42 Accessing packet marking functions Andrew Beverley
@ 2010-06-19 20:01 ` Jan Engelhardt
  2010-06-19 20:50   ` Andrew Beverley
                     ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Jan Engelhardt @ 2010-06-19 20:01 UTC (permalink / raw)
  To: Andrew Beverley; +Cc: netfilter-devel

On Saturday 2010-06-19 18:42, Andrew Beverley wrote:

>I am considering patching Squid proxy so that it retains a packet's mark
>value if it could not be fetched from the cache. Squid already has
>similar functionality for the TOS field, but I would like to extend this
>to netfilter's mark feature.
>
>Can somebody point me in the right direction for the correct way of
>setting and accessing the mark value of a packet? The TOS feature in
>Squid uses setsockopt(). Is there an equivalent for mark? Should I be
>using libnetfilter_queue?

setsockopt(fd, SOL_SOCKET, SO_MARK, ...)

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-19 20:01 ` Jan Engelhardt
@ 2010-06-19 20:50   ` Andrew Beverley
  2010-06-20 11:16   ` Andrew Beverley
  2010-06-29 21:22   ` Andrew Beverley
  2 siblings, 0 replies; 17+ messages in thread
From: Andrew Beverley @ 2010-06-19 20:50 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: netfilter-devel

> >Can somebody point me in the right direction for the correct way of
> >setting and accessing the mark value of a packet? The TOS feature in
> >Squid uses setsockopt(). Is there an equivalent for mark? Should I be
> >using libnetfilter_queue?
> 
> setsockopt(fd, SOL_SOCKET, SO_MARK, ...)

Thanks Jan. I was honestly googling that for ages :-)

Not really for discussion here, but the whole concept of having a proxy
server in combination with traffic shaping rules makes things
challenging. All my traffic shaping rules are based on source and
destination interfaces, ports, and client addresses, so once a proxy is
thrown in, that sort of information is unavailable, as all the traffic
is no longer forwarded, but instead goes to and from the local host...

Andy



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-19 20:01 ` Jan Engelhardt
  2010-06-19 20:50   ` Andrew Beverley
@ 2010-06-20 11:16   ` Andrew Beverley
  2010-06-20 11:52     ` Jan Engelhardt
  2010-06-29 21:22   ` Andrew Beverley
  2 siblings, 1 reply; 17+ messages in thread
From: Andrew Beverley @ 2010-06-20 11:16 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: netfilter-devel

> >Can somebody point me in the right direction for the correct way of
> >setting and accessing the mark value of a packet? The TOS feature in
> >Squid uses setsockopt(). Is there an equivalent for mark? Should I be
> >using libnetfilter_queue?
> 
> setsockopt(fd, SOL_SOCKET, SO_MARK, ...)

I am getting the error "Operation not permitted" when trying to do this.
Is this because the packet is not in a state where it can be marked, or
am I doing something stupid? My code is:

int nfmark = 255;
setsockopt(fd, SOL_SOCKET, SO_MARK, (int *) &nfmark, sizeof(int));


The following lines of code in the same place work okay to set the TOS:

int nfmark = 255;
setsockopt(fd, IPPROTO_IP, IP_TOS, (int *) &nfmark, sizeof(int));


Thanks,

Andy



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-20 11:16   ` Andrew Beverley
@ 2010-06-20 11:52     ` Jan Engelhardt
  2010-06-20 12:31       ` Andrew Beverley
  0 siblings, 1 reply; 17+ messages in thread
From: Jan Engelhardt @ 2010-06-20 11:52 UTC (permalink / raw)
  To: Andrew Beverley; +Cc: netfilter-devel

On Sunday 2010-06-20 13:16, Andrew Beverley wrote:

>> >Can somebody point me in the right direction for the correct way of
>> >setting and accessing the mark value of a packet? The TOS feature in
>> >Squid uses setsockopt(). Is there an equivalent for mark? Should I be
>> >using libnetfilter_queue?
>> 
>> setsockopt(fd, SOL_SOCKET, SO_MARK, ...)
>
>I am getting the error "Operation not permitted" when trying to do this.
>Is this because the packet is not in a state where it can be marked, or
>am I doing something stupid? My code is:
>
>int nfmark = 255;
>setsockopt(fd, SOL_SOCKET, SO_MARK, (int *) &nfmark, sizeof(int));
>
>
>The following lines of code in the same place work okay to set the TOS:
>
>int nfmark = 255;
>setsockopt(fd, IPPROTO_IP, IP_TOS, (int *) &nfmark, sizeof(int));

1. Do away with the pointless casts.
2. Needs root privileges.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-20 11:52     ` Jan Engelhardt
@ 2010-06-20 12:31       ` Andrew Beverley
  2010-06-22  6:16         ` Patrick McHardy
  0 siblings, 1 reply; 17+ messages in thread
From: Andrew Beverley @ 2010-06-20 12:31 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: netfilter-devel

> >> >Can somebody point me in the right direction for the correct way of
> >> >setting and accessing the mark value of a packet? The TOS feature in
> >> >Squid uses setsockopt(). Is there an equivalent for mark? Should I be
> >> >using libnetfilter_queue?
> >> 
> >> setsockopt(fd, SOL_SOCKET, SO_MARK, ...)
> >
> >I am getting the error "Operation not permitted" when trying to do this.
> >Is this because the packet is not in a state where it can be marked, or
> >am I doing something stupid? My code is:
> >
> >int nfmark = 255;
> >setsockopt(fd, SOL_SOCKET, SO_MARK, (int *) &nfmark, sizeof(int));

> 1. Do away with the pointless casts.
> 2. Needs root privileges.

Thanks, that works now when running as root (with pointless casts
removed).

The problem is that Squid normally runs as a non-privileged user (I had
to remove the root checks from the code to get it to run as root). Is
there any way to mark packets when not root? Or is the only way to make
this work to run a small part of Squid as root?

Thanks,

Andy



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-20 12:31       ` Andrew Beverley
@ 2010-06-22  6:16         ` Patrick McHardy
  2010-06-28 21:21           ` Andrew Beverley
  0 siblings, 1 reply; 17+ messages in thread
From: Patrick McHardy @ 2010-06-22  6:16 UTC (permalink / raw)
  To: Andrew Beverley; +Cc: Jan Engelhardt, netfilter-devel

Andrew Beverley wrote:
>>>>> Can somebody point me in the right direction for the correct way of
>>>>> setting and accessing the mark value of a packet? The TOS feature in
>>>>> Squid uses setsockopt(). Is there an equivalent for mark? Should I be
>>>>> using libnetfilter_queue?
>>>>>           
>>>> setsockopt(fd, SOL_SOCKET, SO_MARK, ...)
>>>>         
>>> I am getting the error "Operation not permitted" when trying to do this.
>>> Is this because the packet is not in a state where it can be marked, or
>>> am I doing something stupid? My code is:
>>>
>>> int nfmark = 255;
>>> setsockopt(fd, SOL_SOCKET, SO_MARK, (int *) &nfmark, sizeof(int));
>>>       
>
>   
>> 1. Do away with the pointless casts.
>> 2. Needs root privileges.
>>     
>
> Thanks, that works now when running as root (with pointless casts
> removed).
>
> The problem is that Squid normally runs as a non-privileged user (I had
> to remove the root checks from the code to get it to run as root). Is
> there any way to mark packets when not root? Or is the only way to make
> this work to run a small part of Squid as root?


enter_suid()/leave_suid().

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-22  6:16         ` Patrick McHardy
@ 2010-06-28 21:21           ` Andrew Beverley
  2010-06-28 21:45             ` Jan Engelhardt
  0 siblings, 1 reply; 17+ messages in thread
From: Andrew Beverley @ 2010-06-28 21:21 UTC (permalink / raw)
  To: Patrick McHardy; +Cc: Jan Engelhardt, netfilter-devel

> > The problem is that Squid normally runs as a non-privileged user (I had
> > to remove the root checks from the code to get it to run as root). Is
> > there any way to mark packets when not root? Or is the only way to make
> > this work to run a small part of Squid as root?
> 
> 
> enter_suid()/leave_suid().

Thanks, although in the end I have decided to try and use the
CAP_NET_ADMIN capability flag instead, to keep the use of root to a
minimum.

Cheers,

Andy



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-28 21:21           ` Andrew Beverley
@ 2010-06-28 21:45             ` Jan Engelhardt
  0 siblings, 0 replies; 17+ messages in thread
From: Jan Engelhardt @ 2010-06-28 21:45 UTC (permalink / raw)
  To: Andrew Beverley; +Cc: Patrick McHardy, netfilter-devel


On Monday 2010-06-28 23:21, Andrew Beverley wrote:

>> > The problem is that Squid normally runs as a non-privileged user (I had
>> > to remove the root checks from the code to get it to run as root). Is
>> > there any way to mark packets when not root? Or is the only way to make
>> > this work to run a small part of Squid as root?
>> 
>> enter_suid()/leave_suid().
>
>Thanks, although in the end I have decided to try and use the
>CAP_NET_ADMIN capability flag instead, to keep the use of root to a
>minimum.

Hey, I've you're thatmuch worried, you'd be using selinux ;-)

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-19 20:01 ` Jan Engelhardt
  2010-06-19 20:50   ` Andrew Beverley
  2010-06-20 11:16   ` Andrew Beverley
@ 2010-06-29 21:22   ` Andrew Beverley
  2010-06-29 21:35     ` Maciej Żenczykowski
  2 siblings, 1 reply; 17+ messages in thread
From: Andrew Beverley @ 2010-06-29 21:22 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: netfilter-devel

> >I am considering patching Squid proxy so that it retains a packet's mark
> >value if it could not be fetched from the cache. Squid already has
> >similar functionality for the TOS field, but I would like to extend this
> >to netfilter's mark feature.
> >
> >Can somebody point me in the right direction for the correct way of
> >setting and accessing the mark value of a packet? The TOS feature in
> >Squid uses setsockopt(). Is there an equivalent for mark? Should I be
> >using libnetfilter_queue?
> 
> setsockopt(fd, SOL_SOCKET, SO_MARK, ...)

Thanks for the help so far. To retrieve the mark should I just be using:

getsockopt(fd, SOL_SOCKET, SO_MARK, ...) ?

I keep getting a mark of 0 despite setting a mark in PREROUTING. My code
is as follows:

int mark = 0;
int marklen = sizeof(mark);
getsockopt(newsocket, SOL_SOCKET, SO_MARK, &mark, &marklen);



Thanks,

Andy



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-29 21:22   ` Andrew Beverley
@ 2010-06-29 21:35     ` Maciej Żenczykowski
  2010-06-30  6:14       ` Andrew Beverley
  0 siblings, 1 reply; 17+ messages in thread
From: Maciej Żenczykowski @ 2010-06-29 21:35 UTC (permalink / raw)
  To: Andrew Beverley; +Cc: Jan Engelhardt, netfilter-devel

That retrieves the socket mark, not the packet mark.
The packet mark on outgoing packets gets initialized to the socket mark...

On Tue, Jun 29, 2010 at 14:22, Andrew Beverley <andy@andybev.com> wrote:
>> >I am considering patching Squid proxy so that it retains a packet's mark
>> >value if it could not be fetched from the cache. Squid already has
>> >similar functionality for the TOS field, but I would like to extend this
>> >to netfilter's mark feature.
>> >
>> >Can somebody point me in the right direction for the correct way of
>> >setting and accessing the mark value of a packet? The TOS feature in
>> >Squid uses setsockopt(). Is there an equivalent for mark? Should I be
>> >using libnetfilter_queue?
>>
>> setsockopt(fd, SOL_SOCKET, SO_MARK, ...)
>
> Thanks for the help so far. To retrieve the mark should I just be using:
>
> getsockopt(fd, SOL_SOCKET, SO_MARK, ...) ?
>
> I keep getting a mark of 0 despite setting a mark in PREROUTING. My code
> is as follows:
>
> int mark = 0;
> int marklen = sizeof(mark);
> getsockopt(newsocket, SOL_SOCKET, SO_MARK, &mark, &marklen);
>
>
>
> Thanks,
>
> Andy
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-29 21:35     ` Maciej Żenczykowski
@ 2010-06-30  6:14       ` Andrew Beverley
  2010-06-30  6:15         ` Jan Engelhardt
  0 siblings, 1 reply; 17+ messages in thread
From: Andrew Beverley @ 2010-06-30  6:14 UTC (permalink / raw)
  To: Maciej Żenczykowski; +Cc: Jan Engelhardt, netfilter-devel

On Tue, 2010-06-29 at 14:35 -0700, Maciej Żenczykowski wrote:
> That retrieves the socket mark, not the packet mark.
> The packet mark on outgoing packets gets initialized to the socket mark...

Hmmm, I understand. So is there any way to retrieve a packet's mark as
opposed to a socket's mark?

Thanks,

Andy


--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-30  6:14       ` Andrew Beverley
@ 2010-06-30  6:15         ` Jan Engelhardt
  2010-06-30  6:32           ` Andrew Beverley
  0 siblings, 1 reply; 17+ messages in thread
From: Jan Engelhardt @ 2010-06-30  6:15 UTC (permalink / raw)
  To: Andrew Beverley; +Cc: Maciej Żenczykowski, netfilter-devel

On Wednesday 2010-06-30 08:14, Andrew Beverley wrote:
>
>On Tue, 2010-06-29 at 14:35 -0700, Maciej Żenczykowski wrote:
>> That retrieves the socket mark, not the packet mark.
>> The packet mark on outgoing packets gets initialized to the socket mark...
>
>Hmmm, I understand. So is there any way to retrieve a packet's mark as
>opposed to a socket's mark?

And how would you do that with SOCK_STREAM anyway?
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-30  6:15         ` Jan Engelhardt
@ 2010-06-30  6:32           ` Andrew Beverley
  2010-06-30  6:47             ` Jan Engelhardt
  0 siblings, 1 reply; 17+ messages in thread
From: Andrew Beverley @ 2010-06-30  6:32 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: Maciej Żenczykowski, netfilter-devel

> >> That retrieves the socket mark, not the packet mark.
> >> The packet mark on outgoing packets gets initialized to the socket mark...
> >
> >Hmmm, I understand. So is there any way to retrieve a packet's mark as
> >opposed to a socket's mark?
> 
> And how would you do that with SOCK_STREAM anyway?

I don't know. I'll admit that I don't fully understand what I'm doing
here, which I apologise for, but I'm trying to learn.

All I want to do is retain a packet's mark from its arrival into Squid,
onto its transmission to the client. Something like this:


ppp0 -> PREROUTING -> Squid -> POSTROUTING -> eth0

            ^^                      ^^
         Set Mark               Read mark


If this isn't possible then please tell me.

Thanks,

Andy



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-30  6:32           ` Andrew Beverley
@ 2010-06-30  6:47             ` Jan Engelhardt
  2010-10-24 17:30               ` Andrew Beverley
  0 siblings, 1 reply; 17+ messages in thread
From: Jan Engelhardt @ 2010-06-30  6:47 UTC (permalink / raw)
  To: Andrew Beverley; +Cc: Maciej Żenczykowski, netfilter-devel


On Wednesday 2010-06-30 08:32, Andrew Beverley wrote:
>> >> That retrieves the socket mark, not the packet mark.
>> >> The packet mark on outgoing packets gets initialized to the socket mark...
>> >
>> >Hmmm, I understand. So is there any way to retrieve a packet's mark as
>> >opposed to a socket's mark?
>> 
>> And how would you do that with SOCK_STREAM anyway?
>
>I don't know. I'll admit that I don't fully understand what I'm doing
>here, which I apologise for, but I'm trying to learn.
>
>All I want to do is retain a packet's mark from its arrival into Squid,
>onto its transmission to the client. Something like this:
>
>
>ppp0 -> PREROUTING -> Squid -> POSTROUTING -> eth0
>
>            ^^                      ^^
>         Set Mark               Read mark
>
>If this isn't possible then please tell me.

Hm, interesting case. I would say you could:

 - use CONNMARK in PREROUTING/INPUT
 - use libnetfilter_conntrack to query the connmark from within squid
   (since squid has address and port, that should identify the 
   connection within the nfct table)
 - use the so-obtained ctmark to populate the new socket's skmark

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-06-30  6:47             ` Jan Engelhardt
@ 2010-10-24 17:30               ` Andrew Beverley
  2010-10-24 23:55                 ` Jan Engelhardt
  0 siblings, 1 reply; 17+ messages in thread
From: Andrew Beverley @ 2010-10-24 17:30 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: Maciej Żenczykowski, netfilter-devel

> >All I want to do is retain a packet's mark from its arrival into Squid,
> >onto its transmission to the client. Something like this:
> >
> >
> >ppp0 -> PREROUTING -> Squid -> POSTROUTING -> eth0
> >
> >            ^^                      ^^
> >         Set Mark               Read mark
> >
> >If this isn't possible then please tell me.
> 
> Hm, interesting case. I would say you could:
> 
>  - use CONNMARK in PREROUTING/INPUT
>  - use libnetfilter_conntrack to query the connmark from within squid
>    (since squid has address and port, that should identify the 
>    connection within the nfct table)
>  - use the so-obtained ctmark to populate the new socket's skmark

Thanks for this suggestion. Thought I'd drop a quick email (for
completeness) to say that the patch for this has now been included into
Squid. So, it is now possible for Squid to retain the mark on packets
for items that aren't cached, or set a mark on packets when items are
fetched from the cache.

Andy



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Accessing packet marking functions
  2010-10-24 17:30               ` Andrew Beverley
@ 2010-10-24 23:55                 ` Jan Engelhardt
  0 siblings, 0 replies; 17+ messages in thread
From: Jan Engelhardt @ 2010-10-24 23:55 UTC (permalink / raw)
  To: Andrew Beverley; +Cc: Maciej Żenczykowski, netfilter-devel


On Sunday 2010-10-24 19:30, Andrew Beverley wrote:
>> >All I want to do is retain a packet's mark from its arrival into Squid,
>> >onto its transmission to the client. Something like this:
>> >
>> >
>> >ppp0 -> PREROUTING -> Squid -> POSTROUTING -> eth0
>> >
>> >            ^^                      ^^
>> >         Set Mark               Read mark
>> >
>> >If this isn't possible then please tell me.
>> 
>> Hm, interesting case. I would say you could:
>> 
>>  - use CONNMARK in PREROUTING/INPUT
>>  - use libnetfilter_conntrack to query the connmark from within squid
>>    (since squid has address and port, that should identify the 
>>    connection within the nfct table)
>>  - use the so-obtained ctmark to populate the new socket's skmark
>
>Thanks for this suggestion. Thought I'd drop a quick email (for
>completeness) to say that the patch for this has now been included into
>Squid. So, it is now possible for Squid to retain the mark on packets
>for items that aren't cached, or set a mark on packets when items are
>fetched from the cache.

I looked at the change in the squid SCM and...

libnetfilter_conntrack offers .pc files, so squid's configure.ac
should make use of PKG_CHECK_MODULES.

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2010-10-24 23:55 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-06-19 16:42 Accessing packet marking functions Andrew Beverley
2010-06-19 20:01 ` Jan Engelhardt
2010-06-19 20:50   ` Andrew Beverley
2010-06-20 11:16   ` Andrew Beverley
2010-06-20 11:52     ` Jan Engelhardt
2010-06-20 12:31       ` Andrew Beverley
2010-06-22  6:16         ` Patrick McHardy
2010-06-28 21:21           ` Andrew Beverley
2010-06-28 21:45             ` Jan Engelhardt
2010-06-29 21:22   ` Andrew Beverley
2010-06-29 21:35     ` Maciej Żenczykowski
2010-06-30  6:14       ` Andrew Beverley
2010-06-30  6:15         ` Jan Engelhardt
2010-06-30  6:32           ` Andrew Beverley
2010-06-30  6:47             ` Jan Engelhardt
2010-10-24 17:30               ` Andrew Beverley
2010-10-24 23:55                 ` Jan Engelhardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).