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