LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2015, 08:43 AM   #1
Praju26
LQ Newbie
 
Registered: May 2015
Posts: 8

Rep: Reputation: Disabled
Unhappy Sending ethernet packet with VLAN tag in C socket


I am having a problem to understand VLAN tagging. I have already done changes in the /etc/network/interface file using this link(http://myhomelab.blogspot.de/2014/01...-in-linux.html). I am using socket programming on Raspbian (Raspberry pi) using C language.

I tried 2 methods:
1. The socket in C is

s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);

When i send a ethernet packet from the raspberry pi using the above socket i only see the outgoing "arp" messages on the bus.

2. s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))

But this creates an error "sendto failed: Invalid argument" or "sendto failed: Network down" during sending the packet, the code to send packet is:

struct sockaddr_in sin;
sin.sin_family = AF_PACKET;
sin.sin_port = htons(30490);
sin.sin_addr.s_addr = inet_addr(destination ip address);

if(sendto(socket, data, iph->tot_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{ perror("Sendto failed"); }
else { printf("Packet send"); }

Could anyone help me with the information on how to attach a VLAN tag with every packet that is being send ? Or what i am doing wrong in above code ?

Is there a code which is already implemented for sending packets with VLAN?

(In my setup i am not using any router or switch)
 
Old 06-23-2015, 02:19 PM   #2
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
Method 1 should work but you have to build packet from Ethernet header.
 
Old 06-24-2015, 01:32 AM   #3
Praju26
LQ Newbie
 
Registered: May 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
I didnt understand "build packet from Ethernet header" means that i should manually attach the MAC address ?
 
Old 06-24-2015, 02:41 PM   #4
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
Quote:
i should manually attach the MAC address ?
Yes, you have to generate Ethernet header, such as source and destination MAC. For your case, VLAN tag.
Another one, you should use AF_PACKET instead of AF_INET in method #1.
 
Old 06-26-2015, 12:41 AM   #5
Praju26
LQ Newbie
 
Registered: May 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Unhappy

Ok. Now the problem is the destination and source MAC address will not always remain the same. But the destination ip address will always be the same. I am implementing a platform independent application. Now how should i proceed ?
 
Old 06-26-2015, 02:19 PM   #6
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
If you don't want to deal with MAC address, you have to set socket option, IP_HDRINCL and SO_BINDTODEVICE.
 
Old 06-30-2015, 07:41 AM   #7
Praju26
LQ Newbie
 
Registered: May 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
@nini09 could you please provide an example for this ?
 
Old 06-30-2015, 03:13 PM   #8
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
// Set flag so socket expects us to provide IPv4 header.
if (setsockopt (sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)) < 0) {
perror ("setsockopt() failed to set IP_HDRINCL ");
exit (EXIT_FAILURE);
}

// Bind socket to interface index.
if (setsockopt (sd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof (ifr)) < 0) {
perror ("setsockopt() failed to bind to interface ");
exit (EXIT_FAILURE);
}
 
Old 07-01-2015, 08:10 AM   #9
Praju26
LQ Newbie
 
Registered: May 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by nini09 View Post
// Set flag so socket expects us to provide IPv4 header.
if (setsockopt (sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)) < 0) {
perror ("setsockopt() failed to set IP_HDRINCL ");
exit (EXIT_FAILURE);
}

// Bind socket to interface index.
if (setsockopt (sd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof (ifr)) < 0) {
perror ("setsockopt() failed to bind to interface ");
exit (EXIT_FAILURE);
}
The packets are not being send using this method. I only shows the IPv4 packets in Wireshark. not the vlan and udp packet attached to it.
 
Old 07-01-2015, 06:34 PM   #10
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
No Ethernet header?
 
  


Reply

Tags
ethernet, raspbian, socket, vlan



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why the eth0 send out the packet with tag vlan id 0 wesley_q Linux - Networking 3 03-02-2015 07:44 AM
vlan tag packet is stripped in eth1 not in eth2 interface? sargurus Linux - Networking 0 04-03-2014 10:44 AM
how configure bridge in linux for sending packets from access point to ethernet vlan dkpradhan Linux - Wireless Networking 1 11-27-2013 12:43 AM
Route non-vlan packet to a vlan interface mic.sed Linux - Networking 2 04-23-2010 02:39 AM
Receive Ethernet VLAN frame on the native ethernet interface (raw packet socket) scottbiker Linux - Networking 0 07-13-2009 03:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 06:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration