]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_pppoe.c
This commit was generated by cvs2svn to compensate for changes in r171366,
[FreeBSD/FreeBSD.git] / sys / netgraph / ng_pppoe.c
1 /*
2  * ng_pppoe.c
3  */
4
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  * 
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  * 
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Julian Elischer <julian@freebsd.org>
39  *
40  * $FreeBSD$
41  * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/ktr.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52 #include <net/ethernet.h>
53
54 #include <netgraph/ng_message.h>
55 #include <netgraph/netgraph.h>
56 #include <netgraph/ng_parse.h>
57 #include <netgraph/ng_pppoe.h>
58 #include <netgraph/ng_ether.h>
59
60 #ifdef NG_SEPARATE_MALLOC
61 MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
62 #else
63 #define M_NETGRAPH_PPPOE M_NETGRAPH
64 #endif
65
66 #define SIGNOFF "session closed"
67 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
68
69 /*
70  * This section contains the netgraph method declarations for the
71  * pppoe node. These methods define the netgraph pppoe 'type'.
72  */
73
74 static ng_constructor_t ng_pppoe_constructor;
75 static ng_rcvmsg_t      ng_pppoe_rcvmsg;
76 static ng_shutdown_t    ng_pppoe_shutdown;
77 static ng_newhook_t     ng_pppoe_newhook;
78 static ng_connect_t     ng_pppoe_connect;
79 static ng_rcvdata_t     ng_pppoe_rcvdata;
80 static ng_disconnect_t  ng_pppoe_disconnect;
81
82 /* Parse type for struct ngpppoe_init_data */
83 static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
84         = NG_PPPOE_INIT_DATA_TYPE_INFO;
85 static const struct ng_parse_type ngpppoe_init_data_state_type = {
86         &ng_parse_struct_type,
87         &ngpppoe_init_data_type_fields
88 };
89
90 /* Parse type for struct ngpppoe_sts */
91 static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
92         = NG_PPPOE_STS_TYPE_INFO;
93 static const struct ng_parse_type ng_pppoe_sts_state_type = {
94         &ng_parse_struct_type,
95         &ng_pppoe_sts_type_fields
96 };
97
98 /* List of commands and how to convert arguments to/from ASCII */
99 static const struct ng_cmdlist ng_pppoe_cmds[] = {
100         {
101           NGM_PPPOE_COOKIE,
102           NGM_PPPOE_CONNECT,
103           "pppoe_connect",
104           &ngpppoe_init_data_state_type,
105           NULL
106         },
107         {
108           NGM_PPPOE_COOKIE,
109           NGM_PPPOE_LISTEN,
110           "pppoe_listen",
111           &ngpppoe_init_data_state_type,
112           NULL
113         },
114         {
115           NGM_PPPOE_COOKIE,
116           NGM_PPPOE_OFFER,
117           "pppoe_offer",
118           &ngpppoe_init_data_state_type,
119           NULL
120         },
121         {
122           NGM_PPPOE_COOKIE,
123           NGM_PPPOE_SERVICE,
124           "pppoe_service",
125           &ngpppoe_init_data_state_type,
126           NULL
127         },
128         {
129           NGM_PPPOE_COOKIE,
130           NGM_PPPOE_SUCCESS,
131           "pppoe_success",
132           &ng_pppoe_sts_state_type,
133           NULL
134         },
135         {
136           NGM_PPPOE_COOKIE,
137           NGM_PPPOE_FAIL,
138           "pppoe_fail",
139           &ng_pppoe_sts_state_type,
140           NULL
141         },
142         {
143           NGM_PPPOE_COOKIE,
144           NGM_PPPOE_CLOSE,
145           "pppoe_close",
146           &ng_pppoe_sts_state_type,
147           NULL
148         },
149         {
150           NGM_PPPOE_COOKIE,
151           NGM_PPPOE_SETMODE,
152           "pppoe_setmode",
153           &ng_parse_string_type,
154           NULL
155         },
156         {
157           NGM_PPPOE_COOKIE,
158           NGM_PPPOE_GETMODE,
159           "pppoe_getmode",
160           NULL,
161           &ng_parse_string_type
162         },
163         {
164           NGM_PPPOE_COOKIE,
165           NGM_PPPOE_SETENADDR,
166           "setenaddr",
167           &ng_parse_enaddr_type,
168           NULL
169         },
170         { 0 }
171 };
172
173 /* Netgraph node type descriptor */
174 static struct ng_type typestruct = {
175         .version =      NG_ABI_VERSION,
176         .name =         NG_PPPOE_NODE_TYPE,
177         .constructor =  ng_pppoe_constructor,
178         .rcvmsg =       ng_pppoe_rcvmsg,
179         .shutdown =     ng_pppoe_shutdown,
180         .newhook =      ng_pppoe_newhook,
181         .connect =      ng_pppoe_connect,
182         .rcvdata =      ng_pppoe_rcvdata,
183         .disconnect =   ng_pppoe_disconnect,
184         .cmdlist =      ng_pppoe_cmds,
185 };
186 NETGRAPH_INIT(pppoe, &typestruct);
187
188 /*
189  * States for the session state machine.
190  * These have no meaning if there is no hook attached yet.
191  */
192 enum state {
193     PPPOE_SNONE=0,      /* [both] Initial state */
194     PPPOE_LISTENING,    /* [Daemon] Listening for discover initiation pkt */
195     PPPOE_SINIT,        /* [Client] Sent discovery initiation */
196     PPPOE_PRIMED,       /* [Server] Awaiting PADI from daemon */
197     PPPOE_SOFFER,       /* [Server] Sent offer message  (got PADI)*/
198     PPPOE_SREQ,         /* [Client] Sent a Request */
199     PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
200     PPPOE_CONNECTED,    /* [Both] Connection established, Data received */
201     PPPOE_DEAD          /* [Both] */
202 };
203
204 #define NUMTAGS 20 /* number of tags we are set up to work with */
205
206 /*
207  * Information we store for each hook on each node for negotiating the
208  * session. The mbuf and cluster are freed once negotiation has completed.
209  * The whole negotiation block is then discarded.
210  */
211
212 struct sess_neg {
213         struct mbuf             *m; /* holds cluster with last sent packet */
214         union   packet          *pkt; /* points within the above cluster */
215         struct callout          handle;   /* see timeout(9) */
216         u_int                   timeout; /* 0,1,2,4,8,16 etc. seconds */
217         u_int                   numtags;
218         const struct pppoe_tag  *tags[NUMTAGS];
219         u_int                   service_len;
220         u_int                   ac_name_len;
221
222         struct datatag          service;
223         struct datatag          ac_name;
224 };
225 typedef struct sess_neg *negp;
226
227 /*
228  * Session information that is needed after connection.
229  */
230 struct sess_con {
231         hook_p                  hook;
232         uint16_t                Session_ID;
233         enum state              state;
234         ng_ID_t                 creator;        /* who to notify */
235         struct pppoe_full_hdr   pkt_hdr;        /* used when connected */
236         negp                    neg;            /* used when negotiating */
237 };
238 typedef struct sess_con *sessp;
239
240 #define NG_PPPOE_SESSION_NODE(sp) NG_HOOK_NODE(sp->hook)
241
242 /*
243  * Information we store for each node
244  */
245 struct PPPoE {
246         node_p          node;           /* back pointer to node */
247         hook_p          ethernet_hook;
248         hook_p          debug_hook;
249         u_int           packets_in;     /* packets in from ethernet */
250         u_int           packets_out;    /* packets out towards ethernet */
251         uint32_t        flags;
252 #define COMPAT_3COM     0x00000001
253 #define COMPAT_DLINK    0x00000002
254         struct ether_header     eh;
255 };
256 typedef struct PPPoE *priv_p;
257
258 union uniq {
259         char bytes[sizeof(void *)];
260         void *pointer;
261 };
262
263 #define LEAVE(x) do { error = x; goto quit; } while(0)
264 static void     pppoe_start(sessp sp);
265 static void     ng_pppoe_sendpacket(sessp sp);
266 static void     pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
267 static const    struct pppoe_tag *scan_tags(sessp sp,
268                         const struct pppoe_hdr* ph);
269 static  int     pppoe_send_event(sessp sp, enum cmd cmdid);
270
271 /*************************************************************************
272  * Some basic utilities  from the Linux version with author's permission.*
273  * Author:      Michal Ostrowski <mostrows@styx.uwaterloo.ca>            *
274  ************************************************************************/
275
276 /*
277  * Generate a new session id
278  * XXX find out the FreeBSD locking scheme.
279  */
280 static uint16_t
281 get_new_sid(node_p node)
282 {
283         priv_p privp = NG_NODE_PRIVATE(node);
284         static int pppoe_sid = 10;
285         sessp sp;
286         hook_p  hook;
287         uint16_t val;
288
289 restart:
290         val = pppoe_sid++;
291         /*
292          * Spec says 0xFFFF is reserved.
293          * Also don't use 0x0000
294          */
295         if (val == 0xffff) {
296                 pppoe_sid = 20;
297                 goto restart;
298         }
299
300         /* Check it isn't already in use. */
301         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
302                 /* Don't check special hooks. */
303                 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
304                 ||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
305                         continue;
306                 sp = NG_HOOK_PRIVATE(hook);
307                 if (sp->Session_ID == val)
308                         goto restart;
309         }
310
311         CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
312
313         return (val);
314 }
315
316
317 /*
318  * Return the location where the next tag can be put
319  */
320 static __inline const struct pppoe_tag*
321 next_tag(const struct pppoe_hdr* ph)
322 {
323         return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
324             + ntohs(ph->length));
325 }
326
327 /*
328  * Look for a tag of a specific type.
329  * Don't trust any length the other end says,
330  * but assume we already sanity checked ph->length.
331  */
332 static const struct pppoe_tag*
333 get_tag(const struct pppoe_hdr* ph, uint16_t idx)
334 {
335         const char *const end = (const char *)next_tag(ph);
336         const struct pppoe_tag *pt = &ph->tag[0];
337         const char *ptn;
338
339         /*
340          * Keep processing tags while a tag header will still fit.
341          */
342         while((const char*)(pt + 1) <= end) {
343                 /*
344                  * If the tag data would go past the end of the packet, abort.
345                  */
346                 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
347                 if (ptn > end) {
348                         CTR2(KTR_NET, "%20s: invalid length for tag %d",
349                             __func__, idx);
350                         return (NULL);
351                 }
352                 if (pt->tag_type == idx) {
353                         CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
354                         return (pt);
355                 }
356
357                 pt = (const struct pppoe_tag*)ptn;
358         }
359
360         CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
361         return (NULL);
362 }
363
364 /**************************************************************************
365  * Inlines to initialise or add tags to a session's tag list.
366  **************************************************************************/
367 /*
368  * Initialise the session's tag list.
369  */
370 static void
371 init_tags(sessp sp)
372 {
373         KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
374         sp->neg->numtags = 0;
375 }
376
377 static void
378 insert_tag(sessp sp, const struct pppoe_tag *tp)
379 {
380         negp neg = sp->neg;
381         int i;
382
383         KASSERT(neg != NULL, ("%s: no neg", __func__));
384         if ((i = neg->numtags++) < NUMTAGS) {
385                 neg->tags[i] = tp;
386         } else {
387                 log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
388                     "packet\n");
389                 neg->numtags--;
390         }
391 }
392
393 /*
394  * Make up a packet, using the tags filled out for the session.
395  *
396  * Assume that the actual pppoe header and ethernet header
397  * are filled out externally to this routine.
398  * Also assume that neg->wh points to the correct
399  * location at the front of the buffer space.
400  */
401 static void
402 make_packet(sessp sp) {
403         struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
404         const struct pppoe_tag **tag;
405         char *dp;
406         int count;
407         int tlen;
408         uint16_t length = 0;
409
410         KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
411             ("%s: called from wrong state", __func__));
412         CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
413
414         dp = (char *)wh->ph.tag;
415         for (count = 0, tag = sp->neg->tags;
416             ((count < sp->neg->numtags) && (count < NUMTAGS));
417             tag++, count++) {
418                 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
419                 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
420                         log(LOG_NOTICE, "ng_pppoe: tags too long\n");
421                         sp->neg->numtags = count;
422                         break;  /* XXX chop off what's too long */
423                 }
424                 bcopy(*tag, (char *)dp, tlen);
425                 length += tlen;
426                 dp += tlen;
427         }
428         wh->ph.length = htons(length);
429         sp->neg->m->m_len = length + sizeof(*wh);
430         sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
431 }
432
433 /**************************************************************************
434  * Routines to match a service.                                           *
435  **************************************************************************/
436
437 /*
438  * Find a hook that has a service string that matches that
439  * we are seeking. For now use a simple string.
440  * In the future we may need something like regexp().
441  *
442  * Null string is a wildcard (ANY service), according to RFC2516.
443  * And historical FreeBSD wildcard is also "*".
444  */
445
446 static hook_p
447 pppoe_match_svc(node_p node, const struct pppoe_tag *tag)
448 {
449         priv_p privp = NG_NODE_PRIVATE(node);
450         hook_p hook;
451
452         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
453                 sessp sp = NG_HOOK_PRIVATE(hook);
454                 negp neg;
455
456                 /* Skip any hook that is debug or ethernet. */
457                 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook) ||
458                     (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
459                         continue;
460
461                 /* Skip any sessions which are not in LISTEN mode. */
462                 if (sp->state != PPPOE_LISTENING)
463                         continue;
464
465                 neg = sp->neg;
466
467                 /* Empty Service-Name matches any service. */
468                 if (neg->service_len == 0)
469                         break;
470
471                 /* Special case for a blank or "*" service name (wildcard). */
472                 if (neg->service_len == 1 && neg->service.data[0] == '*')
473                         break;
474
475                 /* If the lengths don't match, that aint it. */
476                 if (neg->service_len != ntohs(tag->tag_len))
477                         continue;
478
479                 if (strncmp(tag->tag_data, neg->service.data,
480                     ntohs(tag->tag_len)) == 0)
481                         break;
482         }
483         CTR3(KTR_NET, "%20s: matched %p for %s", __func__, hook, tag->tag_data);
484
485         return (hook);
486 }
487
488 /*
489  * Broadcast the PADI packet in m0 to all listening hooks.
490  * This routine is called when a PADI with empty Service-Name
491  * tag is received. Client should receive PADOs with all
492  * available services.
493  */
494 static int
495 pppoe_broadcast_padi(node_p node, struct mbuf *m0)
496 {
497         priv_p privp = NG_NODE_PRIVATE(node);
498         hook_p hook;
499         int error = 0;
500
501         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
502                 sessp sp = NG_HOOK_PRIVATE(hook);
503                 struct mbuf *m;
504
505                 /*
506                  * Go through all listening hooks and
507                  * broadcast the PADI packet up there
508                  */
509                 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook) ||
510                     (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
511                         continue;
512
513                 if (sp->state != PPPOE_LISTENING)
514                         continue;
515
516                 m = m_dup(m0, M_DONTWAIT);
517                 if (m == NULL)
518                         return (ENOMEM);
519                 NG_SEND_DATA_ONLY(error, hook, m);
520                 if (error)
521                         return (error);
522         }
523
524         return (0);
525 }
526
527 /*
528  * Find a hook, which name equals to given service.
529  */
530 static hook_p
531 pppoe_find_svc(node_p node, const char *svc_name, int svc_len)
532 {
533         priv_p privp = NG_NODE_PRIVATE(node);
534         hook_p  hook;
535
536         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
537                 sessp sp = NG_HOOK_PRIVATE(hook);
538                 negp neg;
539
540                 /* Skip any hook that is debug or ethernet. */
541                 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook) ||
542                     (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
543                         continue;
544
545                 /* Skip any sessions which are not in LISTEN mode. */
546                 if (sp->state != PPPOE_LISTENING)
547                         continue;
548
549                 neg = sp->neg;
550
551                 if (neg->service_len == svc_len &&
552                     strncmp(svc_name, neg->service.data, svc_len == 0))
553                         return (hook);
554         }
555
556         return (NULL);
557 }
558
559 /**************************************************************************
560  * Routine to find a particular session that matches an incoming packet.  *
561  **************************************************************************/
562 static hook_p
563 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh)
564 {
565         priv_p  privp = NG_NODE_PRIVATE(node);
566         sessp   sp = NULL;
567         hook_p  hook = NULL;
568         uint16_t session = ntohs(wh->ph.sid);
569
570         /*
571          * Find matching peer/session combination.
572          */
573         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
574                 /* don't check special hooks */
575                 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
576                 ||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook)) {
577                         continue;
578                 }
579                 sp = NG_HOOK_PRIVATE(hook);
580                 if ( ( (sp->state == PPPOE_CONNECTED)
581                     || (sp->state == PPPOE_NEWCONNECTED) )
582                 && (sp->Session_ID == session)
583                 && (bcmp(sp->pkt_hdr.eh.ether_dhost,
584                     wh->eh.ether_shost,
585                     ETHER_ADDR_LEN)) == 0) {
586                         break;
587                 }
588         }
589         CTR3(KTR_NET, "%20s: matched %p for %d", __func__, hook, session);
590
591         return (hook);
592 }
593
594 static hook_p
595 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
596 {
597         priv_p  privp = NG_NODE_PRIVATE(node);
598         hook_p  hook = NULL;
599         union uniq uniq;
600
601         bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
602         /* Cycle through all known hooks. */
603         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
604                 /* Don't check special hooks. */
605                 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
606                 ||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
607                         continue;
608                 if (uniq.pointer == NG_HOOK_PRIVATE(hook))
609                         break;
610         }
611         CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, uniq.pointer);
612
613         return (hook);
614 }
615
616 /**************************************************************************
617  * Start of Netgraph entrypoints.                                         *
618  **************************************************************************/
619
620 /*
621  * Allocate the private data structure and link it with node.
622  */
623 static int
624 ng_pppoe_constructor(node_p node)
625 {
626         priv_p privp;
627
628         /* Initialize private descriptor. */
629         privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
630         if (privp == NULL)
631                 return (ENOMEM);
632
633         /* Link structs together; this counts as our one reference to *node. */
634         NG_NODE_SET_PRIVATE(node, privp);
635         privp->node = node;
636
637         /* Initialize to standard mode. */
638         memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
639         privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
640
641         CTR3(KTR_NET, "%20s: created node [%x] (%p)",
642             __func__, node->nd_ID, node);
643
644         return (0);
645 }
646
647 /*
648  * Give our ok for a hook to be added...
649  * point the hook's private info to the hook structure.
650  *
651  * The following hook names are special:
652  *  "ethernet":  the hook that should be connected to a NIC.
653  *  "debug":    copies of data sent out here  (when I write the code).
654  * All other hook names need only be unique. (the framework checks this).
655  */
656 static int
657 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
658 {
659         const priv_p privp = NG_NODE_PRIVATE(node);
660         sessp sp;
661
662         if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
663                 privp->ethernet_hook = hook;
664                 NG_HOOK_SET_PRIVATE(hook, &privp->ethernet_hook);
665         } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
666                 privp->debug_hook = hook;
667                 NG_HOOK_SET_PRIVATE(hook, &privp->debug_hook);
668         } else {
669                 /*
670                  * Any other unique name is OK.
671                  * The infrastructure has already checked that it's unique,
672                  * so just allocate it and hook it in.
673                  */
674                 sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
675                 if (sp == NULL)
676                         return (ENOMEM);
677
678                 NG_HOOK_SET_PRIVATE(hook, sp);
679                 sp->hook = hook;
680         }
681         CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
682             __func__, node->nd_ID, node, name, hook);
683
684         return(0);
685 }
686
687 /*
688  * Hook has been added successfully. Request the MAC address of
689  * the underlying Ethernet node.
690  */
691 static int
692 ng_pppoe_connect(hook_p hook)
693 {
694         const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
695         struct ng_mesg *msg;
696         int error;
697
698         if (hook != privp->ethernet_hook)
699                 return (0);
700
701         /*
702          * If this is Ethernet hook, then request MAC address
703          * from our downstream.
704          */
705         NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
706         if (msg == NULL)
707                 return (ENOBUFS);
708
709         /*
710          * Our hook and peer hook have HK_INVALID flag set,
711          * so we can't use NG_SEND_MSG_HOOK() macro here.
712          */
713         NG_SEND_MSG_ID(error, privp->node, msg,
714             NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
715             NG_NODE_ID(privp->node));
716
717         return (error);
718 }
719 /*
720  * Get a netgraph control message.
721  * Check it is one we understand. If needed, send a response.
722  * We sometimes save the address for an async action later.
723  * Always free the message.
724  */
725 static int
726 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
727 {
728         priv_p privp = NG_NODE_PRIVATE(node);
729         struct ngpppoe_init_data *ourmsg = NULL;
730         struct ng_mesg *resp = NULL;
731         int error = 0;
732         hook_p hook = NULL;
733         sessp sp = NULL;
734         negp neg = NULL;
735         struct ng_mesg *msg;
736
737         NGI_GET_MSG(item, msg);
738         CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
739             __func__, node->nd_ID, node, msg->header.cmd,
740             msg->header.typecookie);
741
742         /* Deal with message according to cookie and command. */
743         switch (msg->header.typecookie) {
744         case NGM_PPPOE_COOKIE:
745                 switch (msg->header.cmd) {
746                 case NGM_PPPOE_CONNECT:
747                 case NGM_PPPOE_LISTEN:
748                 case NGM_PPPOE_OFFER:
749                 case NGM_PPPOE_SERVICE:
750                         ourmsg = (struct ngpppoe_init_data *)msg->data;
751                         if (msg->header.arglen < sizeof(*ourmsg)) {
752                                 log(LOG_ERR, "ng_pppoe[%x]: init data too "
753                                     "small\n", node->nd_ID);
754                                 LEAVE(EMSGSIZE);
755                         }
756                         if (msg->header.arglen - sizeof(*ourmsg) >
757                             PPPOE_SERVICE_NAME_SIZE) {
758                                 log(LOG_ERR, "ng_pppoe[%x]: service name "
759                                     "too big\n", node->nd_ID);
760                                 LEAVE(EMSGSIZE);
761                         }
762                         if (msg->header.arglen - sizeof(*ourmsg) <
763                             ourmsg->data_len) {
764                                 log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
765                                     "length, %d should be %zd\n", node->nd_ID,
766                                     ourmsg->data_len,
767                                     msg->header.arglen - sizeof (*ourmsg));
768                                 LEAVE(EMSGSIZE);
769                         }
770
771                         /* Make sure strcmp will terminate safely. */
772                         ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
773
774                         /* Cycle through all known hooks. */
775                         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
776                                 if (NG_HOOK_NAME(hook) &&
777                                     strcmp(NG_HOOK_NAME(hook), ourmsg->hook) ==
778                                     0)
779                                         break;
780                         }
781                         if (hook == NULL)
782                                 LEAVE(ENOENT);
783
784                         if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook) ||
785                             (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
786                                 LEAVE(EINVAL);
787
788                         sp = NG_HOOK_PRIVATE(hook);
789
790                         if (msg->header.cmd == NGM_PPPOE_LISTEN) {
791                                 /*
792                                  * Ensure we aren't already listening for this
793                                  * service.
794                                  */
795                                 if (pppoe_find_svc(node, ourmsg->data,
796                                     ourmsg->data_len) != NULL)
797                                         LEAVE(EEXIST);
798                         }
799
800                         /*
801                          * PPPOE_SERVICE advertisments are set up
802                          * on sessions that are in PRIMED state.
803                          */
804                         if (msg->header.cmd == NGM_PPPOE_SERVICE)
805                                 break;
806
807                         if (sp->state != PPPOE_SNONE) {
808                                 log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
809                                     "active\n", node->nd_ID);
810                                 LEAVE(EISCONN);
811                         }
812
813                         /*
814                          * Set up prototype header.
815                          */
816                         neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
817                             M_NOWAIT | M_ZERO);
818
819                         if (neg == NULL)
820                                 LEAVE(ENOMEM);
821
822                         neg->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
823                         if (neg->m == NULL) {
824                                 free(neg, M_NETGRAPH_PPPOE);
825                                 LEAVE(ENOBUFS);
826                         }
827                         neg->m->m_pkthdr.rcvif = NULL;
828                         sp->neg = neg;
829                         ng_callout_init(&neg->handle);
830                         neg->m->m_len = sizeof(struct pppoe_full_hdr);
831                         neg->pkt = mtod(neg->m, union packet*);
832                         memcpy((void *)&neg->pkt->pkt_header.eh,
833                             &privp->eh, sizeof(struct ether_header));
834                         neg->pkt->pkt_header.ph.ver = 0x1;
835                         neg->pkt->pkt_header.ph.type = 0x1;
836                         neg->pkt->pkt_header.ph.sid = 0x0000;
837                         neg->timeout = 0;
838
839                         sp->creator = NGI_RETADDR(item);
840                 }
841                 switch (msg->header.cmd) {
842                 case NGM_PPPOE_GET_STATUS:
843                     {
844                         struct ngpppoestat *stats;
845
846                         NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
847                         if (!resp)
848                                 LEAVE(ENOMEM);
849
850                         stats = (struct ngpppoestat *) resp->data;
851                         stats->packets_in = privp->packets_in;
852                         stats->packets_out = privp->packets_out;
853                         break;
854                     }
855                 case NGM_PPPOE_CONNECT:
856                         /*
857                          * Check the hook exists and is Uninitialised.
858                          * Send a PADI request, and start the timeout logic.
859                          * Store the originator of this message so we can send
860                          * a success of fail message to them later.
861                          * Move the session to SINIT.
862                          * Set up the session to the correct state and
863                          * start it.
864                          */
865                         neg->service.hdr.tag_type = PTT_SRV_NAME;
866                         neg->service.hdr.tag_len =
867                             htons((uint16_t)ourmsg->data_len);
868                         if (ourmsg->data_len)
869                                 bcopy(ourmsg->data, neg->service.data,
870                                     ourmsg->data_len);
871                         neg->service_len = ourmsg->data_len;
872                         pppoe_start(sp);
873                         break;
874                 case NGM_PPPOE_LISTEN:
875                         /*
876                          * Check the hook exists and is Uninitialised.
877                          * Install the service matching string.
878                          * Store the originator of this message so we can send
879                          * a success of fail message to them later.
880                          * Move the hook to 'LISTENING'
881                          */
882                         neg->service.hdr.tag_type = PTT_SRV_NAME;
883                         neg->service.hdr.tag_len =
884                             htons((uint16_t)ourmsg->data_len);
885
886                         if (ourmsg->data_len)
887                                 bcopy(ourmsg->data, neg->service.data,
888                                     ourmsg->data_len);
889                         neg->service_len = ourmsg->data_len;
890                         neg->pkt->pkt_header.ph.code = PADT_CODE;
891                         /*
892                          * Wait for PADI packet coming from Ethernet.
893                          */
894                         sp->state = PPPOE_LISTENING;
895                         break;
896                 case NGM_PPPOE_OFFER:
897                         /*
898                          * Check the hook exists and is Uninitialised.
899                          * Store the originator of this message so we can send
900                          * a success of fail message to them later.
901                          * Store the AC-Name given and go to PRIMED.
902                          */
903                         neg->ac_name.hdr.tag_type = PTT_AC_NAME;
904                         neg->ac_name.hdr.tag_len =
905                             htons((uint16_t)ourmsg->data_len);
906                         if (ourmsg->data_len)
907                                 bcopy(ourmsg->data, neg->ac_name.data,
908                                     ourmsg->data_len);
909                         neg->ac_name_len = ourmsg->data_len;
910                         neg->pkt->pkt_header.ph.code = PADO_CODE;
911                         /*
912                          * Wait for PADI packet coming from hook.
913                          */
914                         sp->state = PPPOE_PRIMED;
915                         break;
916                 case NGM_PPPOE_SERVICE:
917                         /*
918                          * Check the session is primed.
919                          * for now just allow ONE service to be advertised.
920                          * If you do it twice you just overwrite.
921                          */
922                         if (sp->state != PPPOE_PRIMED) {
923                                 log(LOG_NOTICE, "ng_pppoe[%x]: session not "
924                                     "primed\n", node->nd_ID);
925                                 LEAVE(EISCONN);
926                         }
927                         neg = sp->neg;
928                         neg->service.hdr.tag_type = PTT_SRV_NAME;
929                         neg->service.hdr.tag_len =
930                             htons((uint16_t)ourmsg->data_len);
931
932                         if (ourmsg->data_len)
933                                 bcopy(ourmsg->data, neg->service.data,
934                                     ourmsg->data_len);
935                         neg->service_len = ourmsg->data_len;
936                         break;
937                 case NGM_PPPOE_SETMODE:
938                     {
939                         char *s;
940                         size_t len;
941
942                         if (msg->header.arglen == 0)
943                                 LEAVE(EINVAL);
944
945                         s = (char *)msg->data;
946                         len = msg->header.arglen - 1;
947
948                         /* Search for matching mode string. */
949                         if (len == strlen(NG_PPPOE_STANDARD) &&
950                             (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
951                                 privp->flags = 0;
952                                 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
953                                 break;
954                         }
955                         if (len == strlen(NG_PPPOE_3COM) &&
956                             (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
957                                 privp->flags |= COMPAT_3COM;
958                                 privp->eh.ether_type =
959                                     ETHERTYPE_PPPOE_3COM_DISC;
960                                 break;
961                         }
962                         if (len == strlen(NG_PPPOE_DLINK) &&
963                             (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
964                                 privp->flags |= COMPAT_DLINK;
965                                 break;
966                         }
967                         error = EINVAL;
968                         break;
969                     }
970                 case NGM_PPPOE_GETMODE:
971                     {
972                         char *s;
973                         size_t len = 0;
974
975                         if (privp->flags == 0)
976                                 len += strlen(NG_PPPOE_STANDARD) + 1;
977                         if (privp->flags & COMPAT_3COM)
978                                 len += strlen(NG_PPPOE_3COM) + 1;
979                         if (privp->flags & COMPAT_DLINK)
980                                 len += strlen(NG_PPPOE_DLINK) + 1;
981
982                         NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
983                         if (resp == NULL)
984                                 LEAVE(ENOMEM);
985
986                         s = (char *)resp->data;
987                         if (privp->flags == 0) {
988                                 len = strlen(NG_PPPOE_STANDARD);
989                                 strlcpy(s, NG_PPPOE_STANDARD, len + 1);
990                                 break;
991                         }
992                         if (privp->flags & COMPAT_3COM) {
993                                 len = strlen(NG_PPPOE_3COM);
994                                 strlcpy(s, NG_PPPOE_3COM, len + 1);
995                                 s += len;
996                         }
997                         if (privp->flags & COMPAT_DLINK) {
998                                 if (s != resp->data)
999                                         *s++ = '|';
1000                                 len = strlen(NG_PPPOE_DLINK);
1001                                 strlcpy(s, NG_PPPOE_DLINK, len + 1);
1002                         }
1003                         break;
1004                     }
1005                 case NGM_PPPOE_SETENADDR:
1006                         if (msg->header.arglen != ETHER_ADDR_LEN)
1007                                 LEAVE(EINVAL);
1008                         bcopy(msg->data, &privp->eh.ether_shost,
1009                             ETHER_ADDR_LEN);
1010                         break;
1011                 default:
1012                         LEAVE(EINVAL);
1013                 }
1014                 break;
1015         case NGM_ETHER_COOKIE:
1016                 if (!(msg->header.flags & NGF_RESP))
1017                         LEAVE(EINVAL);
1018                 switch (msg->header.cmd) {
1019                 case NGM_ETHER_GET_ENADDR:
1020                         if (msg->header.arglen != ETHER_ADDR_LEN)
1021                                 LEAVE(EINVAL);
1022                         bcopy(msg->data, &privp->eh.ether_shost,
1023                             ETHER_ADDR_LEN);
1024                         break;
1025                 default:
1026                         LEAVE(EINVAL);
1027                 }
1028                 break;
1029         default:
1030                 LEAVE(EINVAL);
1031         }
1032
1033         /* Take care of synchronous response, if any. */
1034 quit:
1035         CTR2(KTR_NET, "%20s: returning %d", __func__, error);
1036         NG_RESPOND_MSG(error, node, item, resp);
1037         /* Free the message and return. */
1038         NG_FREE_MSG(msg);
1039         return(error);
1040 }
1041
1042 /*
1043  * Start a client into the first state. A separate function because
1044  * it can be needed if the negotiation times out.
1045  */
1046 static void
1047 pppoe_start(sessp sp)
1048 {
1049         priv_p  privp = NG_NODE_PRIVATE(NG_PPPOE_SESSION_NODE(sp));
1050         struct {
1051                 struct pppoe_tag hdr;
1052                 union   uniq    data;
1053         } __packed uniqtag;
1054
1055         /*
1056          * Kick the state machine into starting up.
1057          */
1058         CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1059         sp->state = PPPOE_SINIT;
1060         /*
1061          * Reset the packet header to broadcast. Since we are
1062          * in a client mode use configured ethertype.
1063          */
1064         memcpy((void *)&sp->neg->pkt->pkt_header.eh, &privp->eh,
1065             sizeof(struct ether_header));
1066         sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
1067         uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
1068         uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
1069         uniqtag.data.pointer = sp;
1070         init_tags(sp);
1071         insert_tag(sp, &uniqtag.hdr);
1072         insert_tag(sp, &sp->neg->service.hdr);
1073         make_packet(sp);
1074         ng_pppoe_sendpacket(sp);
1075 }
1076
1077 static int
1078 send_acname(sessp sp, const struct pppoe_tag *tag)
1079 {
1080         int error, tlen;
1081         struct ng_mesg *msg;
1082         struct ngpppoe_sts *sts;
1083
1084         CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1085
1086         NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
1087             sizeof(struct ngpppoe_sts), M_NOWAIT);
1088         if (msg == NULL)
1089                 return (ENOMEM);
1090
1091         sts = (struct ngpppoe_sts *)msg->data;
1092         tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
1093         strncpy(sts->hook, tag->tag_data, tlen);
1094         sts->hook[tlen] = '\0';
1095         NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1096
1097         return (error);
1098 }
1099
1100 static int
1101 send_sessionid(sessp sp)
1102 {
1103         int error;
1104         struct ng_mesg *msg;
1105
1106         CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1107
1108         NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1109             sizeof(uint16_t), M_NOWAIT);
1110         if (msg == NULL)
1111                 return (ENOMEM);
1112
1113         *(uint16_t *)msg->data = sp->Session_ID;
1114         NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1115
1116         return (error);
1117 }
1118
1119 /*
1120  * Receive data, and do something with it.
1121  * The caller will never free m, so if we use up this data
1122  * or abort we must free it.
1123  */
1124 static int
1125 ng_pppoe_rcvdata(hook_p hook, item_p item)
1126 {
1127         node_p                  node = NG_HOOK_NODE(hook);
1128         const priv_p            privp = NG_NODE_PRIVATE(node);
1129         sessp                   sp = NG_HOOK_PRIVATE(hook);
1130         const struct pppoe_tag  *utag = NULL, *tag = NULL;
1131         const struct pppoe_full_hdr *wh;
1132         const struct pppoe_hdr  *ph;
1133         negp                    neg = NULL;
1134         struct mbuf             *m;
1135         hook_p                  sendhook;
1136         int                     error = 0;
1137         uint16_t                session;
1138         uint16_t                length;
1139         uint8_t                 code;
1140         struct {
1141                 struct pppoe_tag hdr;
1142                 union   uniq    data;
1143         } __packed uniqtag;
1144
1145         CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1146             __func__, node->nd_ID, node, item, hook->hk_name, hook);
1147
1148         NGI_GET_M(item, m);
1149         if (NG_HOOK_PRIVATE(hook) == &privp->debug_hook) {
1150                 /*
1151                  * Data from the debug hook gets sent without modification
1152                  * straight to the ethernet.
1153                  */
1154                 NG_FWD_ITEM_HOOK( error, item, privp->ethernet_hook);
1155                 privp->packets_out++;
1156         } else if (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook) {
1157                 /*
1158                  * Incoming data.
1159                  * Dig out various fields from the packet.
1160                  * Use them to decide where to send it.
1161                  */
1162                 
1163                 privp->packets_in++;
1164                 if( m->m_len < sizeof(*wh)) {
1165                         m = m_pullup(m, sizeof(*wh)); /* Checks length */
1166                         if (m == NULL) {
1167                                 log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1168                                     "m_pullup(wh)\n", node->nd_ID);
1169                                 LEAVE(ENOBUFS);
1170                         }
1171                 }
1172                 wh = mtod(m, struct pppoe_full_hdr *);
1173                 length = ntohs(wh->ph.length);
1174                 switch(wh->eh.ether_type) {
1175                 case    ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1176                 case    ETHERTYPE_PPPOE_DISC:
1177                         /*
1178                          * We need to try to make sure that the tag area
1179                          * is contiguous, or we could wander off the end
1180                          * of a buffer and make a mess.
1181                          * (Linux wouldn't have this problem).
1182                          */
1183                         if (m->m_pkthdr.len <= MHLEN) {
1184                                 if( m->m_len < m->m_pkthdr.len) {
1185                                         m = m_pullup(m, m->m_pkthdr.len);
1186                                         if (m == NULL) {
1187                                                 log(LOG_NOTICE, "ng_pppoe[%x]: "
1188                                                     "couldn't "
1189                                                     "m_pullup(pkthdr)\n",
1190                                                     node->nd_ID);
1191                                                 LEAVE(ENOBUFS);
1192                                         }
1193                                 }
1194                         }
1195                         if (m->m_len != m->m_pkthdr.len) {
1196                                 /*
1197                                  * It's not all in one piece.
1198                                  * We need to do extra work.
1199                                  * Put it into a cluster.
1200                                  */
1201                                 struct mbuf *n;
1202                                 n = m_dup(m, M_DONTWAIT);
1203                                 m_freem(m);
1204                                 m = n;
1205                                 if (m) {
1206                                         /* just check we got a cluster */
1207                                         if (m->m_len != m->m_pkthdr.len) {
1208                                                 m_freem(m);
1209                                                 m = NULL;
1210                                         }
1211                                 }
1212                                 if (m == NULL) {
1213                                         log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1214                                             "fragmented\n", node->nd_ID);
1215                                         LEAVE(EMSGSIZE);
1216                                 }
1217                         }
1218                         wh = mtod(m, struct pppoe_full_hdr *);
1219                         length = ntohs(wh->ph.length);
1220                         ph = &wh->ph;
1221                         session = ntohs(wh->ph.sid);
1222                         code = wh->ph.code;
1223
1224                         switch(code) {
1225                         case    PADI_CODE:
1226                                 /*
1227                                  * We are a server:
1228                                  * Look for a hook with the required service
1229                                  * and send the ENTIRE packet up there.
1230                                  * It should come back to a new hook in
1231                                  * PRIMED state. Look there for further
1232                                  * processing.
1233                                  */
1234                                 tag = get_tag(ph, PTT_SRV_NAME);
1235                                 if (tag == NULL) {
1236                                         CTR1(KTR_NET,
1237                                             "%20s: PADI w/o Service-Name",
1238                                             __func__);
1239                                         LEAVE(ENETUNREACH);
1240                                 }
1241
1242                                 /*
1243                                  * First, try to match Service-Name
1244                                  * against our listening hooks. If
1245                                  * no success and we are in D-Link
1246                                  * compat mode and Service-Name is
1247                                  * empty, then we broadcast the PADI
1248                                  * to all listening hooks.
1249                                  */
1250                                 sendhook = pppoe_match_svc(node, tag);
1251                                 if (sendhook != NULL)
1252                                         NG_FWD_NEW_DATA(error, item,
1253                                             sendhook, m);
1254                                 else if (privp->flags & COMPAT_DLINK &&
1255                                          ntohs(tag->tag_len) == 0)
1256                                         error = pppoe_broadcast_padi(node, m);
1257                                 else
1258                                         error = ENETUNREACH;
1259                                 break;
1260                         case    PADO_CODE:
1261                                 /*
1262                                  * We are a client:
1263                                  * Use the host_uniq tag to find the
1264                                  * hook this is in response to.
1265                                  * Received #2, now send #3
1266                                  * For now simply accept the first we receive.
1267                                  */
1268                                 utag = get_tag(ph, PTT_HOST_UNIQ);
1269                                 if ((utag == NULL)
1270                                 || (ntohs(utag->tag_len) != sizeof(sp))) {
1271                                         log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1272                                             "unique field\n", node->nd_ID);
1273                                         LEAVE(ENETUNREACH);
1274                                 }
1275
1276                                 sendhook = pppoe_finduniq(node, utag);
1277                                 if (sendhook == NULL) {
1278                                         log(LOG_NOTICE, "ng_pppoe[%x]: no "
1279                                             "matching session\n", node->nd_ID);
1280                                         LEAVE(ENETUNREACH);
1281                                 }
1282
1283                                 /*
1284                                  * Check the session is in the right state.
1285                                  * It needs to be in PPPOE_SINIT.
1286                                  */
1287                                 sp = NG_HOOK_PRIVATE(sendhook);
1288                                 if (sp->state != PPPOE_SINIT) {
1289                                         log(LOG_NOTICE, "ng_pppoe[%x]: session "
1290                                             "in wrong state\n", node->nd_ID);
1291                                         LEAVE(ENETUNREACH);
1292                                 }
1293                                 neg = sp->neg;
1294                                 ng_uncallout(&neg->handle, node);
1295
1296                                 /*
1297                                  * This is the first time we hear
1298                                  * from the server, so note it's
1299                                  * unicast address, replacing the
1300                                  * broadcast address .
1301                                  */
1302                                 bcopy(wh->eh.ether_shost,
1303                                         neg->pkt->pkt_header.eh.ether_dhost,
1304                                         ETHER_ADDR_LEN);
1305                                 neg->timeout = 0;
1306                                 neg->pkt->pkt_header.ph.code = PADR_CODE;
1307                                 init_tags(sp);
1308                                 insert_tag(sp, utag);      /* Host Unique */
1309                                 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1310                                         insert_tag(sp, tag); /* return cookie */
1311                                 if ((tag = get_tag(ph, PTT_AC_NAME))) { 
1312                                         insert_tag(sp, tag); /* return it */
1313                                         send_acname(sp, tag);
1314                                 }
1315                                 insert_tag(sp, &neg->service.hdr); /* Service */
1316                                 scan_tags(sp, ph);
1317                                 make_packet(sp);
1318                                 sp->state = PPPOE_SREQ;
1319                                 ng_pppoe_sendpacket(sp);
1320                                 break;
1321                         case    PADR_CODE:
1322
1323                                 /*
1324                                  * We are a server:
1325                                  * Use the ac_cookie tag to find the
1326                                  * hook this is in response to.
1327                                  */
1328                                 utag = get_tag(ph, PTT_AC_COOKIE);
1329                                 if ((utag == NULL)
1330                                 || (ntohs(utag->tag_len) != sizeof(sp))) {
1331                                         LEAVE(ENETUNREACH);
1332                                 }
1333
1334                                 sendhook = pppoe_finduniq(node, utag);
1335                                 if (sendhook == NULL) {
1336                                         LEAVE(ENETUNREACH);
1337                                 }
1338
1339                                 /*
1340                                  * Check the session is in the right state.
1341                                  * It needs to be in PPPOE_SOFFER
1342                                  * or PPPOE_NEWCONNECTED. If the latter,
1343                                  * then this is a retry by the client.
1344                                  * so be nice, and resend.
1345                                  */
1346                                 sp = NG_HOOK_PRIVATE(sendhook);
1347                                 if (sp->state == PPPOE_NEWCONNECTED) {
1348                                         /*
1349                                          * Whoa! drop back to resend that
1350                                          * PADS packet.
1351                                          * We should still have a copy of it.
1352                                          */
1353                                         sp->state = PPPOE_SOFFER;
1354                                 }
1355                                 if (sp->state != PPPOE_SOFFER) {
1356                                         LEAVE (ENETUNREACH);
1357                                         break;
1358                                 }
1359                                 neg = sp->neg;
1360                                 ng_uncallout(&neg->handle, node);
1361                                 neg->pkt->pkt_header.ph.code = PADS_CODE;
1362                                 if (sp->Session_ID == 0)
1363                                         neg->pkt->pkt_header.ph.sid =
1364                                             htons(sp->Session_ID
1365                                                 = get_new_sid(node));
1366                                 send_sessionid(sp);
1367                                 neg->timeout = 0;
1368                                 /*
1369                                  * start working out the tags to respond with.
1370                                  */
1371                                 init_tags(sp);
1372                                 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1373                                 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1374                                         insert_tag(sp, tag);/* return service */
1375                                 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1376                                         insert_tag(sp, tag); /* return it */
1377                                 insert_tag(sp, utag);   /* ac_cookie */
1378                                 scan_tags(sp, ph);
1379                                 make_packet(sp);
1380                                 sp->state = PPPOE_NEWCONNECTED;
1381                                 ng_pppoe_sendpacket(sp);
1382                                 /*
1383                                  * Having sent the last Negotiation header,
1384                                  * Set up the stored packet header to
1385                                  * be correct for the actual session.
1386                                  * But keep the negotialtion stuff
1387                                  * around in case we need to resend this last
1388                                  * packet. We'll discard it when we move
1389                                  * from NEWCONNECTED to CONNECTED
1390                                  */
1391                                 sp->pkt_hdr = neg->pkt->pkt_header;
1392                                 /* Configure ethertype depending on what
1393                                  * ethertype was used at discovery phase */
1394                                 if (sp->pkt_hdr.eh.ether_type ==
1395                                     ETHERTYPE_PPPOE_3COM_DISC)
1396                                         sp->pkt_hdr.eh.ether_type
1397                                                 = ETHERTYPE_PPPOE_3COM_SESS;
1398                                 else
1399                                         sp->pkt_hdr.eh.ether_type
1400                                                 = ETHERTYPE_PPPOE_SESS;
1401                                 sp->pkt_hdr.ph.code = 0;
1402                                 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1403                                 break;
1404                         case    PADS_CODE:
1405                                 /*
1406                                  * We are a client:
1407                                  * Use the host_uniq tag to find the
1408                                  * hook this is in response to.
1409                                  * take the session ID and store it away.
1410                                  * Also make sure the pre-made header is
1411                                  * correct and set us into Session mode.
1412                                  */
1413                                 utag = get_tag(ph, PTT_HOST_UNIQ);
1414                                 if ((utag == NULL)
1415                                 || (ntohs(utag->tag_len) != sizeof(sp))) {
1416                                         LEAVE (ENETUNREACH);
1417                                         break;
1418                                 }
1419                                 sendhook = pppoe_finduniq(node, utag);
1420                                 if (sendhook == NULL) {
1421                                         LEAVE(ENETUNREACH);
1422                                 }
1423
1424                                 /*
1425                                  * Check the session is in the right state.
1426                                  * It needs to be in PPPOE_SREQ.
1427                                  */
1428                                 sp = NG_HOOK_PRIVATE(sendhook);
1429                                 if (sp->state != PPPOE_SREQ) {
1430                                         LEAVE(ENETUNREACH);
1431                                 }
1432                                 neg = sp->neg;
1433                                 ng_uncallout(&neg->handle, node);
1434                                 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1435                                 sp->Session_ID = ntohs(wh->ph.sid);
1436                                 send_sessionid(sp);
1437                                 neg->timeout = 0;
1438                                 sp->state = PPPOE_CONNECTED;
1439                                 /*
1440                                  * Now we have gone to Connected mode,
1441                                  * Free all resources needed for
1442                                  * negotiation.
1443                                  * Keep a copy of the header we will be using.
1444                                  */
1445                                 sp->pkt_hdr = neg->pkt->pkt_header;
1446                                 if (privp->flags & COMPAT_3COM)
1447                                         sp->pkt_hdr.eh.ether_type
1448                                                 = ETHERTYPE_PPPOE_3COM_SESS;
1449                                 else
1450                                         sp->pkt_hdr.eh.ether_type
1451                                                 = ETHERTYPE_PPPOE_SESS;
1452                                 sp->pkt_hdr.ph.code = 0;
1453                                 m_freem(neg->m);
1454                                 free(sp->neg, M_NETGRAPH_PPPOE);
1455                                 sp->neg = NULL;
1456                                 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1457                                 break;
1458                         case    PADT_CODE:
1459                                 /*
1460                                  * Send a 'close' message to the controlling
1461                                  * process (the one that set us up);
1462                                  * And then tear everything down.
1463                                  *
1464                                  * Find matching peer/session combination.
1465                                  */
1466                                 sendhook = pppoe_findsession(node, wh);
1467                                 if (sendhook == NULL) {
1468                                         LEAVE(ENETUNREACH);
1469                                 }
1470                                 /* send message to creator */
1471                                 /* close hook */
1472                                 if (sendhook) {
1473                                         ng_rmhook_self(sendhook);
1474                                 }
1475                                 break;
1476                         default:
1477                                 LEAVE(EPFNOSUPPORT);
1478                         }
1479                         break;
1480                 case    ETHERTYPE_PPPOE_3COM_SESS:
1481                 case    ETHERTYPE_PPPOE_SESS:
1482                         /*
1483                          * Find matching peer/session combination.
1484                          */
1485                         sendhook = pppoe_findsession(node, wh);
1486                         if (sendhook == NULL) {
1487                                 LEAVE (ENETUNREACH);
1488                                 break;
1489                         }
1490                         sp = NG_HOOK_PRIVATE(sendhook);
1491                         m_adj(m, sizeof(*wh));
1492                         if (m->m_pkthdr.len < length) {
1493                                 /* Packet too short, dump it */
1494                                 LEAVE(EMSGSIZE);
1495                         }
1496
1497                         /* Also need to trim excess at the end */
1498                         if (m->m_pkthdr.len > length) {
1499                                 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1500                         }
1501                         if ( sp->state != PPPOE_CONNECTED) {
1502                                 if (sp->state == PPPOE_NEWCONNECTED) {
1503                                         sp->state = PPPOE_CONNECTED;
1504                                         /*
1505                                          * Now we have gone to Connected mode,
1506                                          * Free all resources needed for
1507                                          * negotiation. Be paranoid about
1508                                          * whether there may be a timeout.
1509                                          */
1510                                         m_freem(sp->neg->m);
1511                                         ng_uncallout(&sp->neg->handle, node);
1512                                         free(sp->neg, M_NETGRAPH_PPPOE);
1513                                         sp->neg = NULL;
1514                                 } else {
1515                                         LEAVE (ENETUNREACH);
1516                                         break;
1517                                 }
1518                         }
1519                         NG_FWD_NEW_DATA( error, item, sendhook, m);
1520                         break;
1521                 default:
1522                         LEAVE(EPFNOSUPPORT);
1523                 }
1524         } else {
1525                 /*
1526                  * Not ethernet or debug hook..
1527                  *
1528                  * The packet has come in on a normal hook.
1529                  * We need to find out what kind of hook,
1530                  * So we can decide how to handle it.
1531                  * Check the hook's state.
1532                  */
1533                 sp = NG_HOOK_PRIVATE(hook);
1534                 switch (sp->state) {
1535                 case    PPPOE_NEWCONNECTED:
1536                 case    PPPOE_CONNECTED: {
1537                         static const u_char addrctrl[] = { 0xff, 0x03 };
1538                         struct pppoe_full_hdr *wh;
1539
1540                         /*
1541                          * Remove PPP address and control fields, if any.
1542                          * For example, ng_ppp(4) always sends LCP packets
1543                          * with address and control fields as required by
1544                          * generic PPP. PPPoE is an exception to the rule.
1545                          */
1546                         if (m->m_pkthdr.len >= 2) {
1547                                 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1548                                         LEAVE(ENOBUFS);
1549                                 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1550                                         m_adj(m, 2);
1551                         }
1552                         /*
1553                          * Bang in a pre-made header, and set the length up
1554                          * to be correct. Then send it to the ethernet driver.
1555                          * But first correct the length.
1556                          */
1557                         sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1558                         M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1559                         if (m == NULL)
1560                                 LEAVE(ENOBUFS);
1561
1562                         wh = mtod(m, struct pppoe_full_hdr *);
1563                         bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1564                         NG_FWD_NEW_DATA( error, item, privp->ethernet_hook, m);
1565                         privp->packets_out++;
1566                         break;
1567                         }
1568                 case    PPPOE_PRIMED:
1569                         /*
1570                          * A PADI packet is being returned by the application
1571                          * that has set up this hook. This indicates that it
1572                          * wants us to offer service.
1573                          */
1574                         neg = sp->neg;
1575                         if (m->m_len < sizeof(*wh)) {
1576                                 m = m_pullup(m, sizeof(*wh));
1577                                 if (m == NULL)
1578                                         LEAVE(ENOBUFS);
1579                         }
1580                         wh = mtod(m, struct pppoe_full_hdr *);
1581                         ph = &wh->ph;
1582                         session = ntohs(wh->ph.sid);
1583                         length = ntohs(wh->ph.length);
1584                         code = wh->ph.code;
1585                         /* Use peers mode in session. */
1586                         neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1587                         if (code != PADI_CODE)
1588                                 LEAVE(EINVAL);
1589                         ng_uncallout(&neg->handle, node);
1590
1591                         /*
1592                          * This is the first time we hear
1593                          * from the client, so note it's
1594                          * unicast address, replacing the
1595                          * broadcast address.
1596                          */
1597                         bcopy(wh->eh.ether_shost,
1598                                 neg->pkt->pkt_header.eh.ether_dhost,
1599                                 ETHER_ADDR_LEN);
1600                         sp->state = PPPOE_SOFFER;
1601                         neg->timeout = 0;
1602                         neg->pkt->pkt_header.ph.code = PADO_CODE;
1603
1604                         /*
1605                          * Start working out the tags to respond with.
1606                          */
1607                         uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1608                         uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1609                         uniqtag.data.pointer = sp;
1610                         init_tags(sp);
1611                         insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1612                         if ((tag = get_tag(ph, PTT_SRV_NAME)))
1613                                 insert_tag(sp, tag);      /* return service */
1614                         /*
1615                          * If we have a NULL service request
1616                          * and have an extra service defined in this hook,
1617                          * then also add a tag for the extra service.
1618                          * XXX this is a hack. eventually we should be able
1619                          * to support advertising many services, not just one
1620                          */
1621                         if (((tag == NULL) || (tag->tag_len == 0)) &&
1622                             (neg->service.hdr.tag_len != 0)) {
1623                                 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1624                         }
1625                         if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1626                                 insert_tag(sp, tag); /* returned hostunique */
1627                         insert_tag(sp, &uniqtag.hdr);
1628                         scan_tags(sp, ph);
1629                         make_packet(sp);
1630                         ng_pppoe_sendpacket(sp);
1631                         break;
1632
1633                 /*
1634                  * Packets coming from the hook make no sense
1635                  * to sessions in these states. Throw them away.
1636                  */
1637                 case    PPPOE_SINIT:
1638                 case    PPPOE_SREQ:
1639                 case    PPPOE_SOFFER:
1640                 case    PPPOE_SNONE:
1641                 case    PPPOE_LISTENING:
1642                 case    PPPOE_DEAD:
1643                 default:
1644                         LEAVE(ENETUNREACH);
1645                 }
1646         }
1647 quit:
1648         if (item)
1649                 NG_FREE_ITEM(item);
1650         NG_FREE_M(m);
1651         return error;
1652 }
1653
1654 /*
1655  * Do local shutdown processing..
1656  * If we are a persistant device, we might refuse to go away, and
1657  * we'd only remove our links and reset ourself.
1658  */
1659 static int
1660 ng_pppoe_shutdown(node_p node)
1661 {
1662         const priv_p privdata = NG_NODE_PRIVATE(node);
1663
1664         NG_NODE_SET_PRIVATE(node, NULL);
1665         NG_NODE_UNREF(privdata->node);
1666         free(privdata, M_NETGRAPH_PPPOE);
1667         return (0);
1668 }
1669
1670 /*
1671  * Hook disconnection
1672  *
1673  * Clean up all dangling links and information about the session/hook.
1674  * For this type, removal of the last link destroys the node.
1675  */
1676 static int
1677 ng_pppoe_disconnect(hook_p hook)
1678 {
1679         node_p node = NG_HOOK_NODE(hook);
1680         priv_p privp = NG_NODE_PRIVATE(node);
1681         sessp   sp;
1682         int     hooks;
1683
1684         hooks = NG_NODE_NUMHOOKS(node); /* This one already not counted. */
1685         if (NG_HOOK_PRIVATE(hook) == &privp->debug_hook) {
1686                 privp->debug_hook = NULL;
1687         } else if (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook) {
1688                 privp->ethernet_hook = NULL;
1689                 if (NG_NODE_IS_VALID(node))
1690                         ng_rmnode_self(node);
1691         } else {
1692                 sp = NG_HOOK_PRIVATE(hook);
1693                 if (sp->state != PPPOE_SNONE ) {
1694                         pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1695                 }
1696                 /*
1697                  * According to the spec, if we are connected,
1698                  * we should send a DISC packet if we are shutting down
1699                  * a session.
1700                  */
1701                 if ((privp->ethernet_hook)
1702                 && ((sp->state == PPPOE_CONNECTED)
1703                  || (sp->state == PPPOE_NEWCONNECTED))) {
1704                         struct mbuf *m;
1705                         struct pppoe_full_hdr *wh;
1706                         struct pppoe_tag *tag;
1707                         int     msglen = strlen(SIGNOFF);
1708                         int error = 0;
1709
1710                         /* Revert the stored header to DISC/PADT mode. */
1711                         wh = &sp->pkt_hdr;
1712                         wh->ph.code = PADT_CODE;
1713                         /*
1714                          * Configure ethertype depending on what was used
1715                          * during sessions stage.
1716                          */
1717                         if (sp->pkt_hdr.eh.ether_type ==
1718                             ETHERTYPE_PPPOE_3COM_SESS)
1719                                 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1720                         else
1721                                 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1722
1723                         /* Generate a packet of that type. */
1724                         MGETHDR(m, M_DONTWAIT, MT_DATA);
1725                         if (m == NULL)
1726                                 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1727                                     "mbufs\n", node->nd_ID);
1728                         else {
1729                                 m->m_pkthdr.rcvif = NULL;
1730                                 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1731                                 bcopy((caddr_t)wh, mtod(m, caddr_t),
1732                                     sizeof(*wh));
1733                                 /*
1734                                  * Add a General error message and adjust
1735                                  * sizes.
1736                                  */
1737                                 wh = mtod(m, struct pppoe_full_hdr *);
1738                                 tag = wh->ph.tag;
1739                                 tag->tag_type = PTT_GEN_ERR;
1740                                 tag->tag_len = htons((u_int16_t)msglen);
1741                                 strncpy(tag->tag_data, SIGNOFF, msglen);
1742                                 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1743                                     msglen);
1744                                 wh->ph.length = htons(sizeof(*tag) + msglen);
1745                                 NG_SEND_DATA_ONLY(error,
1746                                         privp->ethernet_hook, m);
1747                         }
1748                 }
1749                 /*
1750                  * As long as we have somewhere to store the timeout handle,
1751                  * we may have a timeout pending.. get rid of it.
1752                  */
1753                 if (sp->neg) {
1754                         ng_uncallout(&sp->neg->handle, node);
1755                         if (sp->neg->m)
1756                                 m_freem(sp->neg->m);
1757                         free(sp->neg, M_NETGRAPH_PPPOE);
1758                 }
1759                 free(sp, M_NETGRAPH_PPPOE);
1760                 NG_HOOK_SET_PRIVATE(hook, NULL);
1761
1762                 /*
1763                  * Work out how many session hooks there are.
1764                  * Node goes away on last session hook removal.
1765                  */
1766                 if (privp->ethernet_hook)
1767                         hooks -= 1;
1768                 if (privp->debug_hook)
1769                         hooks -= 1;
1770         }
1771         if ((NG_NODE_NUMHOOKS(node) == 0) &&
1772             (NG_NODE_IS_VALID(node)))
1773                 ng_rmnode_self(node);
1774         return (0);
1775 }
1776
1777 /*
1778  * Timeouts come here.
1779  */
1780 static void
1781 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
1782 {
1783         priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1784         sessp   sp = NG_HOOK_PRIVATE(hook);
1785         negp    neg = sp->neg;
1786         struct mbuf *m0 = NULL;
1787         int     error = 0;
1788
1789         CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
1790             __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
1791         switch(sp->state) {
1792                 /*
1793                  * Resend the last packet, using an exponential backoff.
1794                  * After a period of time, stop growing the backoff,
1795                  * And either leave it, or revert to the start.
1796                  */
1797         case    PPPOE_SINIT:
1798         case    PPPOE_SREQ:
1799                 /* Timeouts on these produce resends. */
1800                 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1801                 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1802                 ng_callout(&neg->handle, node, hook, neg->timeout * hz,
1803                     pppoe_ticker, NULL, 0);
1804                 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1805                         if (sp->state == PPPOE_SREQ) {
1806                                 /* Revert to SINIT mode. */
1807                                 pppoe_start(sp);
1808                         } else {
1809                                 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1810                         }
1811                 }
1812                 break;
1813         case    PPPOE_PRIMED:
1814         case    PPPOE_SOFFER:
1815                 /* A timeout on these says "give up" */
1816                 ng_rmhook_self(hook);
1817                 break;
1818         default:
1819                 /* Timeouts have no meaning in other states. */
1820                 log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
1821                     node->nd_ID);
1822         }
1823 }
1824
1825
1826 static void
1827 ng_pppoe_sendpacket(sessp sp)
1828 {
1829         struct  mbuf *m0 = NULL;
1830         hook_p  hook = sp->hook;
1831         node_p  node = NG_HOOK_NODE(hook);
1832         priv_p  privp = NG_NODE_PRIVATE(node);
1833         negp    neg = sp->neg;
1834         int     error = 0;
1835
1836         CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1837         switch(sp->state) {
1838         case    PPPOE_LISTENING:
1839         case    PPPOE_DEAD:
1840         case    PPPOE_SNONE:
1841         case    PPPOE_CONNECTED:
1842                 log(LOG_NOTICE, "%s: unexpected state %d\n",
1843                     __func__, sp->state);
1844                 break;
1845
1846         case    PPPOE_NEWCONNECTED:
1847                 /* Send the PADS without a timeout - we're now connected. */
1848                 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1849                 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1850                 break;
1851
1852         case    PPPOE_PRIMED:
1853                 /* No packet to send, but set up the timeout. */
1854                 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1855                     pppoe_ticker, NULL, 0);
1856                 break;
1857
1858         case    PPPOE_SOFFER:
1859                 /*
1860                  * Send the offer but if they don't respond
1861                  * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1862                  */
1863                 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1864                 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1865                 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1866                     pppoe_ticker, NULL, 0);
1867                 break;
1868
1869         case    PPPOE_SINIT:
1870         case    PPPOE_SREQ:
1871                 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1872                 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1873                 ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1874                     pppoe_ticker, NULL, 0);
1875                 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1876                 break;
1877
1878         default:
1879                 error = EINVAL;
1880                 log(LOG_NOTICE, "%s: bad state %d\n", __func__, sp->state);
1881         }
1882 }
1883
1884 /*
1885  * Parse an incoming packet to see if any tags should be copied to the
1886  * output packet. Don't do any tags that have been handled in the main
1887  * state machine.
1888  */
1889 static const struct pppoe_tag*
1890 scan_tags(sessp sp, const struct pppoe_hdr* ph)
1891 {
1892         const char *const end = (const char *)next_tag(ph);
1893         const char *ptn;
1894         const struct pppoe_tag *pt = &ph->tag[0];
1895
1896         /*
1897          * Keep processing tags while a tag header will still fit.
1898          */
1899         CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1900
1901         while((const char*)(pt + 1) <= end) {
1902                 /*
1903                  * If the tag data would go past the end of the packet, abort.
1904                  */
1905                 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1906                 if(ptn > end)
1907                         return NULL;
1908
1909                 switch (pt->tag_type) {
1910                 case    PTT_RELAY_SID:
1911                         insert_tag(sp, pt);
1912                         break;
1913                 case    PTT_EOL:
1914                         return NULL;
1915                 case    PTT_SRV_NAME:
1916                 case    PTT_AC_NAME:
1917                 case    PTT_HOST_UNIQ:
1918                 case    PTT_AC_COOKIE:
1919                 case    PTT_VENDOR:
1920                 case    PTT_SRV_ERR:
1921                 case    PTT_SYS_ERR:
1922                 case    PTT_GEN_ERR:
1923                         break;
1924                 }
1925                 pt = (const struct pppoe_tag*)ptn;
1926         }
1927         return NULL;
1928 }
1929         
1930 static  int
1931 pppoe_send_event(sessp sp, enum cmd cmdid)
1932 {
1933         int error;
1934         struct ng_mesg *msg;
1935         struct ngpppoe_sts *sts;
1936
1937         CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1938
1939         NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1940                         sizeof(struct ngpppoe_sts), M_NOWAIT);
1941         if (msg == NULL)
1942                 return (ENOMEM);
1943         sts = (struct ngpppoe_sts *)msg->data;
1944         strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1945         NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1946         return (error);
1947 }