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