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 nglogx(const char *, ...) __printflike(1, 2);
71 static int ReceivedSignal
;
74 usage(const char *prog
)
76 fprintf(stderr
, "usage: %s [-Fd] [-P pidfile] [-a name] [-e exec | -l label]"
77 " [-p provider] interface\n", prog
);
88 ConfigureNode(const char *prog __unused
, const char *iface
, const char *provider
,
89 int cs
, int ds __unused
, int debug
, struct ngm_connect
*ngc
)
92 * We're going to do this with the passed `ds' & `cs' descriptors:
102 * .---------. .-----------.
103 * | pppoe | | socket |
104 * | <iface> |(pppoe-<pid>)<---->(pppoe-<pid>)| <unnamed> |
105 * `--------- `-----------'
107 * ^ .-----------. .-------------.
108 * | | socket | | ppp -direct |
109 * `--->(exec-<pid>)| <unnamed> |--fd--| provider |
110 * `-----------' `-------------'
112 * where there are potentially many ppp processes running off of the
114 * The exec-<pid> hook isn't made 'till we Spawn().
118 struct ngpppoe_init_data
*data
;
119 const struct hooklist
*hlist
;
120 const struct nodeinfo
*ninfo
;
121 const struct linkinfo
*nlink
;
122 struct ngm_mkpeer mkp
;
123 struct ng_mesg
*resp
;
129 * Ask for a list of hooks attached to the "ether" node. This node should
130 * magically exist as a way of hooking stuff onto an ethernet device
132 epath
= (char *)alloca(strlen(iface
) + 2);
133 sprintf(epath
, "%s:", iface
);
136 fprintf(stderr
, "Sending NGM_LISTHOOKS to %s\n", epath
);
138 if (NgSendMsg(cs
, epath
, NGM_GENERIC_COOKIE
, NGM_LISTHOOKS
, NULL
, 0) < 0) {
140 fprintf(stderr
, "%s Cannot send a netgraph message: Invalid interface\n",
143 fprintf(stderr
, "%s Cannot send a netgraph message: %s\n",
144 epath
, strerror(errno
));
145 return EX_UNAVAILABLE
;
148 /* Get our list back */
149 resp
= (struct ng_mesg
*)rbuf
;
150 if (NgRecvMsg(cs
, resp
, sizeof rbuf
, NULL
) <= 0) {
151 perror("Cannot get netgraph response");
152 return EX_UNAVAILABLE
;
155 hlist
= (const struct hooklist
*)resp
->data
;
156 ninfo
= &hlist
->nodeinfo
;
159 fprintf(stderr
, "Got reply from id [%x]: Type %s with %d hooks\n",
160 ninfo
->id
, ninfo
->type
, ninfo
->hooks
);
162 /* Make sure we've got the right type of node */
163 if (strncmp(ninfo
->type
, NG_ETHER_NODE_TYPE
, sizeof NG_ETHER_NODE_TYPE
- 1)) {
164 fprintf(stderr
, "%s Unexpected node type ``%s'' (wanted ``"
165 NG_ETHER_NODE_TYPE
"'')\n", epath
, ninfo
->type
);
169 /* look for a hook already attached. */
170 for (f
= 0; f
< ninfo
->hooks
; f
++) {
171 nlink
= &hlist
->link
[f
];
174 fprintf(stderr
, " Got [%x]:%s -> [%x]:%s\n", ninfo
->id
,
175 nlink
->ourhook
, nlink
->nodeinfo
.id
, nlink
->peerhook
);
177 if (!strcmp(nlink
->ourhook
, NG_ETHER_HOOK_ORPHAN
) ||
178 !strcmp(nlink
->ourhook
, NG_ETHER_HOOK_DIVERT
)) {
180 * Something is using the data coming out of this `ether' node.
181 * If it's a PPPoE node, we use that node, otherwise we complain that
182 * someone else is using the node.
184 if (strcmp(nlink
->nodeinfo
.type
, NG_PPPOE_NODE_TYPE
)) {
185 fprintf(stderr
, "%s Node type %s is currently active\n",
186 epath
, nlink
->nodeinfo
.type
);
187 return EX_UNAVAILABLE
;
193 if (f
== ninfo
->hooks
) {
195 * Create a new PPPoE node connected to the `ether' node using
196 * the magic `orphan' and `ethernet' hooks
198 snprintf(mkp
.type
, sizeof mkp
.type
, "%s", NG_PPPOE_NODE_TYPE
);
199 snprintf(mkp
.ourhook
, sizeof mkp
.ourhook
, "%s", NG_ETHER_HOOK_ORPHAN
);
200 snprintf(mkp
.peerhook
, sizeof mkp
.peerhook
, "%s", NG_PPPOE_HOOK_ETHERNET
);
203 fprintf(stderr
, "Send MKPEER: %s%s -> [type %s]:%s\n", epath
,
204 mkp
.ourhook
, mkp
.type
, mkp
.peerhook
);
206 if (NgSendMsg(cs
, epath
, NGM_GENERIC_COOKIE
,
207 NGM_MKPEER
, &mkp
, sizeof mkp
) < 0) {
208 fprintf(stderr
, "%s Cannot create a peer PPPoE node: %s\n",
209 epath
, strerror(errno
));
214 /* Connect the PPPoE node to our socket node. */
215 snprintf(ngc
->path
, sizeof ngc
->path
, "%s%s", epath
, NG_ETHER_HOOK_ORPHAN
);
216 snprintf(ngc
->ourhook
, sizeof ngc
->ourhook
, "pppoe-%ld", (long)getpid());
217 memcpy(ngc
->peerhook
, ngc
->ourhook
, sizeof ngc
->peerhook
);
219 if (NgSendMsg(cs
, ".:", NGM_GENERIC_COOKIE
,
220 NGM_CONNECT
, ngc
, sizeof *ngc
) < 0) {
221 perror("Cannot CONNECT PPPoE and socket nodes");
225 plen
= strlen(provider
);
227 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ plen
);
228 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
->peerhook
);
229 memcpy(data
->data
, provider
, plen
);
230 data
->data_len
= plen
;
232 spath
= (char *)alloca(strlen(ngc
->peerhook
) + 3);
234 strcpy(spath
+ 2, ngc
->ourhook
);
238 fprintf(stderr
, "Sending PPPOE_LISTEN to %s, provider %s\n",
241 fprintf(stderr
, "Sending PPPOE_LISTEN to %s\n", spath
);
244 if (NgSendMsg(cs
, spath
, NGM_PPPOE_COOKIE
, NGM_PPPOE_LISTEN
,
245 data
, sizeof *data
+ plen
) == -1) {
246 fprintf(stderr
, "%s: Cannot LISTEN on netgraph node: %s\n",
247 spath
, strerror(errno
));
255 Spawn(const char *prog __unused
, const char *acname
, const char *provider
,
256 const char *exec
, struct ngm_connect ngc
, int cs
, int ds
, void *request
,
257 size_t sz
, int debug
)
259 char msgbuf
[sizeof(struct ng_mesg
) + sizeof(struct ngpppoe_sts
)];
260 struct ng_mesg
*rep
= (struct ng_mesg
*)msgbuf
;
261 struct ngpppoe_sts
*sts
= (struct ngpppoe_sts
*)(msgbuf
+ sizeof *rep
);
262 struct ngpppoe_init_data
*data
;
263 char env
[sizeof(HISMACADDR
)+18], unknown
[14], sessionid
[5], *path
;
264 unsigned char *macaddr
;
268 switch ((ret
= fork())) {
270 syslog(LOG_ERR
, "fork: %m");
285 /* Create a new socket node */
287 syslog(LOG_INFO
, "Creating a new socket node");
289 if (NgMkSockNode(NULL
, &cs
, &ds
) == -1) {
290 syslog(LOG_ERR
, "Cannot create netgraph socket node: %m");
294 /* Connect the PPPoE node to our new socket node. */
295 snprintf(ngc
.ourhook
, sizeof ngc
.ourhook
, "exec-%ld", (long)getpid());
296 memcpy(ngc
.peerhook
, ngc
.ourhook
, sizeof ngc
.peerhook
);
299 syslog(LOG_INFO
, "Sending CONNECT from .:%s -> %s.%s",
300 ngc
.ourhook
, ngc
.path
, ngc
.peerhook
);
301 if (NgSendMsg(cs
, ".:", NGM_GENERIC_COOKIE
,
302 NGM_CONNECT
, &ngc
, sizeof ngc
) < 0) {
303 syslog(LOG_ERR
, "Cannot CONNECT PPPoE and socket nodes: %m");
308 * If we tell the socket node not to LINGER, it will go away when
309 * the last hook is removed.
312 syslog(LOG_INFO
, "Sending NGM_SOCK_CMD_NOLINGER to socket");
313 if (NgSendMsg(cs
, ".:", NGM_SOCKET_COOKIE
,
314 NGM_SOCK_CMD_NOLINGER
, NULL
, 0) < 0) {
315 syslog(LOG_ERR
, "Cannot send NGM_SOCK_CMD_NOLINGER: %m");
319 /* Put the PPPoE node into OFFER mode */
320 slen
= strlen(acname
);
321 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ slen
);
322 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
.ourhook
);
323 memcpy(data
->data
, acname
, slen
);
324 data
->data_len
= slen
;
326 path
= (char *)alloca(strlen(ngc
.ourhook
) + 3);
328 strcpy(path
+ 2, ngc
.ourhook
);
330 syslog(LOG_INFO
, "Offering to %s as access concentrator %s",
332 if (NgSendMsg(cs
, path
, NGM_PPPOE_COOKIE
, NGM_PPPOE_OFFER
,
333 data
, sizeof *data
+ slen
) == -1) {
334 syslog(LOG_INFO
, "%s: Cannot OFFER on netgraph node: %m", path
);
337 /* If we have a provider code, set it */
339 slen
= strlen(provider
);
340 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ slen
);
341 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
.ourhook
);
342 memcpy(data
->data
, provider
, slen
);
343 data
->data_len
= slen
;
345 syslog(LOG_INFO
, "adding to %s as offered service %s",
347 if (NgSendMsg(cs
, path
, NGM_PPPOE_COOKIE
, NGM_PPPOE_SERVICE
,
348 data
, sizeof *data
+ slen
) == -1) {
349 syslog(LOG_INFO
, "%s: Cannot add service on netgraph node: %m", path
);
354 /* Put the peer's MAC address in the environment */
355 if (sz
>= sizeof(struct ether_header
)) {
356 macaddr
= ((struct ether_header
*)request
)->ether_shost
;
357 snprintf(env
, sizeof(env
), "%s=%x:%x:%x:%x:%x:%x", HISMACADDR
,
358 macaddr
[0], macaddr
[1], macaddr
[2], macaddr
[3], macaddr
[4],
360 if (putenv(env
) != 0)
361 syslog(LOG_INFO
, "putenv: cannot set %s: %m", env
);
364 /* And send our request data to the waiting node */
366 syslog(LOG_INFO
, "Sending original request to %s (%zu bytes)", path
, sz
);
367 if (NgSendData(ds
, ngc
.ourhook
, request
, sz
) == -1) {
368 syslog(LOG_ERR
, "Cannot send original request to %s: %m", path
);
372 /* Then wait for a success indication */
375 syslog(LOG_INFO
, "Waiting for a SUCCESS reply %s", path
);
378 if ((ret
= NgRecvMsg(cs
, rep
, sizeof msgbuf
, NULL
)) < 0) {
379 syslog(LOG_ERR
, "%s: Cannot receive a message: %m", path
);
384 /* The socket has been closed */
385 syslog(LOG_INFO
, "%s: Client timed out", path
);
389 if (rep
->header
.version
!= NG_VERSION
) {
390 syslog(LOG_ERR
, "%ld: Unexpected netgraph version, expected %ld",
391 (long)rep
->header
.version
, (long)NG_VERSION
);
395 if (rep
->header
.typecookie
!= NGM_PPPOE_COOKIE
) {
396 syslog(LOG_INFO
, "%ld: Unexpected netgraph cookie, expected %ld",
397 (long)rep
->header
.typecookie
, (long)NGM_PPPOE_COOKIE
);
401 switch (rep
->header
.cmd
) {
402 case NGM_PPPOE_SET_FLAG
: msg
= "SET_FLAG"; break;
403 case NGM_PPPOE_CONNECT
: msg
= "CONNECT"; break;
404 case NGM_PPPOE_LISTEN
: msg
= "LISTEN"; break;
405 case NGM_PPPOE_OFFER
: msg
= "OFFER"; break;
406 case NGM_PPPOE_SUCCESS
: msg
= "SUCCESS"; break;
407 case NGM_PPPOE_FAIL
: msg
= "FAIL"; break;
408 case NGM_PPPOE_CLOSE
: msg
= "CLOSE"; break;
409 case NGM_PPPOE_GET_STATUS
: msg
= "GET_STATUS"; break;
410 case NGM_PPPOE_ACNAME
:
412 if (setenv("ACNAME", sts
->hook
, 1) != 0)
413 syslog(LOG_WARNING
, "setenv: cannot set ACNAME=%s: %m",
416 case NGM_PPPOE_SESSIONID
:
418 snprintf(sessionid
, sizeof sessionid
, "%04x", *(u_int16_t
*)sts
);
419 if (setenv("SESSIONID", sessionid
, 1) != 0)
420 syslog(LOG_WARNING
, "setenv: cannot set SESSIONID=%s: %m",
424 snprintf(unknown
, sizeof unknown
, "<%d>", (int)rep
->header
.cmd
);
429 switch (rep
->header
.cmd
) {
431 case NGM_PPPOE_CLOSE
:
432 syslog(LOG_ERR
, "Received NGM_PPPOE_%s (hook \"%s\")",
437 syslog(LOG_INFO
, "Received NGM_PPPOE_%s (hook \"%s\")", msg
, sts
->hook
);
438 } while (rep
->header
.cmd
!= NGM_PPPOE_SUCCESS
);
440 dup2(ds
, STDIN_FILENO
);
441 dup2(ds
, STDOUT_FILENO
);
446 syslog(LOG_INFO
, "Executing: %s", exec
);
447 execlp(_PATH_BSHELL
, _PATH_BSHELL
, "-c", exec
, (char *)NULL
);
448 syslog(LOG_ERR
, "execlp failed: %m");
455 syslog(LOG_ERR
, "Second fork failed: %m");
464 const char *module
[] = { "netgraph", "ng_socket", "ng_ether", "ng_pppoe" };
467 for (f
= 0; f
< sizeof module
/ sizeof *module
; f
++)
468 if (modfind(module
[f
]) == -1 && kldload(module
[f
]) == -1) {
469 fprintf(stderr
, "kldload: %s: %s\n", module
[f
], strerror(errno
));
478 nglog(const char *fmt
, ...)
483 snprintf(nfmt
, sizeof nfmt
, "%s: %s", fmt
, strerror(errno
));
485 vsyslog(LOG_INFO
, nfmt
, ap
);
490 nglogx(const char *fmt
, ...)
495 vsyslog(LOG_INFO
, fmt
, ap
);
500 main(int argc
, char *argv
[])
502 char hostname
[MAXHOSTNAMELEN
], *exec
, rhook
[NG_HOOKSIZ
];
503 unsigned char response
[1024];
504 const char *label
, *prog
, *provider
, *acname
;
505 struct ngm_connect ngc
;
506 struct sigaction act
;
507 int ch
, cs
, ds
, ret
, optF
, optd
, optn
, sz
, f
;
510 prog
= strrchr(argv
[0], '/');
511 prog
= prog
? prog
+ 1 : argv
[0];
517 optF
= optd
= optn
= 0;
519 while ((ch
= getopt(argc
, argv
, "FP:a:de:l:n:p:")) != -1) {
547 NgSetDebug(atoi(optarg
));
559 if (optind
>= argc
|| optind
+ 2 < argc
)
562 if (exec
!= NULL
&& label
!= NULL
)
569 fprintf(stderr
, "%s: Either a provider, a label or an exec command"
570 " must be given\n", prog
);
573 exec
= (char *)alloca(sizeof DEFAULT_EXEC_PREFIX
+ strlen(label
));
575 fprintf(stderr
, "%s: Cannot allocate %zu bytes\n", prog
,
576 sizeof DEFAULT_EXEC_PREFIX
+ strlen(label
));
579 strcpy(exec
, DEFAULT_EXEC_PREFIX
);
580 strcpy(exec
+ sizeof DEFAULT_EXEC_PREFIX
- 1, label
);
583 if (acname
== NULL
) {
586 if (gethostname(hostname
, sizeof hostname
))
587 strcpy(hostname
, "localhost");
588 else if ((dot
= strchr(hostname
, '.')))
596 return EX_UNAVAILABLE
;
599 /* Create a socket node */
600 if (NgMkSockNode(NULL
, &cs
, &ds
) == -1) {
601 perror("Cannot create netgraph socket node");
605 /* Connect it up (and fill in `ngc') */
606 if ((ret
= ConfigureNode(prog
, argv
[optind
], provider
, cs
, ds
,
613 if (!optF
&& daemon(1, 0) == -1) {
621 if (pidfile
!= NULL
) {
624 if ((fp
= fopen(pidfile
, "w")) == NULL
) {
630 fprintf(fp
, "%d\n", (int)getpid());
635 openlog(prog
, LOG_PID
| (optF
? LOG_PERROR
: 0), LOG_DAEMON
);
637 NgSetErrLog(nglog
, nglogx
);
639 memset(&act
, '\0', sizeof act
);
640 act
.sa_handler
= Farewell
;
642 sigemptyset(&act
.sa_mask
);
643 sigaction(SIGHUP
, &act
, NULL
);
644 sigaction(SIGINT
, &act
, NULL
);
645 sigaction(SIGQUIT
, &act
, NULL
);
646 sigaction(SIGTERM
, &act
, NULL
);
648 while (!ReceivedSignal
) {
650 syslog(LOG_INFO
, "Listening as provider %s", provider
);
652 syslog(LOG_INFO
, "Listening");
654 switch (sz
= NgRecvData(ds
, response
, sizeof response
, rhook
)) {
656 syslog(LOG_INFO
, "NgRecvData: %m");
659 syslog(LOG_INFO
, "NgRecvData: socket closed");
665 ptr
= dbuf
= alloca(sz
* 2 + 1);
666 for (f
= 0; f
< sz
; f
++, ptr
+= 2)
667 sprintf(ptr
, "%02x", (u_char
)response
[f
]);
669 syslog(LOG_INFO
, "Got %d bytes of data: %s", sz
, dbuf
);
673 ret
= EX_UNAVAILABLE
;
676 Spawn(prog
, acname
, provider
, exec
, ngc
, cs
, ds
, response
, sz
, optd
);
682 if (ReceivedSignal
) {
683 syslog(LOG_INFO
, "Received signal %d, exiting", ReceivedSignal
);
685 signal(ReceivedSignal
, SIG_DFL
);
686 raise(ReceivedSignal
);
690 ret
= -ReceivedSignal
;