1/* pppoatm.c - pppd plugin to implement PPPoATM protocol.
2 *
3 * Copyright 2000 Mitchell Blank Jr.
4 * Based in part on work from Jens Axboe and Paul Mackerras.
5 * Updated to ppp-2.4.1 by Bernhard Kaindl
6 *
7 * Updated to ppp-2.4.2 by David Woodhouse 2004.
8 *  - disconnect method added
9 *  - remove_options() abuse removed.
10 *
11 *  This program is free software; you can redistribute it and/or
12 *  modify it under the terms of the GNU General Public License
13 *  as published by the Free Software Foundation; either version
14 *  2 of the License, or (at your option) any later version.
15 */
16#include <unistd.h>
17#include <string.h>
18#include <stdlib.h>
19#include "pppd.h"
20#include "pathnames.h"
21#include "fsm.h" /* Needed for lcp.h to include cleanly */
22#include "lcp.h"
23#include <atm.h>
24#include <linux/atmdev.h>
25#include <linux/atmppp.h>
26#include <sys/stat.h>
27#include <net/if.h>
28#include <sys/ioctl.h>
29
30const char pppd_version[] = VERSION;
31
32static struct sockaddr_atmpvc pvcaddr;
33static char *qosstr = NULL;
34static bool llc_encaps = 0;
35static bool vc_encaps = 0;
36static int device_got_set = 0;
37static int pppoatm_max_mtu, pppoatm_max_mru;
38static int setdevname_pppoatm(const char *cp, const char **argv, int doit);
39struct channel pppoa_channel;
40static int pppoa_fd = -1;
41
42static option_t pppoa_options[] = {
43	{ "device name", o_wild, (void *) &setdevname_pppoatm,
44	  "ATM service provider IDs: VPI.VCI",
45	  OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG  | OPT_A2STRVAL | OPT_STATIC,
46	  devnam},
47	{ "llc-encaps", o_bool, &llc_encaps,
48	  "use LLC encapsulation for PPPoATM", 1},
49	{ "vc-encaps", o_bool, &vc_encaps,
50	  "use VC multiplexing for PPPoATM (default)", 1},
51	{ "qos", o_string, &qosstr,
52	  "set QoS for PPPoATM connection", 1},
53	{ NULL }
54};
55
56/* returns:
57 *  -1 if there's a problem with setting the device
58 *   0 if we can't parse "cp" as a valid name of a device
59 *   1 if "cp" is a reasonable thing to name a device
60 * Note that we don't actually open the device at this point
61 * We do need to fill in:
62 *   devnam: a string representation of the device
63 *   devstat: a stat structure of the device.  In this case
64 *     we're not opening a device, so we just make sure
65 *     to set up S_ISCHR(devstat.st_mode) != 1, so we
66 *     don't get confused that we're on stdin.
67 */
68int (*old_setdevname_hook)(const char* cp) = NULL;
69static int setdevname_pppoatm(const char *cp, const char **argv, int doit)
70{
71	struct sockaddr_atmpvc addr;
72	extern struct stat devstat;
73	if (device_got_set)
74		return 0;
75	//info("PPPoATM setdevname_pppoatm: '%s'", cp);
76	memset(&addr, 0, sizeof addr);
77	if (text2atm(cp, (struct sockaddr *) &addr, sizeof(addr),
78	    T2A_PVC | T2A_NAME) < 0) {
79               if(doit)
80                   info("atm does not recognize: %s", cp);
81		return 0;
82           }
83	if (!doit) return 1;
84	//if (!dev_set_ok()) return -1;
85	memcpy(&pvcaddr, &addr, sizeof pvcaddr);
86	strlcpy(devnam, cp, sizeof devnam);
87	devstat.st_mode = S_IFSOCK;
88	if (the_channel != &pppoa_channel) {
89		the_channel = &pppoa_channel;
90		lcp_wantoptions[0].neg_accompression = 0;
91		lcp_allowoptions[0].neg_accompression = 0;
92		lcp_wantoptions[0].neg_asyncmap = 0;
93		lcp_allowoptions[0].neg_asyncmap = 0;
94		lcp_wantoptions[0].neg_pcompression = 0;
95	}
96	info("PPPoATM setdevname_pppoatm - SUCCESS:%s", cp);
97	device_got_set = 1;
98	return 1;
99}
100
101#define pppoatm_overhead() (llc_encaps ? 6 : 2)
102
103static void no_device_given_pppoatm(void)
104{
105	fatal("No vpi.vci specified");
106}
107
108static void set_line_discipline_pppoatm(int fd)
109{
110	struct atm_backend_ppp be;
111	be.backend_num = ATM_BACKEND_PPP;
112	if (!llc_encaps)
113		be.encaps = PPPOATM_ENCAPS_VC;
114	else if (!vc_encaps)
115		be.encaps = PPPOATM_ENCAPS_LLC;
116	else
117		be.encaps = PPPOATM_ENCAPS_AUTODETECT;
118	if (ioctl(fd, ATM_SETBACKEND, &be) < 0)
119		fatal("ioctl(ATM_SETBACKEND): %m");
120}
121
122#if 0
123static void reset_line_discipline_pppoatm(int fd)
124{
125	atm_backend_t be = ATM_BACKEND_RAW;
126	/* 2.4 doesn't support this yet */
127	(void) ioctl(fd, ATM_SETBACKEND, &be);
128}
129#endif
130
131static int connect_pppoatm(void)
132{
133	int fd;
134	struct atm_qos qos;
135
136	/* XXX: This won't work on Android */
137	system ("/sbin/modprobe pppoatm");
138
139	if (!device_got_set)
140		no_device_given_pppoatm();
141	fd = socket(AF_ATMPVC, SOCK_DGRAM, 0);
142	if (fd < 0)
143		fatal("failed to create socket: %m");
144	memset(&qos, 0, sizeof qos);
145	qos.txtp.traffic_class = qos.rxtp.traffic_class = ATM_UBR;
146	/* TODO: support simplified QoS setting */
147	if (qosstr != NULL)
148		if (text2qos(qosstr, &qos, 0))
149			fatal("Can't parse QoS: \"%s\"");
150	qos.txtp.max_sdu = lcp_allowoptions[0].mru + pppoatm_overhead();
151	qos.rxtp.max_sdu = lcp_wantoptions[0].mru + pppoatm_overhead();
152	qos.aal = ATM_AAL5;
153	if (setsockopt(fd, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0)
154		fatal("setsockopt(SO_ATMQOS): %m");
155	/* TODO: accept on SVCs... */
156	if (connect(fd, (struct sockaddr *) &pvcaddr,
157	    sizeof(struct sockaddr_atmpvc)))
158		fatal("connect(%s): %m", devnam);
159	pppoatm_max_mtu = lcp_allowoptions[0].mru;
160	pppoatm_max_mru = lcp_wantoptions[0].mru;
161	set_line_discipline_pppoatm(fd);
162	strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
163	pppoa_fd = fd;
164	return fd;
165}
166
167static void disconnect_pppoatm(void)
168{
169	close(pppoa_fd);
170}
171
172static void send_config_pppoa(int mtu,
173			      u_int32_t asyncmap,
174			      int pcomp,
175			      int accomp)
176{
177	int sock;
178	struct ifreq ifr;
179	if (mtu > pppoatm_max_mtu)
180		error("Couldn't increase MTU to %d", mtu);
181	sock = socket(AF_INET, SOCK_DGRAM, 0);
182	if (sock < 0)
183		fatal("Couldn't create IP socket: %m");
184	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
185	ifr.ifr_mtu = mtu;
186	if (ioctl(sock, SIOCSIFMTU, (caddr_t) &ifr) < 0)
187		fatal("ioctl(SIOCSIFMTU): %m");
188	(void) close (sock);
189}
190
191static void recv_config_pppoa(int mru,
192			      u_int32_t asyncmap,
193			      int pcomp,
194			      int accomp)
195{
196	if (mru > pppoatm_max_mru)
197		error("Couldn't increase MRU to %d", mru);
198}
199
200void plugin_init(void)
201{
202#if defined(__linux__)
203	extern int new_style_driver;	/* From sys-linux.c */
204	if (!ppp_available() && !new_style_driver)
205		fatal("Kernel doesn't support ppp_generic - "
206		    "needed for PPPoATM");
207#else
208	fatal("No PPPoATM support on this OS");
209#endif
210	info("PPPoATM plugin_init");
211	add_options(pppoa_options);
212}
213struct channel pppoa_channel = {
214    options: pppoa_options,
215    process_extra_options: NULL,
216    check_options: NULL,
217    connect: &connect_pppoatm,
218    disconnect: &disconnect_pppoatm,
219    establish_ppp: &generic_establish_ppp,
220    disestablish_ppp: &generic_disestablish_ppp,
221    send_config: &send_config_pppoa,
222    recv_config: &recv_config_pppoa,
223    close: NULL,
224    cleanup: NULL
225};
226