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