Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
The ONE Campaign to make poverty history

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 86 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here




       #include <sys/socket.h>
       #include <features.h>    /* for the glibc version number */
       #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
       #include <netpacket/packet.h>
       #include <net/ethernet.h>     /* the L2 protocols */
       #include <asm/types.h>
       #include <linux/if_packet.h>
       #include <linux/if_ether.h>   /* The L2 protocols */

       packet_socket = socket(PF_PACKET, int socket_type, int protocol);


       Packet  sockets are used to receive or send raw packets at
       the device driver (OSI Layer 2) level. They allow the user
       to  implement protocol modules in user space on top of the
       physical layer.

       The socket_type is either SOCK_RAW for raw packets includ­
       ing the link level header or SOCK_DGRAM for cooked packets
       with the link level header removed. The link level  header
       information  is  available  in  a common format in a sock­
       addr_ll.  protocol is the IEEE 802.3  protocol  number  in
       network order. See the <linux/if_ether.h> include file for
       a list of allowed  protocols.  When  protocol  is  set  to
       htons(ETH_P_ALL)  then  all  protocols  are received.  All
       incoming packets of that protocol type will be  passed  to
       the  packet socket before they are passed to the protocols
       implemented in the kernel.

       Only processes with effective uid  0  or  the  CAP_NET_RAW
       capability may open packet sockets.

       SOCK_RAW  packets are passed to and from the device driver
       without any changes in the packet data.  When receiving  a
       packet,  the address is still parsed and passed in a stan­
       dard sockaddr_ll address structure.  When  transmitting  a
       packet, the user supplied buffer should contain the physi­
       cal layer header.  That packet is then  queued  unmodified
       to the network driver of the interface defined by the des­
       tination address. Some device  drivers  always  add  other
       headers.   SOCK_RAW  is similar to but not compatible with
       the obsolete SOCK_PACKET of Linux 2.0.

       SOCK_DGRAM operates on a slightly higher level. The physi­
       cal  header  is removed before the packet is passed to the
       user.  Packets sent through a SOCK_DGRAM packet socket get
       a  suitable physical layer header based on the information
       recvfrom(2) the real length of the packet on the  wire  is
       always returned, even when it is longer than the buffer.


       The  sockaddr_ll  is  a  device independent physical layer

              struct sockaddr_ll {
                  unsigned short  sll_family;    /* Always AF_PACKET */
                  unsigned short  sll_protocol;  /* Physical layer protocol */
                  int             sll_ifindex;   /* Interface number */
                  unsigned short  sll_hatype;    /* Header type */
                  unsigned char   sll_pkttype;   /* Packet type */
                  unsigned char   sll_halen;     /* Length of address */
                  unsigned char   sll_addr[8];   /* Physical layer address */

       sll_protocol is the standard  ethernet  protocol  type  in
       network  order  as defined in the linux/if_ether.h include
       file.  It defaults to the socket's protocol.   sll_ifindex
       is  the  interface  index  of  the  interface  (see netde­
       vice(7)); 0 matches any interface (only  legal  for  bind­
       ing).   sll_hatype  is  a  ARP  type  as  defined  in  the
       linux/if_arp.h include  file.   sll_pkttype  contains  the
       packet  type.  Valid  types  are  PACKET_HOST for a packet
       addressed to the local host, PACKET_BROADCAST for a physi­
       cal  layer broadcast packet, PACKET_MULTICAST for a packet
       sent to a physical layer multicast address,  PACKET_OTHER­
       HOST  for a packet to some other host that has been caught
       by a device driver in promiscuous mode, and  PACKET_OUTGO­
       ING  for  a  packet originated from the local host that is
       looped back to a packet  socket.  These  types  make  only
       sense  for  receiving.  sll_addr and sll_halen contain the
       physical layer (e.g. IEEE 802.3) address and  its  length.
       The exact interpretation depends on the device.

       When  you send packets it is enough to specify sll_family,
       sll_addr, sll_halen, sll_ifindex.  The other fields should
       be  0.   sll_hatype  and  sll_pkttype  are set on received
       packets for your information.  For bind only  sll_protocol
       and sll_ifindex are used.


       Packet  sockets  can  be  used to configure physical layer
       multicasting and promiscuous mode.  It  works  by  calling
       setsockopt(2) on a packet socket for SOL_PACKET and one of
       the options PACKET_ADD_MEMBERSHIP  to  add  a  binding  or
       PACKET_DROP_MEMBERSHIP  to  drop  it.   They both expect a
       packet_mreq structure as argument:

       in mr_address and mr_alen, and PACKET_MR_ALLMULTI sets the
       socket up to receive all multicast packets arriving at the

       In addition the traditional ioctls SIOCSIFFLAGS,  SIOCADD­
       MULTI, SIOCDELMULTI can be used for the same purpose.


       SIOCGSTAMP  can  be  used to receive the time stamp of the
       last received packet. Argument is a struct timeval.

       In addition all standard ioctls  defined  in  netdevice(7)
       and socket(7) are valid on packet sockets.


       Packet  sockets  do  no  error  handling other than errors
       occurred while passing the packet to  the  device  driver.
       They don't have the concept of a pending error.


       In  Linux  2.0, the only way to get a packet socket was by
       calling socket(PF_INET, SOCK_PACKET, protocol).   This  is
       still supported but strongly deprecated.  The main differ­
       ence between the two methods is that SOCK_PACKET uses  the
       old  struct  sockaddr_pkt  to  specify an interface, which
       doesn't provide physical layer independence.

              struct sockaddr_pkt
                  unsigned short  spkt_family;
                  unsigned char   spkt_device[14];
                  unsigned short  spkt_protocol;

       spkt_family contains the device type, spkt_protocol is the
       IEEE  802.3  protocol  type as defined in <sys/if_ether.h>
       and spkt_device is the device name as  a  null  terminated
       string, e.g. eth0.

       This  structure  is obsolete and should not be used in new


       For portable programs it is suggested to use PF_PACKET via
       pcap(3);  although  this  only  covers  a  subset  of  the
       PF_PACKET features.

       Packet sockets are not subject  to  the  input  or  output
       firewall chains.


              Interface is not up.

              No interface address passed.

       ENODEV Unknown device name or interface index specified in
              interface address.

              Packet is bigger than interface MTU.

              Not enough memory to allocate the packet.

       EFAULT User passed invalid memory address.

       EINVAL Invalid argument.

       ENXIO  Interface  address  contained   illegal   interface

       EPERM  User  has insufficient privileges to carry out this

              Unknown multicast group address passed.

       ENOENT No packet received.

              In addition other errors may be  generated  by  the
              low-level driver.


       PF_PACKET  is  a  new  feature in Linux 2.2. Earlier Linux
       versions supported only SOCK_PACKET.

       The MSG_TRUNC recvmsg extension is an ugly hack and should
       be replaced by a control message.  There is  currently  no
       way to get the original destination address of packets via


       This man page was written by Andi  Kleen  with  help  from
       Matthew Wilcox.  PF_PACKET in Linux 2.2 was implemented by
       Alexey Kuznetsov, based on code by Alan Cox and others.


       ip(7), socket(7), socket(2), raw(7), pcap(3)

       RFC 894 for the standard IP Ethernet encapsulation.

       RFC 1700 for the IEEE 802.3 IP encapsulation.

       The <linux/if_ether.h> include  file  for  physical  layer

Linux Man Page              1999-04-29                  PACKET(7)
Help us cut cost by not downloading the whole site!
Use of automated download sofware ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and therefore is expressedly prohibited. For more details on this, take a look here



Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
The Linux Tutorial can use your help.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.09 Seconds