1 /***********************************************************************
5 * Implementation of user-space PPPoE redirector for Linux.
7 * Functions for opening a raw socket and reading/writing raw Ethernet frames.
9 * Copyright (C) 2000 by Roaring Penguin Software Inc.
11 * This program may be distributed according to the terms of the GNU
12 * General Public License, version 2 or (at your option) any later version.
14 ***********************************************************************/
16 static char const RCSID[] =
17 "$Id: if.c,v 1.2 2008/06/09 08:34:23 paulus Exp $";
25 #include <pppd/pppd.h>
31 #ifdef HAVE_NETPACKET_PACKET_H
32 #include <netpacket/packet.h>
33 #elif defined(HAVE_LINUX_IF_PACKET_H)
34 #include <linux/if_packet.h>
37 #ifdef HAVE_ASM_TYPES_H
38 #include <asm/types.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
49 #ifdef HAVE_NET_IF_ARP_H
50 #include <net/if_arp.h>
53 /* Initialize frame types to RFC 2516 values. Some broken peers apparently
54 use different frame types... sigh... */
56 UINT16_t Eth_PPPOE_Discovery = ETH_PPPOE_DISCOVERY;
57 UINT16_t Eth_PPPOE_Session = ETH_PPPOE_SESSION;
59 /**********************************************************************
62 * packet -- a received PPPoE packet
64 * ethernet packet type (see /usr/include/net/ethertypes.h)
66 * Checks the ethernet packet header to determine its type.
67 * We should only be receveing DISCOVERY and SESSION types if the BPF
68 * is set up correctly. Logs an error if an unexpected type is received.
69 * Note that the ethernet type names come from "pppoe.h" and the packet
70 * packet structure names use the LINUX dialect to maintain consistency
71 * with the rest of this file. See the BSD section of "pppoe.h" for
72 * translations of the data structure names.
73 ***********************************************************************/
75 etherType(PPPoEPacket *packet)
77 UINT16_t type = (UINT16_t) ntohs(packet->ethHdr.h_proto);
78 if (type != Eth_PPPOE_Discovery && type != Eth_PPPOE_Session) {
79 error("Invalid ether type 0x%x", type);
84 /**********************************************************************
85 *%FUNCTION: openInterface
87 * ifname -- name of interface
88 * type -- Ethernet frame type
89 * hwaddr -- if non-NULL, set to the hardware address
91 * A raw socket for talking to the Ethernet card. Exits on error.
93 * Opens a raw Ethernet socket
94 ***********************************************************************/
96 openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr)
104 #ifdef HAVE_STRUCT_SOCKADDR_LL
105 struct sockaddr_ll sa;
110 memset(&sa, 0, sizeof(sa));
112 #ifdef HAVE_STRUCT_SOCKADDR_LL
119 maxlen = sizeof(sa.sa_data);
122 if (strlen(ifname) >= maxlen) {
123 error("Can't use interface %.16s: name is too long", ifname);
127 if ((fd = socket(domain, stype, htons(type))) < 0) {
128 /* Give a more helpful message for the common error case */
129 if (errno == EPERM) {
130 fatal("Cannot create raw socket -- pppoe must be run as root.");
132 error("Can't open socket for pppoe: %m");
136 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) {
137 error("Can't set socket options for pppoe: %m");
142 /* Fill in hardware address */
144 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
145 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
146 error("Can't get hardware address for %s: %m", ifname);
150 memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
152 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
153 warn("Interface %.16s is not Ethernet", ifname);
156 if (NOT_UNICAST(hwaddr)) {
157 fatal("Can't use interface %.16s: it has broadcast/multicast MAC address",
162 /* Sanity check on MTU */
163 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
164 if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
165 error("Can't get MTU for %s: %m", ifname);
166 } else if (ifr.ifr_mtu < ETH_DATA_LEN) {
167 error("Interface %.16s has MTU of %d -- should be at least %d.",
168 ifname, ifr.ifr_mtu, ETH_DATA_LEN);
169 error("This may cause serious connection problems.");
172 #ifdef HAVE_STRUCT_SOCKADDR_LL
173 /* Get interface index */
174 sa.sll_family = AF_PACKET;
175 sa.sll_protocol = htons(type);
177 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
178 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
179 error("Could not get interface index for %s: %m", ifname);
183 sa.sll_ifindex = ifr.ifr_ifindex;
186 strlcpy(sa.sa_data, ifname, sizeof(sa.sa_data));
189 /* We're only interested in packets on specified interface */
190 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
191 error("Failed to bind to interface %s: %m", ifname);
200 /***********************************************************************
201 *%FUNCTION: sendPacket
203 * sock -- socket to send to
204 * pkt -- the packet to transmit
205 * size -- size of packet (in bytes)
207 * 0 on success; -1 on failure
210 ***********************************************************************/
212 sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size)
217 pppoe_log_packet("Send ", pkt);
218 #if defined(HAVE_STRUCT_SOCKADDR_LL)
219 err = send(sock, pkt, size, 0);
223 strlcpy(sa.sa_data, conn->ifName, sizeof(sa.sa_data));
224 err = sendto(sock, pkt, size, 0, &sa, sizeof(sa));
227 error("error sending pppoe packet: %m");
233 /***********************************************************************
234 *%FUNCTION: receivePacket
236 * sock -- socket to read from
237 * pkt -- place to store the received packet
238 * size -- set to size of packet in bytes
240 * >= 0 if all OK; < 0 if error
243 ***********************************************************************/
245 receivePacket(int sock, PPPoEPacket *pkt, int *size)
247 if ((*size = recv(sock, pkt, sizeof(PPPoEPacket), 0)) < 0) {
248 error("error receiving pppoe packet: %m");
252 pppoe_log_packet("Recv ", pkt);