+ip6_ext_next_header (ip6_ext_header_t * ext_hdr)
+{
+ return (void *) ((u8 *) ext_hdr + ip6_ext_header_len (ext_hdr));
+}
+
+always_inline int
+vlib_object_within_buffer_data (vlib_main_t * vm, vlib_buffer_t * b,
+ void *obj, size_t len)
+{
+ u8 *o = obj;
+ if (o < b->data ||
+ o + len > b->data + vlib_buffer_get_default_data_size (vm))
+ return 0;
+ return 1;
+}
+
+/*
+ * find ipv6 extension header within ipv6 header within buffer b
+ *
+ * @param vm
+ * @param b buffer to limit search to
+ * @param ip6_header ipv6 header
+ * @param header_type extension header type to search for
+ * @param[out] prev_ext_header address of header preceding found header
+ */
+always_inline void *
+ip6_ext_header_find (vlib_main_t * vm, vlib_buffer_t * b,
+ ip6_header_t * ip6_header, u8 header_type,
+ ip6_ext_header_t ** prev_ext_header)
+{
+ ip6_ext_header_t *prev = NULL;
+ ip6_ext_header_t *result = NULL;
+ if ((ip6_header)->protocol == header_type)
+ {
+ result = (void *) (ip6_header + 1);
+ if (!vlib_object_within_buffer_data (vm, b, result,
+ ip6_ext_header_len (result)))
+ {
+ result = NULL;
+ }
+ }
+ else
+ {
+ result = NULL;
+ prev = (void *) (ip6_header + 1);
+ while (ip6_ext_hdr (prev->next_hdr) && prev->next_hdr != header_type)
+ {
+ prev = ip6_ext_next_header (prev);
+ if (!vlib_object_within_buffer_data (vm, b, prev,
+ ip6_ext_header_len (prev)))
+ {
+ prev = NULL;
+ break;
+ }
+ }
+ if (prev && (prev->next_hdr == header_type))
+ {
+ result = ip6_ext_next_header (prev);
+ if (!vlib_object_within_buffer_data (vm, b, result,
+ ip6_ext_header_len (result)))
+ {
+ result = NULL;
+ }
+ }
+ }
+ if (prev_ext_header)
+ {
+ *prev_ext_header = prev;
+ }
+ return result;
+}