2 * Copyright (c) 1999-2001 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/libexec/pppoed/pppoed.c,v 1.2.6.8 2002/06/17 02:21:25 brian Exp $
27 * $DragonFly: src/libexec/pppoed/pppoed.c,v 1.4 2007/06/04 00:40:31 swildner Exp $
30 #include <sys/param.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
37 #include <net/ethernet.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
40 #include <netgraph/ether/ng_ether.h>
41 #include <netgraph/ng_message.h>
42 #include <netgraph/pppoe/ng_pppoe.h>
43 #include <netgraph/socket/ng_socket.h>
53 #include <sys/fcntl.h>
55 #include <sys/linker.h>
56 #include <sys/module.h>
65 #define DEFAULT_EXEC_PREFIX "exec /usr/sbin/ppp -direct "
66 #define HISMACADDR "HISMACADDR"
67 #define SESSION_ID "SESSION_ID"
69 static void nglog(const char *, ...) __printflike(1, 2);
70 static void nglogx(const char *, ...) __printflike(1, 2);
72 static int ReceivedSignal
;
75 usage(const char *prog
)
77 fprintf(stderr
, "usage: %s [-Fd] [-P pidfile] [-a name] [-e exec | -l label]"
78 " [-p provider] interface\n", prog
);
89 ConfigureNode(const char *prog __unused
, const char *iface
, const char *provider
,
90 int cs
, int ds __unused
, int debug
, struct ngm_connect
*ngc
)
93 * We're going to do this with the passed `ds' & `cs' descriptors:
103 * .---------. .-----------.
104 * | pppoe | | socket |
105 * | <iface> |(pppoe-<pid>)<---->(pppoe-<pid>)| <unnamed> |
106 * `--------- `-----------'
108 * ^ .-----------. .-------------.
109 * | | socket | | ppp -direct |
110 * `--->(exec-<pid>)| <unnamed> |--fd--| provider |
111 * `-----------' `-------------'
113 * where there are potentially many ppp processes running off of the
115 * The exec-<pid> hook isn't made 'till we Spawn().
119 struct ngpppoe_init_data
*data
;
120 const struct hooklist
*hlist
;
121 const struct nodeinfo
*ninfo
;
122 const struct linkinfo
*nlink
;
123 struct ngm_mkpeer mkp
;
124 struct ng_mesg
*resp
;
130 * Ask for a list of hooks attached to the "ether" node. This node should
131 * magically exist as a way of hooking stuff onto an ethernet device
133 epath
= (char *)alloca(strlen(iface
) + 2);
134 sprintf(epath
, "%s:", iface
);
137 fprintf(stderr
, "Sending NGM_LISTHOOKS to %s\n", epath
);
139 if (NgSendMsg(cs
, epath
, NGM_GENERIC_COOKIE
, NGM_LISTHOOKS
, NULL
, 0) < 0) {
141 fprintf(stderr
, "%s Cannot send a netgraph message: Invalid interface\n",
144 fprintf(stderr
, "%s Cannot send a netgraph message: %s\n",
145 epath
, strerror(errno
));
146 return EX_UNAVAILABLE
;
149 /* Get our list back */
150 resp
= (struct ng_mesg
*)rbuf
;
151 if (NgRecvMsg(cs
, resp
, sizeof rbuf
, NULL
) <= 0) {
152 perror("Cannot get netgraph response");
153 return EX_UNAVAILABLE
;
156 hlist
= (const struct hooklist
*)resp
->data
;
157 ninfo
= &hlist
->nodeinfo
;
160 fprintf(stderr
, "Got reply from id [%x]: Type %s with %d hooks\n",
161 ninfo
->id
, ninfo
->type
, ninfo
->hooks
);
163 /* Make sure we've got the right type of node */
164 if (strncmp(ninfo
->type
, NG_ETHER_NODE_TYPE
, sizeof NG_ETHER_NODE_TYPE
- 1)) {
165 fprintf(stderr
, "%s Unexpected node type ``%s'' (wanted ``"
166 NG_ETHER_NODE_TYPE
"'')\n", epath
, ninfo
->type
);
170 /* look for a hook already attached. */
171 for (f
= 0; f
< ninfo
->hooks
; f
++) {
172 nlink
= &hlist
->link
[f
];
175 fprintf(stderr
, " Got [%x]:%s -> [%x]:%s\n", ninfo
->id
,
176 nlink
->ourhook
, nlink
->nodeinfo
.id
, nlink
->peerhook
);
178 if (!strcmp(nlink
->ourhook
, NG_ETHER_HOOK_ORPHAN
) ||
179 !strcmp(nlink
->ourhook
, NG_ETHER_HOOK_DIVERT
)) {
181 * Something is using the data coming out of this `ether' node.
182 * If it's a PPPoE node, we use that node, otherwise we complain that
183 * someone else is using the node.
185 if (strcmp(nlink
->nodeinfo
.type
, NG_PPPOE_NODE_TYPE
)) {
186 fprintf(stderr
, "%s Node type %s is currently active\n",
187 epath
, nlink
->nodeinfo
.type
);
188 return EX_UNAVAILABLE
;
194 if (f
== ninfo
->hooks
) {
196 * Create a new PPPoE node connected to the `ether' node using
197 * the magic `orphan' and `ethernet' hooks
199 snprintf(mkp
.type
, sizeof mkp
.type
, "%s", NG_PPPOE_NODE_TYPE
);
200 snprintf(mkp
.ourhook
, sizeof mkp
.ourhook
, "%s", NG_ETHER_HOOK_ORPHAN
);
201 snprintf(mkp
.peerhook
, sizeof mkp
.peerhook
, "%s", NG_PPPOE_HOOK_ETHERNET
);
204 fprintf(stderr
, "Send MKPEER: %s%s -> [type %s]:%s\n", epath
,
205 mkp
.ourhook
, mkp
.type
, mkp
.peerhook
);
207 if (NgSendMsg(cs
, epath
, NGM_GENERIC_COOKIE
,
208 NGM_MKPEER
, &mkp
, sizeof mkp
) < 0) {
209 fprintf(stderr
, "%s Cannot create a peer PPPoE node: %s\n",
210 epath
, strerror(errno
));
215 /* Connect the PPPoE node to our socket node. */
216 snprintf(ngc
->path
, sizeof ngc
->path
, "%s%s", epath
, NG_ETHER_HOOK_ORPHAN
);
217 snprintf(ngc
->ourhook
, sizeof ngc
->ourhook
, "pppoe-%ld", (long)getpid());
218 memcpy(ngc
->peerhook
, ngc
->ourhook
, sizeof ngc
->peerhook
);
220 if (NgSendMsg(cs
, ".:", NGM_GENERIC_COOKIE
,
221 NGM_CONNECT
, ngc
, sizeof *ngc
) < 0) {
222 perror("Cannot CONNECT PPPoE and socket nodes");
226 plen
= strlen(provider
);
228 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ plen
);
229 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
->peerhook
);
230 memcpy(data
->data
, provider
, plen
);
231 data
->data_len
= plen
;
233 spath
= (char *)alloca(strlen(ngc
->peerhook
) + 3);
235 strcpy(spath
+ 2, ngc
->ourhook
);
239 fprintf(stderr
, "Sending PPPOE_LISTEN to %s, provider %s\n",
242 fprintf(stderr
, "Sending PPPOE_LISTEN to %s\n", spath
);
245 if (NgSendMsg(cs
, spath
, NGM_PPPOE_COOKIE
, NGM_PPPOE_LISTEN
,
246 data
, sizeof *data
+ plen
) == -1) {
247 fprintf(stderr
, "%s: Cannot LISTEN on netgraph node: %s\n",
248 spath
, strerror(errno
));
256 Spawn(const char *prog __unused
, const char *acname
, const char *provider
,
257 const char *exec
, struct ngm_connect ngc
, int cs
, int ds
, void *request
,
258 size_t sz
, int debug
)
260 char msgbuf
[sizeof(struct ng_mesg
) + sizeof(struct ngpppoe_sts
)];
261 struct ng_mesg
*rep
= (struct ng_mesg
*)msgbuf
;
262 struct ngpppoe_sts
*sts
= (struct ngpppoe_sts
*)(msgbuf
+ sizeof *rep
);
263 struct ngpppoe_init_data
*data
;
264 char env
[18], unknown
[14], sessionid
[5], *path
;
265 unsigned char *macaddr
;
269 switch ((ret
= fork())) {
271 syslog(LOG_ERR
, "fork: %m");
286 /* Create a new socket node */
288 syslog(LOG_INFO
, "Creating a new socket node");
290 if (NgMkSockNode(NULL
, &cs
, &ds
) == -1) {
291 syslog(LOG_ERR
, "Cannot create netgraph socket node: %m");
295 /* Connect the PPPoE node to our new socket node. */
296 snprintf(ngc
.ourhook
, sizeof ngc
.ourhook
, "exec-%ld", (long)getpid());
297 memcpy(ngc
.peerhook
, ngc
.ourhook
, sizeof ngc
.peerhook
);
300 syslog(LOG_INFO
, "Sending CONNECT from .:%s -> %s.%s",
301 ngc
.ourhook
, ngc
.path
, ngc
.peerhook
);
302 if (NgSendMsg(cs
, ".:", NGM_GENERIC_COOKIE
,
303 NGM_CONNECT
, &ngc
, sizeof ngc
) < 0) {
304 syslog(LOG_ERR
, "Cannot CONNECT PPPoE and socket nodes: %m");
309 * If we tell the socket node not to LINGER, it will go away when
310 * the last hook is removed.
313 syslog(LOG_INFO
, "Sending NGM_SOCK_CMD_NOLINGER to socket");
314 if (NgSendMsg(cs
, ".:", NGM_SOCKET_COOKIE
,
315 NGM_SOCK_CMD_NOLINGER
, NULL
, 0) < 0) {
316 syslog(LOG_ERR
, "Cannot send NGM_SOCK_CMD_NOLINGER: %m");
320 /* Put the PPPoE node into OFFER mode */
321 slen
= strlen(acname
);
322 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ slen
);
323 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
.ourhook
);
324 memcpy(data
->data
, acname
, slen
);
325 data
->data_len
= slen
;
327 path
= (char *)alloca(strlen(ngc
.ourhook
) + 3);
329 strcpy(path
+ 2, ngc
.ourhook
);
331 syslog(LOG_INFO
, "Offering to %s as access concentrator %s",
333 if (NgSendMsg(cs
, path
, NGM_PPPOE_COOKIE
, NGM_PPPOE_OFFER
,
334 data
, sizeof *data
+ slen
) == -1) {
335 syslog(LOG_INFO
, "%s: Cannot OFFER on netgraph node: %m", path
);
338 /* If we have a provider code, set it */
340 slen
= strlen(provider
);
341 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ slen
);
342 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
.ourhook
);
343 memcpy(data
->data
, provider
, slen
);
344 data
->data_len
= slen
;
346 syslog(LOG_INFO
, "adding to %s as offered service %s",
348 if (NgSendMsg(cs
, path
, NGM_PPPOE_COOKIE
, NGM_PPPOE_SERVICE
,
349 data
, sizeof *data
+ slen
) == -1) {
350 syslog(LOG_INFO
, "%s: Cannot add service on netgraph node: %m", path
);
355 /* Put the peer's MAC address in the environment */
356 if (sz
>= sizeof(struct ether_header
)) {
357 macaddr
= ((struct ether_header
*)request
)->ether_shost
;
358 snprintf(env
, sizeof(env
), "%x:%x:%x:%x:%x:%x",
359 macaddr
[0], macaddr
[1], macaddr
[2], macaddr
[3], macaddr
[4],
361 if (setenv(HISMACADDR
, env
, 1) != 0)
362 syslog(LOG_INFO
, "setenv: cannot set %s: %m", HISMACADDR
);
365 /* And send our request data to the waiting node */
367 syslog(LOG_INFO
, "Sending original request to %s (%zu bytes)", path
, sz
);
368 if (NgSendData(ds
, ngc
.ourhook
, request
, sz
) == -1) {
369 syslog(LOG_ERR
, "Cannot send original request to %s: %m", path
);
373 /* Then wait for a success indication */
376 syslog(LOG_INFO
, "Waiting for a SUCCESS reply %s", path
);
379 if ((ret
= NgRecvMsg(cs
, rep
, sizeof msgbuf
, NULL
)) < 0) {
380 syslog(LOG_ERR
, "%s: Cannot receive a message: %m", path
);
385 /* The socket has been closed */
386 syslog(LOG_INFO
, "%s: Client timed out", path
);
390 if (rep
->header
.version
!= NG_VERSION
) {
391 syslog(LOG_ERR
, "%ld: Unexpected netgraph version, expected %ld",
392 (long)rep
->header
.version
, (long)NG_VERSION
);
396 if (rep
->header
.typecookie
!= NGM_PPPOE_COOKIE
) {
397 syslog(LOG_INFO
, "%ld: Unexpected netgraph cookie, expected %ld",
398 (long)rep
->header
.typecookie
, (long)NGM_PPPOE_COOKIE
);
402 switch (rep
->header
.cmd
) {
403 case NGM_PPPOE_SET_FLAG
: msg
= "SET_FLAG"; break;
404 case NGM_PPPOE_CONNECT
: msg
= "CONNECT"; break;
405 case NGM_PPPOE_LISTEN
: msg
= "LISTEN"; break;
406 case NGM_PPPOE_OFFER
: msg
= "OFFER"; break;
407 case NGM_PPPOE_SUCCESS
: msg
= "SUCCESS"; break;
408 case NGM_PPPOE_FAIL
: msg
= "FAIL"; break;
409 case NGM_PPPOE_CLOSE
: msg
= "CLOSE"; break;
410 case NGM_PPPOE_GET_STATUS
: msg
= "GET_STATUS"; break;
411 case NGM_PPPOE_ACNAME
:
413 if (setenv("ACNAME", sts
->hook
, 1) != 0)
414 syslog(LOG_WARNING
, "setenv: cannot set ACNAME=%s: %m",
417 case NGM_PPPOE_SESSIONID
:
419 snprintf(sessionid
, sizeof sessionid
, "%04x", *(u_int16_t
*)sts
);
420 if (setenv("SESSIONID", sessionid
, 1) != 0)
421 syslog(LOG_WARNING
, "setenv: cannot set SESSIONID=%s: %m",
425 snprintf(unknown
, sizeof unknown
, "<%d>", (int)rep
->header
.cmd
);
430 switch (rep
->header
.cmd
) {
432 case NGM_PPPOE_CLOSE
:
433 syslog(LOG_ERR
, "Received NGM_PPPOE_%s (hook \"%s\")",
438 syslog(LOG_INFO
, "Received NGM_PPPOE_%s (hook \"%s\")", msg
, sts
->hook
);
439 } while (rep
->header
.cmd
!= NGM_PPPOE_SUCCESS
);
441 dup2(ds
, STDIN_FILENO
);
442 dup2(ds
, STDOUT_FILENO
);
447 syslog(LOG_INFO
, "Executing: %s", exec
);
448 execlp(_PATH_BSHELL
, _PATH_BSHELL
, "-c", exec
, NULL
);
449 syslog(LOG_ERR
, "execlp failed: %m");
456 syslog(LOG_ERR
, "Second fork failed: %m");
465 const char *module
[] = { "netgraph", "ng_socket", "ng_ether", "ng_pppoe" };
468 for (f
= 0; f
< sizeof module
/ sizeof *module
; f
++)
469 if (modfind(module
[f
]) == -1 && kldload(module
[f
]) == -1) {
470 fprintf(stderr
, "kldload: %s: %s\n", module
[f
], strerror(errno
));
479 nglog(const char *fmt
, ...)
484 snprintf(nfmt
, sizeof nfmt
, "%s: %s", fmt
, strerror(errno
));
486 vsyslog(LOG_INFO
, nfmt
, ap
);
491 nglogx(const char *fmt
, ...)
496 vsyslog(LOG_INFO
, fmt
, ap
);
501 main(int argc
, char *argv
[])
503 char hostname
[MAXHOSTNAMELEN
], *exec
, rhook
[NG_HOOKSIZ
];
504 unsigned char response
[1024];
505 const char *label
, *prog
, *provider
, *acname
;
506 struct ngm_connect ngc
;
507 struct sigaction act
;
508 int ch
, cs
, ds
, ret
, optF
, optd
, optn
, sz
, f
;
511 prog
= strrchr(argv
[0], '/');
512 prog
= prog
? prog
+ 1 : argv
[0];
518 optF
= optd
= optn
= 0;
520 while ((ch
= getopt(argc
, argv
, "FP:a:de:l:n:p:")) != -1) {
548 NgSetDebug(atoi(optarg
));
560 if (optind
>= argc
|| optind
+ 2 < argc
)
563 if (exec
!= NULL
&& label
!= NULL
)
570 fprintf(stderr
, "%s: Either a provider, a label or an exec command"
571 " must be given\n", prog
);
574 exec
= (char *)alloca(sizeof DEFAULT_EXEC_PREFIX
+ strlen(label
));
576 fprintf(stderr
, "%s: Cannot allocate %zu bytes\n", prog
,
577 sizeof DEFAULT_EXEC_PREFIX
+ strlen(label
));
580 strcpy(exec
, DEFAULT_EXEC_PREFIX
);
581 strcpy(exec
+ sizeof DEFAULT_EXEC_PREFIX
- 1, label
);
584 if (acname
== NULL
) {
587 if (gethostname(hostname
, sizeof hostname
))
588 strcpy(hostname
, "localhost");
589 else if ((dot
= strchr(hostname
, '.')))
597 return EX_UNAVAILABLE
;
600 /* Create a socket node */
601 if (NgMkSockNode(NULL
, &cs
, &ds
) == -1) {
602 perror("Cannot create netgraph socket node");
606 /* Connect it up (and fill in `ngc') */
607 if ((ret
= ConfigureNode(prog
, argv
[optind
], provider
, cs
, ds
,
614 if (!optF
&& daemon(1, 0) == -1) {
622 if (pidfile
!= NULL
) {
625 if ((fp
= fopen(pidfile
, "w")) == NULL
) {
631 fprintf(fp
, "%d\n", (int)getpid());
636 openlog(prog
, LOG_PID
| (optF
? LOG_PERROR
: 0), LOG_DAEMON
);
638 NgSetErrLog(nglog
, nglogx
);
640 memset(&act
, '\0', sizeof act
);
641 act
.sa_handler
= Farewell
;
643 sigemptyset(&act
.sa_mask
);
644 sigaction(SIGHUP
, &act
, NULL
);
645 sigaction(SIGINT
, &act
, NULL
);
646 sigaction(SIGQUIT
, &act
, NULL
);
647 sigaction(SIGTERM
, &act
, NULL
);
649 while (!ReceivedSignal
) {
651 syslog(LOG_INFO
, "Listening as provider %s", provider
);
653 syslog(LOG_INFO
, "Listening");
655 switch (sz
= NgRecvData(ds
, response
, sizeof response
, rhook
)) {
657 syslog(LOG_INFO
, "NgRecvData: %m");
660 syslog(LOG_INFO
, "NgRecvData: socket closed");
666 ptr
= dbuf
= alloca(sz
* 2 + 1);
667 for (f
= 0; f
< sz
; f
++, ptr
+= 2)
668 sprintf(ptr
, "%02x", (u_char
)response
[f
]);
670 syslog(LOG_INFO
, "Got %d bytes of data: %s", sz
, dbuf
);
674 ret
= EX_UNAVAILABLE
;
677 Spawn(prog
, acname
, provider
, exec
, ngc
, cs
, ds
, response
, sz
, optd
);
683 if (ReceivedSignal
) {
684 syslog(LOG_INFO
, "Received signal %d, exiting", ReceivedSignal
);
686 signal(ReceivedSignal
, SIG_DFL
);
687 raise(ReceivedSignal
);
691 ret
= -ReceivedSignal
;