Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sending raw packet over DGRAM socket with protocol type as ETH_P_ALL

457 views
Skip to first unread message

ittium

unread,
Sep 21, 2011, 12:34:49 AM9/21/11
to
Group,
Can I use packet DGRAM socket with protocol type as ETH_P_ALL

s = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) to send raw frames.


My doubt is if I do not specify the exact link layer protocol, how will
kernel module add the appropriate link layer header.

It seems to work fine if socket is of type SOCK_RAW since in this case
application will add the link layer header.

thanks
Ittium

Rainer Weikusat

unread,
Sep 21, 2011, 8:37:31 AM9/21/11
to
ittium <itt...@gmail.com> writes:
> Group,
> Can I use packet DGRAM socket with protocol type as ETH_P_ALL
>
> s = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) to send raw
> frames.

The protocol in the socket call is used to specify which types of
ethernet frames are supposed to be received on this socket.

> My doubt is if I do not specify the exact link layer protocol, how
> will kernel module add the appropriate link layer header.

If you want to send something using a packet socket, you'll need to
use sendto and specify a destination address in form of a struct
sockaddr_ll. This struct sockaddr_ll includes the interface index of
the interface supposed to be used to send the datagram and the
generated link-layer header will be one suitable for this interface.

Rainer Weikusat

unread,
Sep 21, 2011, 2:00:52 PM9/21/11
to
Rainer Weikusat <rwei...@mssgmbh.com> writes:
> ittium <itt...@gmail.com> writes:
>> Group,
>> Can I use packet DGRAM socket with protocol type as ETH_P_ALL
>>
>> s = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) to send raw
>> frames.
>
> The protocol in the socket call is used to specify which types of
> ethernet frames are supposed to be received on this socket.

While this is consistent with the documentation, it isn't really true.

>> My doubt is if I do not specify the exact link layer protocol, how
>> will kernel module add the appropriate link layer header.
>
> If you want to send something using a packet socket, you'll need to
> use sendto and specify a destination address in form of a struct
> sockaddr_ll. This struct sockaddr_ll includes the interface index of
> the interface supposed to be used to send the datagram and the
> generated link-layer header will be one suitable for this interface.

And while this is mostly true, it doesn't answer the question :-).

I've since had a look at the code which handles this in the kernel
(I'm assuming that this is a misposted Linux question). That's the
packet_snd routine in net/packet/af_packet.c and the relevant code is
this:

if (saddr == NULL) {
ifindex = po->ifindex;
proto = po->num;
addr = NULL;
} else {
err = -EINVAL;
if (msg->msg_namelen < sizeof(struct sockaddr_ll))
goto out;
if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
goto out;
ifindex = saddr->sll_ifindex;
proto = saddr->sll_protocol;
addr = saddr->sll_addr;
}

saddr is a pointer to the address specified in the send call (if
any). This means either an address was specified, then, a link layer
header suitable for the interface given as sll_ifindex using a
protocol of sll_protocol will be generated or no address was given and
then, interface index and protocol come from the socket. po->ifindex
comes from the sockaddr_ll structure passed as argument to an earlier
bind call and po->num is either protocol value from the same
sockaddr_ll structure or the one given in the socket call.

ittium

unread,
Sep 22, 2011, 12:32:31 AM9/22/11
to
Rainer,
thanks a lot for detailed explanation.

While sending packet over DGRAM packet socket, it is ok to open socket
with ETH_P_ALL since kernel will add the appropriate header after seeing
the information in destination address in sendto call.

You have written that interface index passed in address field of sendto
is interface supposed to be used to send the datagram, I do not think
it is true, I feel the **interface index should be index of destination
interface **

Rainer Weikusat

unread,
Sep 22, 2011, 10:37:06 AM9/22/11
to
ittium <itt...@gmail.com> writes:

[...]

> You have written that interface index passed in address field of
> sendto is interface supposed to be used to send the datagram, I do
> not think it is true, I feel the **interface index should be index of
> destination interface **

There is no such thing as 'a destination interface' for an ethernet
frame and even if there was, there would be no (hardware and software
independent) way of determining how the software running on some
device where some (set of) interface(s) received the frame refers to
these interfaces internally.

Valentin Nechayev

unread,
Sep 23, 2011, 10:58:20 AM9/23/11
to
>>> Rainer Weikusat wrote:

>> You have written that interface index passed in address field of
>> sendto is interface supposed to be used to send the datagram, I do
>> not think it is true, I feel the **interface index should be index of
>> destination interface **
RW> There is no such thing as 'a destination interface' for an ethernet
RW> frame and even if there was, there would be no (hardware and software
RW> independent) way of determining how the software running on some
RW> device where some (set of) interface(s) received the frame refers to
RW> these interfaces internally.

There is outgoing interface and I guess he meant it (and not myphical
"destination interface" on remote host)


-netch-

Valentin Nechayev

unread,
Sep 23, 2011, 11:01:34 AM9/23/11
to
>>> ittium wrote:

i> Can I use packet DGRAM socket with protocol type as ETH_P_ALL
i> s = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) to send raw frames.

The portable way is to use libnet and/or libpcap.
You can write simple example and trace how they do the real sending,
but it anyway can be specific to kernel version and possible some other
details.


-netch-

Rainer Weikusat

unread,
Sep 23, 2011, 11:47:59 AM9/23/11
to

I suggest that you reread the text above your statement more
carefully.

Rainer Weikusat

unread,
Sep 23, 2011, 11:49:18 AM9/23/11
to
ne...@segfault.kiev.ua (Valentin Nechayev) writes:
>>>> ittium wrote:
>
> i> Can I use packet DGRAM socket with protocol type as ETH_P_ALL
> i> s = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) to send raw frames.
>
> The portable way is to use libnet and/or libpcap.

Nobody asked for the BSD-developed 'one library to wrap them all!"
code.

ittium

unread,
Sep 24, 2011, 12:19:56 AM9/24/11
to
Rainer,
thanks for pointing this out, there is no standard way of finding
destination interface index.

while sending the message out, source MAC(or any other physical address)
address will be MAC address of local interface passed in sento call
destination address and destination MAC address will as usual, be
picked up from ARP cache.
thanks
Ittium


0 new messages