+static struct udphdr *
+get_upperlayer(register u_char *bp, int *prot)
+{
+ register const u_char *ep;
+ struct ip6_hdr *ip6 = (struct ip6_hdr *)bp;
+ struct udphdr *uh;
+ struct ip6_hbh *hbh;
+ struct ip6_frag *fragh;
+ struct ah *ah;
+ int nh, hlen;
+
+ /* 'ep' points to the end of avaible data. */
+ ep = snapend;
+
+ if (TTEST(ip6->ip6_nxt) == 0)
+ return NULL;
+
+ nh = ip6->ip6_nxt;
+ hlen = sizeof(struct ip6_hdr);
+
+ while (bp < snapend) {
+ bp += hlen;
+
+ switch(nh) {
+ case IPPROTO_UDP:
+ case IPPROTO_TCP:
+ uh = (struct udphdr *)bp;
+ if (TTEST(uh->uh_dport)) {
+ *prot = nh;
+ return(uh);
+ }
+ else
+ return(NULL);
+ /* NOTREACHED */
+
+ case IPPROTO_HOPOPTS:
+ case IPPROTO_DSTOPTS:
+ case IPPROTO_ROUTING:
+ hbh = (struct ip6_hbh *)bp;
+ if (TTEST(hbh->ip6h_len) == 0)
+ return(NULL);
+ nh = hbh->ip6h_nxt;
+ hlen = (hbh->ip6h_len + 1) << 3;
+ break;
+
+ case IPPROTO_FRAGMENT: /* this should be odd, but try anyway */
+ fragh = (struct ip6_frag *)bp;
+ if (TTEST(fragh->ip6f_offlg) == 0)
+ return(NULL);
+ /* fragments with non-zero offset are meaningless */
+ if ((fragh->ip6f_offlg & IP6F_OFF_MASK) != 0)
+ return(NULL);
+ nh = fragh->ip6f_nxt;
+ hlen = sizeof(struct ip6_frag);
+ break;
+
+ case IPPROTO_AH:
+ ah = (struct ah *)bp;
+ if (TTEST(ah->ah_len) == 0)
+ return(NULL);
+ nh = ah->ah_nxt;
+ hlen = (ah->ah_len + 2) << 2;
+ break;
+
+ default: /* unknown or undecodable header */
+ *prot = nh; /* meaningless, but set here anyway */
+ return(NULL);
+ }
+ }
+
+ return(NULL); /* should be notreached, though */
+}
+