change mbuf header
struct mbuf is used by the kernel code to transfer data-packets around.
What if we need to send data through different protocols? Well since most protocols have headers describing the data packet and its data, these need to be changed.
If we have a struct mbuf containing a packet of some protocol and want to change it to another protocol, what is the best way to do this exchange?
Well, it depends on what you mean by best. Maybe by just fetching a new mbuf and copy the new header plus data.
But I came up with a way to do it another way, maybe more logical? Idea is try to just replace the header, at least from our point of view.
First we duplicate mbuf:
m_dup(struct mbuf *mbuf, int how);
Then we adjust the data pointer to point after the old protocol header:
m_adj(struct mbuf *mbuf, int len)
After that we make place for the new header
M_PREPEND(struct mbuf *mbuf, int len, int how);
Now we copy the new header to the beginning of the mbuf. But if we prepend a bigger header than the old one we get two mbufs linked in a chain. If the new chained mbuf is a problem for you, then this is the last step to merge them into one mbuf:
struct mbuf *m_defrag(struct mbuf *m0, int how);
Thats it! Well now we have to verify if this solution is fast enough? ;)








