Skip to contentSkip to conversation

Sanity Check: Getting priority information to QUEUE (long)

DISCUSSION Scott MacKay 2 participants 4 messages Mar 31, 2004 – Apr 1, 2004 netfilter

lore
Share & tools
lore
#0 lore raw

Hi!
Sorry if this post rambles :)
Thanks again to Henrik on the great info for frags and the order of things...

I had a need in the QUEUE target with an idea on how to get there. I wanted to pass it by here quickly as a sanity check on my thinking before I burn down my kernel. I do not see any current method that priority is passed to the QUEUE target. No matter my solution, it will have elements of ugliness since I will need to alter the kernel code for ip_queue.

In the QUEUE userspace, it would be really useful if I could get the priority information on the delivered packet. This would help me if I register QUEUE for both raw/PREROUTING and mangle/PREROUTING to know where I am.

I see in ip_queue.c that the key function hooked is ipq_enqueue_packet. This guy gets both the skb and also a nf_info structure. Looking at the nf_info, it has a "nf_hook_ops *elem". This structure looks like what is used to register locations/priorities .

Question: The nf_info's elem field which gets passed to hooked functions: does it just contain 0-1 nf_hook_ops structures or 0-x? If 0-1, then I would guess the entry has the elem->priority field which would be the priority this hook rode in on. If that is not the case, then how could I use the nf_hook_ops to get the priority info? Is there ver a case where elem==NULL? It would seem not likely, since all hooks look like they register the nf_hook_ops they serve. The concern I have about the data in elem is that the hook field (which also exists in the nf_hook_ops) is *not* gotten from this structure during QUEUE packaging, but rather from the nf_info itself. I am hoping the nf_info's hook field is just a redundant thing.

ipq_enqueue_packet puts together it's ipq_queue_entry stuff and calls ipq_build_packet_message which is where the packaging to userspace occurs. It populates a ipq_packet_msg structure which is what will get sent to the userspace. This structure does have an int hook field which is the proper hook for the packet. It lacks a priority field though.

Providing I can get the priority from the incoming structures, the next part is how to deliver it up. I have 3 ways: 'dirty, fast & ugly', 'super dirty', or cleaner,less compatible.

Dirty. My idea for dirty is 'hey, hook is like 5 values 0-4 and I don't see it growing into a billion more options. priorities are well within a -32K to 32K range. "hook" is an unsigned int (4 bytes). Masking time!!!!
My dirty idea was to steal 2 bytes from the hook field being passed up. I would place the priority in the upper 2 bits and keep the lower 2 bits for the hook number. This has the advantage that I will not alter the size of the message structure being passed up. Ugly, relying on a int being 4 bytes and such. This is really nice since it just needs a kernel recompile. Along the same though but a different field, I could do the same to the mark field. I plan to use it, but well within a 2 byte range; no need for 4 bytes that it has.
Neither of the above hurt anyone (other than different people's QUEUEs :) as the data is single direction: it never gets updated back in the kernel (though I think there may be a patch to allow mark updates). The last idea: add a field to the ipq_packet_msg structure. As ugly as the prev are, I hate this the most since there is no way a QUEUE will know this is there anyways and if not recompiled against an updated header, it will have a bad offset for the data field and stuff. As 'proper' as this may be, it seems like it could cause the most damage to others.

As a nice compromise I was thinking this:
What if I altered the code to overload hook, since priority is most related to hook anyways. I could then make a dummy kernel module, something insmod-able. When loaded, it does the overload. When not loaded, hook acts normally.
It does nothing other than have a flag which ipq_build_packet_msg tries to read during the ipq_packet_msg construction (I will need to read up on how this is done).

__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html

#1 lore raw
Quoted 5 lines · Scott MacKay

In the QUEUE userspace, it would be really useful if I could get the priority information on the delivered packet. This would help me if I register QUEUE for both raw/PREROUTING and mangle/PREROUTING to know where I am.

Right.

You do however have the nfmark field which can be used for pretty much any purpose..

Quoted 6 lines

You do not have the exact priority within the hook Dirty. My idea for dirty is 'hey, hook is like 5 values 0-4 and I don't see it growing into a billion more options. priorities are well within a -32K to 32K range. "hook" is an unsigned int (4 bytes). Masking time!!!!

What is wrong with adding a new field for this? (or even splitting the hook field in two 16 bit fields for the purpose..)

Yes, adding a new field with break binary compatibility, but so will adding data to the hook field by masking.

Quoted 5 lines

The last idea: add a field to the ipq_packet_msg structure. As ugly as the prev are, I hate this the most since there is no way a QUEUE will know this is there anyways and if not recompiled against an updated header

I do not consider this a problem.

If this really worries you then extend ip_queue with a new mode or option to enable the use of this extended ipq_packet_msg_v2 structure. (adding an IPQM_VERSION option comes to mind...)

Regards
Henrik

#2 lore raw
Quoted 5 lines · --- Henrik Nordstrom

On Wed, 31 Mar 2004, Scott MacKay wrote:

You do however have the nfmark field which can be used for pretty much any
purpose..

Very true, but I was hoping to use that for other things. As is, I would like a persistent field in addition to MARK, but that is a different story. I think I will check the patch-o-matic for other add-ons which may serve this function...

Quoted 5 lines

If this really worries you then extend ip_queue with a new mode or option
to enable the use of this extended ipq_packet_msg_v2 structure.
(adding an IPQM_VERSION option comes to mind...)

That actually may be the cleanest...I have not looked at how to send commands to the ip_queue directly, but that could save a module insert and allow me to extend alot more than I currently am doing.

Oh, also, from what I found to answer one of my own
questions: the info elem hook does in fact contain a pointer to the nf_hook_ops for what invoked this app. This makes the hook & priority from that structure available to me, wohoo!

__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html

#3 lore raw

metadata association without structure alteration?

In reply to Henrik Nordstrom

Hello!
I was wondering, is there any way to associate some
arbitrary metadata with a packet as it traverses the netfilter stages without altering a skb or nf_info structure? Currently, I think alterations I want to make to play with packets is limited to ip_queue and it's private structures. I would like to be able to associate some data, though, with a packet (much like the netfilter mark) during it's stay in the netfilter system, but would prefer a method where I do not need to alter any shared header structure. For me, this metadata would only need to be accessible from the ip_queue.c module. I could use a private list, but the only thing is I would not know when to release the metadata as the packet gets dropped/delivered. Maybe a way to register a callback when a packet gets added, dropped or otherwise enters/leave the whole netfilter thing? Thanks in advance!!

-Scott

__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway
http://promotions.yahoo.com/design_giveaway/