]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/pppoatm/pppoatm.c
09cd0b8bfb0142524d12c15971f8e13844e7e6b0
[ppp.git] / pppd / plugins / pppoatm / pppoatm.c
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 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include <unistd.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include "pppd.h"
24 #include "pathnames.h"
25 #include "fsm.h" /* Needed for lcp.h to include cleanly */
26 #include "lcp.h"
27 #include <atm.h>
28 #include <linux/atmdev.h>
29 #include <linux/atmppp.h>
30 #include <sys/stat.h>
31 #include <net/if.h>
32 #include <sys/ioctl.h>
33
34 const char pppd_version[] = VERSION;
35
36 static struct sockaddr_atmpvc pvcaddr;
37 static char *qosstr = NULL;
38 static bool llc_encaps = 0;
39 static bool vc_encaps = 0;
40 static int device_got_set = 0;
41 static int pppoatm_max_mtu, pppoatm_max_mru;
42 static int setdevname_pppoatm(const char *cp, const char **argv, int doit);
43 struct channel pppoa_channel;
44 static int pppoa_fd = -1;
45
46 static option_t pppoa_options[] = {
47         { "device name", o_wild, (void *) &setdevname_pppoatm,
48           "ATM service provider IDs: VPI.VCI",
49           OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG  | OPT_A2STRVAL | OPT_STATIC,
50           devnam},
51         { "llc-encaps", o_bool, &llc_encaps,
52           "use LLC encapsulation for PPPoATM", 1},
53         { "vc-encaps", o_bool, &vc_encaps,
54           "use VC multiplexing for PPPoATM (default)", 1},
55         { "qos", o_string, &qosstr,
56           "set QoS for PPPoATM connection", 1},
57         { NULL }
58 };
59
60 /* returns:
61  *  -1 if there's a problem with setting the device
62  *   0 if we can't parse "cp" as a valid name of a device
63  *   1 if "cp" is a reasonable thing to name a device
64  * Note that we don't actually open the device at this point
65  * We do need to fill in:
66  *   devnam: a string representation of the device
67  *   devstat: a stat structure of the device.  In this case
68  *     we're not opening a device, so we just make sure
69  *     to set up S_ISCHR(devstat.st_mode) != 1, so we
70  *     don't get confused that we're on stdin.
71  */
72 int (*old_setdevname_hook)(const char* cp) = NULL;
73 static int setdevname_pppoatm(const char *cp, const char **argv, int doit)
74 {
75         struct sockaddr_atmpvc addr;
76         extern struct stat devstat;
77
78         if (device_got_set)
79                 return 0;
80
81         memset(&addr, 0, sizeof addr);
82         if (text2atm(cp, (struct sockaddr *) &addr, sizeof(addr),
83             T2A_PVC | T2A_NAME | T2A_WILDCARD) < 0) {
84                 if (doit)
85                         info("cannot parse the ATM address: %s", cp);
86                 return 0;
87         }
88         if (!doit)
89                 return 1;
90
91         memcpy(&pvcaddr, &addr, sizeof pvcaddr);
92         strlcpy(devnam, cp, sizeof devnam);
93         devstat.st_mode = S_IFSOCK;
94         if (the_channel != &pppoa_channel) {
95                 the_channel = &pppoa_channel;
96                 lcp_wantoptions[0].neg_accompression = 0;
97                 lcp_allowoptions[0].neg_accompression = 0;
98                 lcp_wantoptions[0].neg_asyncmap = 0;
99                 lcp_allowoptions[0].neg_asyncmap = 0;
100                 lcp_wantoptions[0].neg_pcompression = 0;
101         }
102         device_got_set = 1;
103         return 1;
104 }
105
106 #define pppoatm_overhead() (llc_encaps ? 6 : 2)
107
108 static void no_device_given_pppoatm(void)
109 {
110         fatal("No vpi.vci specified");
111 }
112
113 static void set_line_discipline_pppoatm(int fd)
114 {
115         struct atm_backend_ppp be;
116
117         be.backend_num = ATM_BACKEND_PPP;
118         if (!llc_encaps)
119                 be.encaps = PPPOATM_ENCAPS_VC;
120         else if (!vc_encaps)
121                 be.encaps = PPPOATM_ENCAPS_LLC;
122         else
123                 be.encaps = PPPOATM_ENCAPS_AUTODETECT;
124
125         if (ioctl(fd, ATM_SETBACKEND, &be) < 0)
126                 fatal("ioctl(ATM_SETBACKEND): %m");
127 }
128
129 #if 0
130 static void reset_line_discipline_pppoatm(int fd)
131 {
132         atm_backend_t be = ATM_BACKEND_RAW;
133         /* 2.4 doesn't support this yet */
134         (void) ioctl(fd, ATM_SETBACKEND, &be);
135 }
136 #endif
137
138 static int connect_pppoatm(void)
139 {
140         int fd;
141         struct atm_qos qos;
142
143         if (!device_got_set)
144                 no_device_given_pppoatm();
145         fd = socket(AF_ATMPVC, SOCK_DGRAM, 0);
146         if (fd < 0)
147                 fatal("failed to create socket: %m");
148         memset(&qos, 0, sizeof qos);
149         qos.txtp.traffic_class = qos.rxtp.traffic_class = ATM_UBR;
150         /* TODO: support simplified QoS setting */
151         if (qosstr != NULL)
152                 if (text2qos(qosstr, &qos, 0))
153                         fatal("Can't parse QoS: \"%s\"");
154         qos.txtp.max_sdu = lcp_allowoptions[0].mru + pppoatm_overhead();
155         qos.rxtp.max_sdu = lcp_wantoptions[0].mru + pppoatm_overhead();
156         qos.aal = ATM_AAL5;
157         if (setsockopt(fd, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0)
158                 fatal("setsockopt(SO_ATMQOS): %m");
159         /* TODO: accept on SVCs... */
160         if (connect(fd, (struct sockaddr *) &pvcaddr,
161             sizeof(struct sockaddr_atmpvc)))
162                 fatal("connect(%s): %m", devnam);
163         pppoatm_max_mtu = lcp_allowoptions[0].mru;
164         pppoatm_max_mru = lcp_wantoptions[0].mru;
165         set_line_discipline_pppoatm(fd);
166         strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
167         pppoa_fd = fd;
168         return fd;
169 }
170
171 static void disconnect_pppoatm(void)
172 {
173         close(pppoa_fd);
174 }
175
176 void plugin_init(void)
177 {
178 #ifdef linux
179         extern int new_style_driver;    /* From sys-linux.c */
180         if (!ppp_available() && !new_style_driver)
181                 fatal("Kernel doesn't support ppp_generic - "
182                     "needed for PPPoATM");
183 #else
184         fatal("No PPPoATM support on this OS");
185 #endif
186         add_options(pppoa_options);
187 }
188
189 struct channel pppoa_channel = {
190     .options = pppoa_options,
191     .process_extra_options = NULL,
192     .check_options = NULL,
193     .connect = &connect_pppoatm,
194     .disconnect = &disconnect_pppoatm,
195     .establish_ppp = &generic_establish_ppp,
196     .disestablish_ppp = &generic_disestablish_ppp,
197     .send_config = NULL,
198     .recv_config = NULL,
199     .close = NULL,
200     .cleanup = NULL
201 };