public inbox for developer@lists.illumos.org (since 2011-08)
 help / color / mirror / Atom feed
From: Denis Kozadaev <denis@tambov.ru>
To: developer@lists.illumos.org
Cc: Marcel Telka <marcel@telka.sk>
Subject: Re: [developer] Raw ethernet packets
Date: Fri, 31 May 2024 17:15:39 +0300	[thread overview]
Message-ID: <202405311715.39644.denis@tambov.ru> (raw)
In-Reply-To: <ZlnRkTtbgteyGih2@telcontar>

[-- Attachment #1: Type: text/plain, Size: 3777 bytes --]

Ok, I tryed to reproduce your task.
Speaking shortly it works, see the attached file.
A test system is a VM undef FreeBSD bhyve, the global zone has an interface:
vioif0: flags=1000943<UP,BROADCAST,RUNNING,PROMISC,MULTICAST,IPv4> mtu 1500 
index 2
        inet 192.168.1.3 netmask ffffff00 broadcast 192.168.1.255
Over this nic I use VNIC and test it in a non-global zone:
LINK         OVER       SPEED    MACADDRESS        MACADDRTYPE VID  ZONE
dev0         vioif0     10000    2:8:20:2d:76:5d   fixed       0    dev01

Now the non-global zone:
dev01% ifconfig dev0
dev0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 192.168.1.10 netmask ffffff00 broadcast 192.168.1.255

dev01% gcc -o tst main.c
dev01% pfexec id
uid=0(root) gid=0(root) группы=0(root),1000(denis)
dev01% pfexec ./tst
result = 60; errno = 0

On the host the NIC is attached to a bridge via VMNET:
asus# ifconfig bridge0
bridge0: flags=1008843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST,LOWER_UP> metric 
0 mtu 1500
	options=0
	ether 58:9c:fc:10:c6:27
	inet 192.168.1.1 netmask 0xffffff00 broadcast 192.168.1.255
	id 00:00:00:00:00:00 priority 32768 hellotime 2 fwddelay 15
	maxage 20 holdcnt 6 proto rstp maxaddr 2000 timeout 1200
	root id 00:00:00:00:00:00 priority 32768 ifcost 0 port 0
	member: vmnet0 flags=143<LEARNING,DISCOVER,AUTOEDGE,AUTOPTP>
	        ifmaxaddr 0 port 4 priority 128 path cost 2000000
	groups: bridge
	nd6 options=9<PERFORMNUD,IFDISABLED>
asus# ifconfig vmnet0
vmnet0: flags=1008943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST,LOWER_UP> 
metric 0 mtu 1500
	options=80000<LINKSTATE>
	ether 58:9c:fc:10:0d:5d
	hwaddr 58:9c:fc:00:58:3f
	groups: vmnet
	media: Ethernet 1000baseT <full-duplex>
	status: active
	nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
	Opened by PID 4683
And the output of tcpdump on the bridge0:
asus# tcpdump -i bridge0 -n not port 22
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on bridge0, link-type EN10MB (Ethernet), snapshot length 262144 
bytes
17:05:02.479797 ARP, Request who-has 10.0.0.61 (ff:ff:ff:ff:ff:ff) tell 
10.0.0.13, length 46
^C
1 packet captured
17 packets received by filter
0 packets dropped by kernel

So, 10.0.0.61 and 10.0.0.13 are from your packet,
therefore it works (in my test env)

On Friday 31 May 2024 16:33:05 Marcel Telka wrote:
> On Fri, May 31, 2024 at 03:18:23PM +0300, Denis Kozadaev wrote:
> > On Friday 31 May 2024 15:07:13 Marcel Telka wrote:
> > > On Fri, May 31, 2024 at 02:23:04PM +0300, Denis Kozadaev wrote:
> > > > The send() function can be used only when the socket is in a
> > > > connected state. (c) manual page at
> > > > https://illumos.org/man/3SOCKET/send Use sendto(), Marcel.
> > >
> > >         sendto(s, buf, sizeof buf - 1, 0, NULL, 0);
> > >
> > > gives the same result: succeeds, but no packet on the network.
> >
> > it is because
> > send(sock, buf, len, flags)
> > could be equivalent to
> > sendto(sock, buf, len, flags, NULL, 0)
> > it depend of the implementation
> > You should specify a real address for sendto()
> > and yes, the packet and the address have duplicate of this data.
>
> I tried to add this:
>
>
>         llp.sll_pkttype = PACKET_BROADCAST;
>         llp.sll_hatype = ARPHRD_ETHER;
>         llp.sll_halen = 6;
>         llp.sll_addr[0] = 0xff;
>         llp.sll_addr[1] = 0xff;
>         llp.sll_addr[2] = 0xff;
>         llp.sll_addr[3] = 0xff;
>         llp.sll_addr[4] = 0xff;
>         llp.sll_addr[5] = 0xff;
>
>         sendto(s, buf, sizeof buf - 1, 0, (struct sockaddr *)&llp, sizeof
> llp);
>
> And the result is stil the same: the function succeeded, no packet on
> the network.


[-- Attachment #2: main.c --]
[-- Type: text/x-csrc, Size: 1933 bytes --]

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/ioctl.h>

#include <net/if.h>

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int
main(void)
{
        int                     sock;
        struct sockaddr_ll      sll, llp;
        struct lifreq           ifr;
        char                    buf[8192], dev[] = "dev0";
        ssize_t                 result;
        char pkt[] =
        "\xff\xff\xff\xff\xff\xff"
        "\x00\x1c\x25\xa0\xb7\x2e"
        "\x08\x06\x00\x01"
        "\x08\x00\x06\x04\x00\x01"
        "\x00\x1c\x25\xa0\xb7\x2e"
        "\x0a\x00\x00\x0d"
        "\xff\xff\xff\xff\xff\xff"
        "\x0a\x00\x00\x3d"
        "\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00"
        ;
        sock = socket(AF_PACKET, SOCK_RAW, 0);
        if (sock < 0) {
                perror("socket");
                return (1);
        }

        strcpy(ifr.lifr_name, dev);
        result = ioctl(sock, SIOCGLIFINDEX, &ifr);
        sll.sll_family = AF_PACKET;
        sll.sll_ifindex = ifr.lifr_index;
        sll.sll_protocol = 0;
        result = bind(sock, (struct sockaddr *)&sll, sizeof(sll));

/*        llp.sll_pkttype = PACKET_BROADCAST;
        llp.sll_hatype = ARPHRD_ETHER;
        llp.sll_halen = 6;
        llp.sll_addr[0] = 0xff;
        llp.sll_addr[1] = 0xff;
        llp.sll_addr[2] = 0xff;
        llp.sll_addr[3] = 0xff;
        llp.sll_addr[4] = 0xff;
        llp.sll_addr[5] = 0xff; */

	result = send(sock, pkt, sizeof(pkt) - 1, 0);
	fprintf(stderr, "result = %d; errno = %d\n", result, errno);
/*        for (;;) {
                result = recvfrom(sock, buf, sizeof(buf), 0, NULL, NULL);
                fprintf(stderr, "received %d bytes\n", (int)result);
        } */

        close(sock);

        return (0);
}

  reply	other threads:[~2024-05-31 14:15 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-31  8:43 Marcel Telka
2024-05-31  8:54 ` [developer] " Marcel Telka
2024-05-31  9:13   ` Denis Kozadaev
2024-05-31  9:51     ` Marcel Telka
2024-05-31 11:08       ` Marcel Telka
2024-05-31 11:23         ` Denis Kozadaev
2024-05-31 12:07           ` Marcel Telka
2024-05-31 12:18             ` Denis Kozadaev
2024-05-31 13:33               ` Marcel Telka
2024-05-31 14:15                 ` Denis Kozadaev [this message]
2024-05-31 15:04                   ` Marcel Telka
2024-05-31 15:20                     ` Denis Kozadaev
2024-05-31  9:03 ` Joshua M. Clulow
2024-05-31  9:50   ` Marcel Telka
2024-05-31 13:45     ` Marcel Telka
2024-05-31  9:43 ` Pramod Batni
2024-05-31  9:49   ` Marcel Telka
2024-06-02  9:05 ` Marcel Telka
2024-06-06 12:54   ` Marcel Telka
2024-06-06 16:51     ` Alan Coopersmith

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202405311715.39644.denis@tambov.ru \
    --to=denis@tambov.ru \
    --cc=developer@lists.illumos.org \
    --cc=marcel@telka.sk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).