# Are PF_LINK sockets supported?



## jtl (Mar 8, 2012)

I'm trying to read ethernet packets with a PF_LINK socket, but so far I can't even successfully open a PF_LINK socket. I haven't been able to turn up any information on how to use a PF_LINK socket, and the few references I've found to questions about how to use such a socket are invariably answered by pointing the poster in some other direction, such as using ioctl, pcap, bfp and such. Are PF_LINK sockets actually supported on FreeBSD, and if so, where might I find some information on how to use them? Thanks for any help.

My attemps fail at step one.


```
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>

int main(void) {

  int sock;

  if ( (sock = socket(PF_LINK, SOCK_RAW, 0)) < 0) {
    perror("socket call failed");
    exit(1);
  }

  close (sock);

  return 0;
}
```

This code always fails with

```
socket call failed: Protocol not supported
```
Trying other types, such as SOCK_DGRAM, don't change the result.


----------



## SirDice (Mar 9, 2012)

From socket(2):

```
The currently understood formats are:

           PF_LOCAL        Host-internal protocols, formerly called PF_UNIX,
           PF_UNIX         Host-internal protocols, deprecated, use PF_LOCAL,
           PF_INET         Internet version 4 protocols,
           PF_PUP          PUP protocols, like BSP,
           PF_APPLETALK    AppleTalk protocols,
           PF_ROUTE        Internal Routing protocol,
           [b]PF_LINK         Link layer interface,[/b]
           PF_IPX          Novell Internet Packet eXchange protocol,
           PF_RTIP         Help Identify RTIP packets,
           PF_PIP          Help Identify PIP packets,
           PF_ISDN         Integrated Services Digital Network,
           PF_KEY          Internal key-management function,
           PF_INET6        Internet version 6 protocols,
           PF_NATM         Native ATM access,
           PF_ATM          ATM,
           PF_NETGRAPH     Netgraph sockets
```


----------



## jtl (Mar 9, 2012)

Thanks - I learned about PF_LINK sockets from the sockets(2) man page. The problem for me is that I can't make it work, and can't find any example of it working. All I can find are similar questions to mine - people posting that they can't make it work. I'm hoping someone can confirm that PF_LINK sockets really are supported, and hopefully point me to some additional information, such as an example of how to open/use one.


----------



## expl (Mar 14, 2012)

I don't think you can open PF_LINK socket of any type. If you need to read/write to/from a datalink layer you need to either use the *bpf* device directly or use libpcap for more code portability.


----------

