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
, const char *iface
, const char *provider
,
89 int cs
, int ds
, 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
;
128 * Ask for a list of hooks attached to the "ether" node. This node should
129 * magically exist as a way of hooking stuff onto an ethernet device
131 epath
= (char *)alloca(strlen(iface
) + 2);
132 sprintf(epath
, "%s:", iface
);
135 fprintf(stderr
, "Sending NGM_LISTHOOKS to %s\n", epath
);
137 if (NgSendMsg(cs
, epath
, NGM_GENERIC_COOKIE
, NGM_LISTHOOKS
, NULL
, 0) < 0) {
139 fprintf(stderr
, "%s Cannot send a netgraph message: Invalid interface\n",
142 fprintf(stderr
, "%s Cannot send a netgraph message: %s\n",
143 epath
, strerror(errno
));
144 return EX_UNAVAILABLE
;
147 /* Get our list back */
148 resp
= (struct ng_mesg
*)rbuf
;
149 if (NgRecvMsg(cs
, resp
, sizeof rbuf
, NULL
) <= 0) {
150 perror("Cannot get netgraph response");
151 return EX_UNAVAILABLE
;
154 hlist
= (const struct hooklist
*)resp
->data
;
155 ninfo
= &hlist
->nodeinfo
;
158 fprintf(stderr
, "Got reply from id [%x]: Type %s with %d hooks\n",
159 ninfo
->id
, ninfo
->type
, ninfo
->hooks
);
161 /* Make sure we've got the right type of node */
162 if (strncmp(ninfo
->type
, NG_ETHER_NODE_TYPE
, sizeof NG_ETHER_NODE_TYPE
- 1)) {
163 fprintf(stderr
, "%s Unexpected node type ``%s'' (wanted ``"
164 NG_ETHER_NODE_TYPE
"'')\n", epath
, ninfo
->type
);
168 /* look for a hook already attached. */
169 for (f
= 0; f
< ninfo
->hooks
; f
++) {
170 nlink
= &hlist
->link
[f
];
173 fprintf(stderr
, " Got [%x]:%s -> [%x]:%s\n", ninfo
->id
,
174 nlink
->ourhook
, nlink
->nodeinfo
.id
, nlink
->peerhook
);
176 if (!strcmp(nlink
->ourhook
, NG_ETHER_HOOK_ORPHAN
) ||
177 !strcmp(nlink
->ourhook
, NG_ETHER_HOOK_DIVERT
)) {
179 * Something is using the data coming out of this `ether' node.
180 * If it's a PPPoE node, we use that node, otherwise we complain that
181 * someone else is using the node.
183 if (strcmp(nlink
->nodeinfo
.type
, NG_PPPOE_NODE_TYPE
)) {
184 fprintf(stderr
, "%s Node type %s is currently active\n",
185 epath
, nlink
->nodeinfo
.type
);
186 return EX_UNAVAILABLE
;
192 if (f
== ninfo
->hooks
) {
194 * Create a new PPPoE node connected to the `ether' node using
195 * the magic `orphan' and `ethernet' hooks
197 snprintf(mkp
.type
, sizeof mkp
.type
, "%s", NG_PPPOE_NODE_TYPE
);
198 snprintf(mkp
.ourhook
, sizeof mkp
.ourhook
, "%s", NG_ETHER_HOOK_ORPHAN
);
199 snprintf(mkp
.peerhook
, sizeof mkp
.peerhook
, "%s", NG_PPPOE_HOOK_ETHERNET
);
202 fprintf(stderr
, "Send MKPEER: %s%s -> [type %s]:%s\n", epath
,
203 mkp
.ourhook
, mkp
.type
, mkp
.peerhook
);
205 if (NgSendMsg(cs
, epath
, NGM_GENERIC_COOKIE
,
206 NGM_MKPEER
, &mkp
, sizeof mkp
) < 0) {
207 fprintf(stderr
, "%s Cannot create a peer PPPoE node: %s\n",
208 epath
, strerror(errno
));
213 /* Connect the PPPoE node to our socket node. */
214 snprintf(ngc
->path
, sizeof ngc
->path
, "%s%s", epath
, NG_ETHER_HOOK_ORPHAN
);
215 snprintf(ngc
->ourhook
, sizeof ngc
->ourhook
, "pppoe-%ld", (long)getpid());
216 memcpy(ngc
->peerhook
, ngc
->ourhook
, sizeof ngc
->peerhook
);
218 if (NgSendMsg(cs
, ".:", NGM_GENERIC_COOKIE
,
219 NGM_CONNECT
, ngc
, sizeof *ngc
) < 0) {
220 perror("Cannot CONNECT PPPoE and socket nodes");
224 plen
= strlen(provider
);
226 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ plen
);
227 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
->peerhook
);
228 memcpy(data
->data
, provider
, plen
);
229 data
->data_len
= plen
;
231 spath
= (char *)alloca(strlen(ngc
->peerhook
) + 3);
233 strcpy(spath
+ 2, ngc
->ourhook
);
237 fprintf(stderr
, "Sending PPPOE_LISTEN to %s, provider %s\n",
240 fprintf(stderr
, "Sending PPPOE_LISTEN to %s\n", spath
);
243 if (NgSendMsg(cs
, spath
, NGM_PPPOE_COOKIE
, NGM_PPPOE_LISTEN
,
244 data
, sizeof *data
+ plen
) == -1) {
245 fprintf(stderr
, "%s: Cannot LISTEN on netgraph node: %s\n",
246 spath
, strerror(errno
));
254 Spawn(const char *prog
, const char *acname
, const char *provider
,
255 const char *exec
, struct ngm_connect ngc
, int cs
, int ds
, void *request
,
258 char msgbuf
[sizeof(struct ng_mesg
) + sizeof(struct ngpppoe_sts
)];
259 struct ng_mesg
*rep
= (struct ng_mesg
*)msgbuf
;
260 struct ngpppoe_sts
*sts
= (struct ngpppoe_sts
*)(msgbuf
+ sizeof *rep
);
261 struct ngpppoe_init_data
*data
;
262 char env
[sizeof(HISMACADDR
)+18], unknown
[14], sessionid
[5], *path
;
263 unsigned char *macaddr
;
267 switch ((ret
= fork())) {
269 syslog(LOG_ERR
, "fork: %m");
284 /* Create a new socket node */
286 syslog(LOG_INFO
, "Creating a new socket node");
288 if (NgMkSockNode(NULL
, &cs
, &ds
) == -1) {
289 syslog(LOG_ERR
, "Cannot create netgraph socket node: %m");
293 /* Connect the PPPoE node to our new socket node. */
294 snprintf(ngc
.ourhook
, sizeof ngc
.ourhook
, "exec-%ld", (long)getpid());
295 memcpy(ngc
.peerhook
, ngc
.ourhook
, sizeof ngc
.peerhook
);
298 syslog(LOG_INFO
, "Sending CONNECT from .:%s -> %s.%s",
299 ngc
.ourhook
, ngc
.path
, ngc
.peerhook
);
300 if (NgSendMsg(cs
, ".:", NGM_GENERIC_COOKIE
,
301 NGM_CONNECT
, &ngc
, sizeof ngc
) < 0) {
302 syslog(LOG_ERR
, "Cannot CONNECT PPPoE and socket nodes: %m");
307 * If we tell the socket node not to LINGER, it will go away when
308 * the last hook is removed.
311 syslog(LOG_INFO
, "Sending NGM_SOCK_CMD_NOLINGER to socket");
312 if (NgSendMsg(cs
, ".:", NGM_SOCKET_COOKIE
,
313 NGM_SOCK_CMD_NOLINGER
, NULL
, 0) < 0) {
314 syslog(LOG_ERR
, "Cannot send NGM_SOCK_CMD_NOLINGER: %m");
318 /* Put the PPPoE node into OFFER mode */
319 slen
= strlen(acname
);
320 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ slen
);
321 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
.ourhook
);
322 memcpy(data
->data
, acname
, slen
);
323 data
->data_len
= slen
;
325 path
= (char *)alloca(strlen(ngc
.ourhook
) + 3);
327 strcpy(path
+ 2, ngc
.ourhook
);
329 syslog(LOG_INFO
, "Offering to %s as access concentrator %s",
331 if (NgSendMsg(cs
, path
, NGM_PPPOE_COOKIE
, NGM_PPPOE_OFFER
,
332 data
, sizeof *data
+ slen
) == -1) {
333 syslog(LOG_INFO
, "%s: Cannot OFFER on netgraph node: %m", path
);
336 /* If we have a provider code, set it */
338 slen
= strlen(provider
);
339 data
= (struct ngpppoe_init_data
*)alloca(sizeof *data
+ slen
);
340 snprintf(data
->hook
, sizeof data
->hook
, "%s", ngc
.ourhook
);
341 memcpy(data
->data
, provider
, slen
);
342 data
->data_len
= slen
;
344 syslog(LOG_INFO
, "adding to %s as offered service %s",
346 if (NgSendMsg(cs
, path
, NGM_PPPOE_COOKIE
, NGM_PPPOE_SERVICE
,
347 data
, sizeof *data
+ slen
) == -1) {
348 syslog(LOG_INFO
, "%s: Cannot add service on netgraph node: %m", path
);
353 /* Put the peer's MAC address in the environment */
354 if (sz
>= sizeof(struct ether_header
)) {
355 macaddr
= ((struct ether_header
*)request
)->ether_shost
;
356 snprintf(env
, sizeof(env
), "%s=%x:%x:%x:%x:%x:%x", HISMACADDR
,
357 macaddr
[0], macaddr
[1], macaddr
[2], macaddr
[3], macaddr
[4],
359 if (putenv(env
) != 0)
360 syslog(LOG_INFO
, "putenv: cannot set %s: %m", env
);
363 /* And send our request data to the waiting node */
365 syslog(LOG_INFO
, "Sending original request to %s (%d bytes)", path
, sz
);
366 if (NgSendData(ds
, ngc
.ourhook
, request
, sz
) == -1) {
367 syslog(LOG_ERR
, "Cannot send original request to %s: %m", path
);
371 /* Then wait for a success indication */
374 syslog(LOG_INFO
, "Waiting for a SUCCESS reply %s", path
);
377 if ((ret
= NgRecvMsg(cs
, rep
, sizeof msgbuf
, NULL
)) < 0) {
378 syslog(LOG_ERR
, "%s: Cannot receive a message: %m", path
);
383 /* The socket has been closed */
384 syslog(LOG_INFO
, "%s: Client timed out", path
);
388 if (rep
->header
.version
!= NG_VERSION
) {
389 syslog(LOG_ERR
, "%ld: Unexpected netgraph version, expected %ld",
390 (long)rep
->header
.version
, (long)NG_VERSION
);
394 if (rep
->header
.typecookie
!= NGM_PPPOE_COOKIE
) {
395 syslog(LOG_INFO
, "%ld: Unexpected netgraph cookie, expected %ld",
396 (long)rep
->header
.typecookie
, (long)NGM_PPPOE_COOKIE
);
400 switch (rep
->header
.cmd
) {
401 case NGM_PPPOE_SET_FLAG
: msg
= "SET_FLAG"; break;
402 case NGM_PPPOE_CONNECT
: msg
= "CONNECT"; break;
403 case NGM_PPPOE_LISTEN
: msg
= "LISTEN"; break;
404 case NGM_PPPOE_OFFER
: msg
= "OFFER"; break;
405 case NGM_PPPOE_SUCCESS
: msg
= "SUCCESS"; break;
406 case NGM_PPPOE_FAIL
: msg
= "FAIL"; break;
407 case NGM_PPPOE_CLOSE
: msg
= "CLOSE"; break;
408 case NGM_PPPOE_GET_STATUS
: msg
= "GET_STATUS"; break;
409 case NGM_PPPOE_ACNAME
:
411 if (setenv("ACNAME", sts
->hook
, 1) != 0)
412 syslog(LOG_WARNING
, "setenv: cannot set ACNAME=%s: %m",
415 case NGM_PPPOE_SESSIONID
:
417 snprintf(sessionid
, sizeof sessionid
, "%04x", *(u_int16_t
*)sts
);
418 if (setenv("SESSIONID", sessionid
, 1) != 0)
419 syslog(LOG_WARNING
, "setenv: cannot set SESSIONID=%s: %m",
423 snprintf(unknown
, sizeof unknown
, "<%d>", (int)rep
->header
.cmd
);
428 switch (rep
->header
.cmd
) {
430 case NGM_PPPOE_CLOSE
:
431 syslog(LOG_ERR
, "Received NGM_PPPOE_%s (hook \"%s\")",
436 syslog(LOG_INFO
, "Received NGM_PPPOE_%s (hook \"%s\")", msg
, sts
->hook
);
437 } while (rep
->header
.cmd
!= NGM_PPPOE_SUCCESS
);
439 dup2(ds
, STDIN_FILENO
);
440 dup2(ds
, STDOUT_FILENO
);
445 syslog(LOG_INFO
, "Executing: %s", exec
);
446 execlp(_PATH_BSHELL
, _PATH_BSHELL
, "-c", exec
, (char *)NULL
);
447 syslog(LOG_ERR
, "execlp failed: %m");
454 syslog(LOG_ERR
, "Second fork failed: %m");
463 const char *module
[] = { "netgraph", "ng_socket", "ng_ether", "ng_pppoe" };
466 for (f
= 0; f
< sizeof module
/ sizeof *module
; f
++)
467 if (modfind(module
[f
]) == -1 && kldload(module
[f
]) == -1) {
468 fprintf(stderr
, "kldload: %s: %s\n", module
[f
], strerror(errno
));
477 nglog(const char *fmt
, ...)
482 snprintf(nfmt
, sizeof nfmt
, "%s: %s", fmt
, strerror(errno
));
484 vsyslog(LOG_INFO
, nfmt
, ap
);
489 nglogx(const char *fmt
, ...)
494 vsyslog(LOG_INFO
, fmt
, ap
);
499 main(int argc
, char *argv
[])
501 char hostname
[MAXHOSTNAMELEN
], *exec
, rhook
[NG_HOOKSIZ
];
502 unsigned char response
[1024];
503 const char *label
, *prog
, *provider
, *acname
;
504 struct ngm_connect ngc
;
505 struct sigaction act
;
506 int ch
, cs
, ds
, ret
, optF
, optd
, optn
, sz
, f
;
509 prog
= strrchr(argv
[0], '/');
510 prog
= prog
? prog
+ 1 : argv
[0];
516 optF
= optd
= optn
= 0;
518 while ((ch
= getopt(argc
, argv
, "FP:a:de:l:n:p:")) != -1) {
546 NgSetDebug(atoi(optarg
));
558 if (optind
>= argc
|| optind
+ 2 < argc
)
561 if (exec
!= NULL
&& label
!= NULL
)
568 fprintf(stderr
, "%s: Either a provider, a label or an exec command"
569 " must be given\n", prog
);
572 exec
= (char *)alloca(sizeof DEFAULT_EXEC_PREFIX
+ strlen(label
));
574 fprintf(stderr
, "%s: Cannot allocate %d bytes\n", prog
,
575 (int)(sizeof DEFAULT_EXEC_PREFIX
) + strlen(label
));
578 strcpy(exec
, DEFAULT_EXEC_PREFIX
);
579 strcpy(exec
+ sizeof DEFAULT_EXEC_PREFIX
- 1, label
);
582 if (acname
== NULL
) {
585 if (gethostname(hostname
, sizeof hostname
))
586 strcpy(hostname
, "localhost");
587 else if ((dot
= strchr(hostname
, '.')))
595 return EX_UNAVAILABLE
;
598 /* Create a socket node */
599 if (NgMkSockNode(NULL
, &cs
, &ds
) == -1) {
600 perror("Cannot create netgraph socket node");
604 /* Connect it up (and fill in `ngc') */
605 if ((ret
= ConfigureNode(prog
, argv
[optind
], provider
, cs
, ds
,
612 if (!optF
&& daemon(1, 0) == -1) {
620 if (pidfile
!= NULL
) {
623 if ((fp
= fopen(pidfile
, "w")) == NULL
) {
629 fprintf(fp
, "%d\n", (int)getpid());
634 openlog(prog
, LOG_PID
| (optF
? LOG_PERROR
: 0), LOG_DAEMON
);
636 NgSetErrLog(nglog
, nglogx
);
638 memset(&act
, '\0', sizeof act
);
639 act
.sa_handler
= Farewell
;
641 sigemptyset(&act
.sa_mask
);
642 sigaction(SIGHUP
, &act
, NULL
);
643 sigaction(SIGINT
, &act
, NULL
);
644 sigaction(SIGQUIT
, &act
, NULL
);
645 sigaction(SIGTERM
, &act
, NULL
);
647 while (!ReceivedSignal
) {
649 syslog(LOG_INFO
, "Listening as provider %s", provider
);
651 syslog(LOG_INFO
, "Listening");
653 switch (sz
= NgRecvData(ds
, response
, sizeof response
, rhook
)) {
655 syslog(LOG_INFO
, "NgRecvData: %m");
658 syslog(LOG_INFO
, "NgRecvData: socket closed");
664 ptr
= dbuf
= alloca(sz
* 2 + 1);
665 for (f
= 0; f
< sz
; f
++, ptr
+= 2)
666 sprintf(ptr
, "%02x", (u_char
)response
[f
]);
668 syslog(LOG_INFO
, "Got %d bytes of data: %s", sz
, dbuf
);
672 ret
= EX_UNAVAILABLE
;
675 Spawn(prog
, acname
, provider
, exec
, ngc
, cs
, ds
, response
, sz
, optd
);
681 if (ReceivedSignal
) {
682 syslog(LOG_INFO
, "Received signal %d, exiting", ReceivedSignal
);
684 signal(ReceivedSignal
, SIG_DFL
);
685 raise(ReceivedSignal
);
689 ret
= -ReceivedSignal
;