|
Line 0
Link Here
|
|
|
1 |
/* pppoatm.c - pppd plugin to implement PPPoATM protocol. |
| 2 |
* |
| 3 |
* Copyright 2000 Mitchell Blank Jr. |
| 4 |
* 2002 Clive Nicolson. |
| 5 |
* Based in part on work from Jens Axboe and Paul Mackerras. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU General Public License |
| 9 |
* as published by the Free Software Foundation; either version |
| 10 |
* 2 of the License, or (at your option) any later version. |
| 11 |
*/ |
| 12 |
#include <errno.h> |
| 13 |
#include <string.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <sys/types.h> |
| 16 |
#include <unistd.h> |
| 17 |
|
| 18 |
#include <atm.h> |
| 19 |
#include <linux/atmdev.h> |
| 20 |
#include <linux/atmppp.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#include <net/if.h> |
| 23 |
#include <sys/ioctl.h> |
| 24 |
|
| 25 |
#include "pppd.h" |
| 26 |
#include "fsm.h" /* Needed for lcp.h to include cleanly */ |
| 27 |
#include "lcp.h" |
| 28 |
|
| 29 |
#define Dinfo if (debug) info |
| 30 |
|
| 31 |
static void pppoatm_process_extra_options __P((void)); |
| 32 |
static void pppoatm_check_options __P((void)); |
| 33 |
static int pppoatm_connect __P((void)); |
| 34 |
static void pppoatm_disconnect __P((void)); |
| 35 |
static int pppoatm_establish_ppp __P((int)); |
| 36 |
static void pppoatm_disestablish_ppp __P((int)); |
| 37 |
static void pppoatm_do_send_config __P((int, u_int32_t, int, int)); |
| 38 |
static void pppoatm_recv_config __P((int, u_int32_t, int, int)); |
| 39 |
static void pppoatm_cleanup __P((void)); |
| 40 |
static void pppoatm_close __P((void)); |
| 41 |
|
| 42 |
char pppd_version[] = VERSION; |
| 43 |
|
| 44 |
static struct pppoatm_globals { |
| 45 |
struct sockaddr_atmpvc pvcaddr; |
| 46 |
int pppoatm_max_mtu, pppoatm_max_mru; |
| 47 |
int real_ttyfd; /* fd for actual serial port */ |
| 48 |
#ifndef OPT_UTILS |
| 49 |
bool lockflag; /* Create lock file to lock the serial dev */ |
| 50 |
#endif |
| 51 |
#if 0 |
| 52 |
bool pppoatm_accept; |
| 53 |
#endif |
| 54 |
bool llc_encaps; |
| 55 |
bool vc_encaps; |
| 56 |
char *qosstr; |
| 57 |
struct atm_qos qos; |
| 58 |
} glb; |
| 59 |
|
| 60 |
extern uid_t uid; |
| 61 |
extern int kill_link; |
| 62 |
|
| 63 |
static int pppoatm_setdevname __P((char *, char **, int)); |
| 64 |
|
| 65 |
static option_t pppoatm_options[] = { |
| 66 |
/* device name must be first, or change pppoatm_connect() below! */ |
| 67 |
{ "device name", o_wild, (void *) &pppoatm_setdevname, |
| 68 |
"atm itf.vpi.vpc", |
| 69 |
OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC, |
| 70 |
devnam}, |
| 71 |
|
| 72 |
#ifdef OPT_UTILS |
| 73 |
lock_option, |
| 74 |
nolock_option, |
| 75 |
record_option, |
| 76 |
#else |
| 77 |
{ "lock", o_bool, &glb.lockflag, /* We ignore glb.lockflag */ |
| 78 |
"Lock serial device with UUCP-style lock file", OPT_PRIO | 1 }, |
| 79 |
{ "nolock", o_bool, &glb.lockflag, |
| 80 |
"Don't lock serial device", OPT_PRIOSUB | OPT_PRIV }, |
| 81 |
#endif |
| 82 |
|
| 83 |
#if 0 |
| 84 |
{ "accept", o_bool, &glb.pppoatm_accept, |
| 85 |
"set PPPoATM socket to accept incoming connections", |
| 86 |
1 }, |
| 87 |
#endif |
| 88 |
{ "llc-encaps", o_bool, &glb.llc_encaps, |
| 89 |
"use LLC encapsulation for PPPoATM", |
| 90 |
1}, |
| 91 |
{ "vc-encaps", o_bool, &glb.vc_encaps, |
| 92 |
"use VC multiplexing for PPPoATM (default)", |
| 93 |
1}, |
| 94 |
{ "qos", o_string, &glb.qosstr, |
| 95 |
"set QoS for PPPoATM connection", |
| 96 |
1}, |
| 97 |
{ NULL } |
| 98 |
}; |
| 99 |
|
| 100 |
static |
| 101 |
struct channel pppoatm_channel = { |
| 102 |
options: pppoatm_options, |
| 103 |
process_extra_options: &pppoatm_process_extra_options, |
| 104 |
check_options: &pppoatm_check_options, |
| 105 |
connect: &pppoatm_connect, |
| 106 |
disconnect: &pppoatm_disconnect, |
| 107 |
establish_ppp: &pppoatm_establish_ppp, |
| 108 |
disestablish_ppp: &pppoatm_disestablish_ppp, |
| 109 |
send_config: &pppoatm_do_send_config, |
| 110 |
recv_config: &pppoatm_recv_config, |
| 111 |
cleanup: &pppoatm_cleanup, |
| 112 |
close: &pppoatm_close |
| 113 |
}; |
| 114 |
|
| 115 |
/* |
| 116 |
* pppoatm_setdevname - Set the device name. |
| 117 |
* If doit is 0, the call is to check whether this option is |
| 118 |
* potentially a device name. |
| 119 |
*/ |
| 120 |
static |
| 121 |
int pppoatm_setdevname(char *cp, char **argv, int doit) |
| 122 |
{ |
| 123 |
Dinfo("In %s doit=%d", __FUNCTION__, doit); |
| 124 |
|
| 125 |
if (*cp == 0) |
| 126 |
return 0; |
| 127 |
|
| 128 |
if (doit == 0) { |
| 129 |
struct sockaddr_atmpvc addr; |
| 130 |
|
| 131 |
memset(&addr, 0, sizeof(addr)); |
| 132 |
if (text2atm(cp, (struct sockaddr *) &addr, sizeof(addr), |
| 133 |
T2A_PVC | T2A_NAME) < 0) |
| 134 |
return 0; |
| 135 |
memcpy(&glb.pvcaddr, &addr, sizeof(glb.pvcaddr)); |
| 136 |
} |
| 137 |
|
| 138 |
strlcpy(devnam, cp, sizeof(devnam)); |
| 139 |
|
| 140 |
if (doit) |
| 141 |
default_device = 0; |
| 142 |
|
| 143 |
return 1; |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* pppoatm_do_send_config - set transmit-side PPP configuration. |
| 148 |
*/ |
| 149 |
static |
| 150 |
void pppoatm_do_send_config(int mtu, u_int32_t accm, int pcomp, int accomp) |
| 151 |
{ |
| 152 |
int sock; |
| 153 |
struct ifreq ifr; |
| 154 |
|
| 155 |
Dinfo("In %s", __FUNCTION__); |
| 156 |
|
| 157 |
if (mtu > glb.pppoatm_max_mtu) { |
| 158 |
warn("Couldn't increase MTU to %d", mtu); |
| 159 |
mtu = glb.pppoatm_max_mtu; |
| 160 |
} |
| 161 |
|
| 162 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 163 |
if (sock < 0) |
| 164 |
fatal("Couldn't create IP socket: %m"); |
| 165 |
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 166 |
ifr.ifr_mtu = mtu; |
| 167 |
if (ioctl(sock, SIOCSIFMTU, (caddr_t) &ifr) < 0) |
| 168 |
fatal("ioctl(SIOCSIFMTU): %m"); |
| 169 |
(void) close (sock); |
| 170 |
} |
| 171 |
|
| 172 |
static |
| 173 |
void pppoatm_recv_config(int mru, u_int32_t asyncmap, int pcomp, int accomp) |
| 174 |
{ |
| 175 |
Dinfo("In %s", __FUNCTION__); |
| 176 |
|
| 177 |
if (mru > glb.pppoatm_max_mru) |
| 178 |
error("Couldn't increase MRU to %d", mru); |
| 179 |
} |
| 180 |
|
| 181 |
void plugin_init(void) |
| 182 |
{ |
| 183 |
Dinfo("In %s", __FUNCTION__); |
| 184 |
|
| 185 |
the_channel = &pppoatm_channel; |
| 186 |
|
| 187 |
lcp_allowoptions[0].asyncmap = 0; |
| 188 |
|
| 189 |
lcp_wantoptions[0].neg_accompression = 0; |
| 190 |
lcp_allowoptions[0].neg_accompression = 0; |
| 191 |
|
| 192 |
lcp_wantoptions[0].neg_asyncmap = 0; |
| 193 |
lcp_allowoptions[0].neg_asyncmap = 0; |
| 194 |
|
| 195 |
lcp_wantoptions[0].neg_pcompression = 0; |
| 196 |
} |
| 197 |
|
| 198 |
static |
| 199 |
int options_for_pppoatm(void) { |
| 200 |
char atm_devnam[sizeof(devnam)]; |
| 201 |
int res; |
| 202 |
|
| 203 |
Dinfo("In %s", __FUNCTION__); |
| 204 |
|
| 205 |
strcpy(atm_devnam, devnam); |
| 206 |
strcpy(devnam, "atm"); |
| 207 |
strlcat(devnam, atm_devnam, sizeof(devnam)); |
| 208 |
res = options_for_tty(); |
| 209 |
strcpy(devnam, atm_devnam); |
| 210 |
return res; |
| 211 |
} |
| 212 |
|
| 213 |
static |
| 214 |
void pppoatm_process_extra_options(void) |
| 215 |
{ |
| 216 |
Dinfo("In %s", __FUNCTION__); |
| 217 |
|
| 218 |
if (!options_for_pppoatm()) |
| 219 |
exit(EXIT_OPTION_ERROR); |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
* pppoatm_check_options - do consistency checks on the options we were given. |
| 224 |
*/ |
| 225 |
static |
| 226 |
void pppoatm_check_options(void) |
| 227 |
{ |
| 228 |
Dinfo("In %s", __FUNCTION__); |
| 229 |
|
| 230 |
#if defined(__linux__) |
| 231 |
{ |
| 232 |
extern int new_style_driver; /* From sys-linux.c */ |
| 233 |
|
| 234 |
if (!new_style_driver) { |
| 235 |
option_error("Kernel doesn't support ppp_generic - " |
| 236 |
"needed for PPPoATM"); |
| 237 |
exit(EXIT_OPTION_ERROR); |
| 238 |
} |
| 239 |
} |
| 240 |
#else |
| 241 |
option_error("No PPPoATM support on this OS"); |
| 242 |
exit(EXIT_OPTION_ERROR); |
| 243 |
#endif |
| 244 |
|
| 245 |
if (!devnam[0]) { |
| 246 |
option_error("No [itf.]vpi.vci specified"); |
| 247 |
exit(EXIT_OPTION_ERROR); |
| 248 |
} |
| 249 |
|
| 250 |
memset(&glb.qos, 0, sizeof(glb.qos)); |
| 251 |
glb.qos.txtp.traffic_class = glb.qos.rxtp.traffic_class = ATM_UBR; |
| 252 |
|
| 253 |
if (glb.qosstr != NULL) |
| 254 |
if (text2qos(glb.qosstr, &glb.qos, 0)) { |
| 255 |
option_error("Can't parse QoS: \"%s\"", glb.qosstr); |
| 256 |
exit(EXIT_OPTION_ERROR); |
| 257 |
} |
| 258 |
|
| 259 |
glb.qos.aal = ATM_AAL5; |
| 260 |
} |
| 261 |
|
| 262 |
static |
| 263 |
int pppoatm_connect(void) |
| 264 |
{ |
| 265 |
int state; |
| 266 |
|
| 267 |
Dinfo("In %s", __FUNCTION__); |
| 268 |
|
| 269 |
glb.real_ttyfd = -1; |
| 270 |
|
| 271 |
strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); |
| 272 |
|
| 273 |
hungup = 0; |
| 274 |
kill_link = 0; |
| 275 |
|
| 276 |
for (state = 0;;) { |
| 277 |
/* If the user specified the device name, become the |
| 278 |
user before opening it. */ |
| 279 |
|
| 280 |
if (pppoatm_options[0].priority < OPRIO_ROOT) |
| 281 |
seteuid(uid); |
| 282 |
|
| 283 |
errno = 0; |
| 284 |
|
| 285 |
switch (state) { |
| 286 |
case 0: |
| 287 |
if ((glb.real_ttyfd = socket(AF_ATMPVC, SOCK_DGRAM, 0)) == -1) { |
| 288 |
error("socket(AF_ATMPVC, SOCK_DGRAM, 0): %m"); |
| 289 |
break; |
| 290 |
} |
| 291 |
state++; |
| 292 |
case 1: |
| 293 |
#define pppoatm_overhead() (glb.llc_encaps ? (4+2) : 2) |
| 294 |
|
| 295 |
glb.qos.txtp.max_sdu = lcp_allowoptions[0].mru + pppoatm_overhead(); |
| 296 |
glb.qos.rxtp.max_sdu = lcp_wantoptions[0].mru + pppoatm_overhead(); |
| 297 |
|
| 298 |
if (setsockopt(glb.real_ttyfd, SOL_ATM, SO_ATMQOS, &glb.qos, sizeof(glb.qos)) < 0) { |
| 299 |
error("setsockopt(SO_ATMQOS): %m"); |
| 300 |
break; |
| 301 |
} |
| 302 |
state++; |
| 303 |
case 2: |
| 304 |
/* TODO: accept on SVCs... see glb.pppoatm_accept */ |
| 305 |
if (connect(glb.real_ttyfd, (struct sockaddr *) &glb.pvcaddr, |
| 306 |
sizeof(struct sockaddr_atmpvc))) { |
| 307 |
error("connect(%s): %m", devnam); |
| 308 |
break; |
| 309 |
} |
| 310 |
state++; |
| 311 |
default: |
| 312 |
case 3: |
| 313 |
glb.pppoatm_max_mtu = lcp_allowoptions[0].mru; |
| 314 |
glb.pppoatm_max_mru = lcp_wantoptions[0].mru; |
| 315 |
|
| 316 |
break; |
| 317 |
} |
| 318 |
|
| 319 |
if (pppoatm_options[0].priority < OPRIO_ROOT) { |
| 320 |
int err = errno; |
| 321 |
|
| 322 |
seteuid(0); |
| 323 |
errno = err; |
| 324 |
} |
| 325 |
|
| 326 |
if (state == 3) |
| 327 |
break; |
| 328 |
|
| 329 |
if (!persist || errno != EINTR) { |
| 330 |
if (glb.real_ttyfd >= 0) { |
| 331 |
close(glb.real_ttyfd); |
| 332 |
glb.real_ttyfd = -1; |
| 333 |
} |
| 334 |
|
| 335 |
error("Failed to open %s: %m", devnam); |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (glb.real_ttyfd == -1) |
| 341 |
status = EXIT_OPEN_FAILED; |
| 342 |
|
| 343 |
return glb.real_ttyfd; |
| 344 |
} |
| 345 |
|
| 346 |
static |
| 347 |
void pppoatm_disconnect(void) { |
| 348 |
Dinfo("In %s", __FUNCTION__); |
| 349 |
} |
| 350 |
|
| 351 |
static |
| 352 |
int pppoatm_establish_ppp (int tty_fd) { |
| 353 |
struct atm_backend_ppp be; |
| 354 |
|
| 355 |
Dinfo("In %s", __FUNCTION__); |
| 356 |
|
| 357 |
be.backend_num = ATM_BACKEND_PPP; |
| 358 |
if (!glb.llc_encaps) |
| 359 |
be.encaps = PPPOATM_ENCAPS_VC; |
| 360 |
else if (!glb.vc_encaps) |
| 361 |
be.encaps = PPPOATM_ENCAPS_LLC; |
| 362 |
else |
| 363 |
be.encaps = PPPOATM_ENCAPS_AUTODETECT; |
| 364 |
if (ioctl(tty_fd, ATM_SETBACKEND, &be) < 0) { |
| 365 |
error("ioctl(ATM_SETBACKEND): %m"); |
| 366 |
return -1; |
| 367 |
} |
| 368 |
|
| 369 |
return generic_establish_ppp(tty_fd); |
| 370 |
} |
| 371 |
|
| 372 |
static |
| 373 |
void pppoatm_disestablish_ppp(int tty_fd) { |
| 374 |
atm_backend_t be = ATM_BACKEND_RAW; |
| 375 |
|
| 376 |
Dinfo("In %s", __FUNCTION__); |
| 377 |
|
| 378 |
/* 2.4 doesn't support this yet */ |
| 379 |
(void) ioctl(tty_fd, ATM_SETBACKEND, &be); |
| 380 |
|
| 381 |
generic_disestablish_ppp(tty_fd); |
| 382 |
} |
| 383 |
|
| 384 |
static |
| 385 |
void pppoatm_cleanup(void) |
| 386 |
{ |
| 387 |
Dinfo("In %s", __FUNCTION__); |
| 388 |
|
| 389 |
pppoatm_close(); |
| 390 |
} |
| 391 |
|
| 392 |
static |
| 393 |
void pppoatm_close(void) |
| 394 |
{ |
| 395 |
Dinfo("In %s", __FUNCTION__); |
| 396 |
|
| 397 |
if (glb.real_ttyfd >= 0) { |
| 398 |
close(glb.real_ttyfd); |
| 399 |
glb.real_ttyfd = -1; |
| 400 |
} |
| 401 |
} |
| 402 |
/* The end */ |