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 ***********************************************************************/
22 #include <pppd/pppd.h>
28 #ifdef HAVE_NETPACKET_PACKET_H
29 #include <netpacket/packet.h>
30 #elif defined(HAVE_LINUX_IF_PACKET_H)
31 #include <linux/if_packet.h>
34 #ifdef HAVE_ASM_TYPES_H
35 #include <asm/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
46 #ifdef HAVE_NET_IF_ARP_H
47 #include <net/if_arp.h>
50 /* Initialize frame types to RFC 2516 values. Some broken peers apparently
51 use different frame types... sigh... */
53 UINT16_t Eth_PPPOE_Discovery = ETH_PPPOE_DISCOVERY;
54 UINT16_t Eth_PPPOE_Session = ETH_PPPOE_SESSION;
56 /**********************************************************************
59 * packet -- a received PPPoE packet
61 * ethernet packet type (see /usr/include/net/ethertypes.h)
63 * Checks the ethernet packet header to determine its type.
64 * We should only be receveing DISCOVERY and SESSION types if the BPF
65 * is set up correctly. Logs an error if an unexpected type is received.
66 * Note that the ethernet type names come from "pppoe.h" and the packet
67 * packet structure names use the LINUX dialect to maintain consistency
68 * with the rest of this file. See the BSD section of "pppoe.h" for
69 * translations of the data structure names.
70 ***********************************************************************/
72 etherType(PPPoEPacket *packet)
74 UINT16_t type = (UINT16_t) ntohs(packet->ethHdr.h_proto);
75 if (type != Eth_PPPOE_Discovery && type != Eth_PPPOE_Session) {
76 error("Invalid ether type 0x%x", type);
81 /**********************************************************************
82 *%FUNCTION: openInterface
84 * ifname -- name of interface
85 * type -- Ethernet frame type
86 * hwaddr -- if non-NULL, set to the hardware address
88 * A raw socket for talking to the Ethernet card. Exits on error.
90 * Opens a raw Ethernet socket
91 ***********************************************************************/
93 openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr)
101 #ifdef HAVE_STRUCT_SOCKADDR_LL
102 struct sockaddr_ll sa;
107 memset(&sa, 0, sizeof(sa));
109 #ifdef HAVE_STRUCT_SOCKADDR_LL
116 maxlen = sizeof(sa.sa_data);
119 if (strlen(ifname) >= maxlen) {
120 error("Can't use interface %.16s: name is too long", ifname);
124 if ((fd = socket(domain, stype, htons(type))) < 0) {
125 /* Give a more helpful message for the common error case */
126 if (errno == EPERM) {
127 fatal("Cannot create raw socket -- pppoe must be run as root.");
129 error("Can't open socket for pppoe: %m");
133 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) {
134 error("Can't set socket options for pppoe: %m");
139 /* Fill in hardware address */
141 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
142 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
143 error("Can't get hardware address for %s: %m", ifname);
147 memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
149 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
150 warn("Interface %.16s is not Ethernet", ifname);
153 if (NOT_UNICAST(hwaddr)) {
154 fatal("Can't use interface %.16s: it has broadcast/multicast MAC address",
159 /* Sanity check on MTU */
160 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
161 if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
162 error("Can't get MTU for %s: %m", ifname);
163 } else if (ifr.ifr_mtu < ETH_DATA_LEN) {
164 error("Interface %.16s has MTU of %d -- should be at least %d.",
165 ifname, ifr.ifr_mtu, ETH_DATA_LEN);
166 error("This may cause serious connection problems.");
169 #ifdef HAVE_STRUCT_SOCKADDR_LL
170 /* Get interface index */
171 sa.sll_family = AF_PACKET;
172 sa.sll_protocol = htons(type);
174 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
175 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
176 error("Could not get interface index for %s: %m", ifname);
180 sa.sll_ifindex = ifr.ifr_ifindex;
183 strlcpy(sa.sa_data, ifname, sizeof(sa.sa_data));
186 /* We're only interested in packets on specified interface */
187 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
188 error("Failed to bind to interface %s: %m", ifname);
197 /***********************************************************************
198 *%FUNCTION: sendPacket
200 * sock -- socket to send to
201 * pkt -- the packet to transmit
202 * size -- size of packet (in bytes)
204 * 0 on success; -1 on failure
207 ***********************************************************************/
209 sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size)
214 pppoe_log_packet("Send ", pkt);
215 #if defined(HAVE_STRUCT_SOCKADDR_LL)
216 err = send(sock, pkt, size, 0);
220 strlcpy(sa.sa_data, conn->ifName, sizeof(sa.sa_data));
221 err = sendto(sock, pkt, size, 0, &sa, sizeof(sa));
224 error("error sending pppoe packet: %m");
230 /***********************************************************************
231 *%FUNCTION: receivePacket
233 * sock -- socket to read from
234 * pkt -- place to store the received packet
235 * size -- set to size of packet in bytes
237 * >= 0 if all OK; < 0 if error
240 ***********************************************************************/
242 receivePacket(int sock, PPPoEPacket *pkt, int *size)
244 if ((*size = recv(sock, pkt, sizeof(PPPoEPacket), 0)) < 0) {
245 error("error receiving pppoe packet: %m");
249 pppoe_log_packet("Recv ", pkt);