summaryrefslogtreecommitdiffstats
path: root/src/vnet/ip/ip_interface.c
blob: d5ee7fd9b2b59ea4c70aefaf6649f3a7df1f7240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/ip/ip.h>

/**
 * @file
 * @brief IP prefix management on interfaces
 */

u32
ip_interface_address_find (ip_lookup_main_t * lm,
			   void *addr_fib, u32 address_length)
{
  uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib);

  if (p)
    return (p[0]);

  return (~0);
}

clib_error_t *
ip_interface_address_add (ip_lookup_main_t * lm,
			  u32 sw_if_index,
			  void *addr_fib,
			  u32 address_length, u32 * result_if_address_index)
{
  vnet_main_t *vnm = vnet_get_main ();
  ip_interface_address_t *a, *prev;
  u32 pi;			/* previous index */
  u32 ai;
  u32 hi;			/* head index */

  /* Verify given length. */
  if ((address_length == 0) ||
      (lm->is_ip6 && address_length > 128) ||
      (!lm->is_ip6 && address_length > 32))
    {
      vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
      return clib_error_create
	("%U wrong length for interface %U",
	 lm->format_address_and_length, addr_fib,
	 address_length, format_vnet_sw_if_index_name, vnm, sw_if_index);
    }

  vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
			   sw_if_index, ~0);

  pool_get_zero (lm->if_address_pool, a);

  ai = a - lm->if_address_pool;
  hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index];

  prev = 0;
  while (pi != (u32) ~ 0)
    {
      prev = pool_elt_at_index (lm->if_address_pool, pi);
      pi = prev->next_this_sw_interface;
    }
  pi = prev ? prev - lm->if_address_pool : (u32) ~ 0;

  a->address_key = mhash_set (&lm->address_to_if_address_index,
			      addr_fib, ai, /* old_value */ 0);
  a->address_length = address_length;
  a->sw_if_index = sw_if_index;
  a->flags = 0;
  a->prev_this_sw_interface = pi;
  a->next_this_sw_interface = ~0;
  if (prev)
    prev->next_this_sw_interface = ai;

  lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
    (hi != ~0) ? hi : ai;

  *result_if_address_index = ai;

  return (NULL);
}

clib_error_t *
ip_interface_address_del (ip_lookup_main_t * lm,
			  vnet_main_t * vnm,
			  u32 address_index, void *addr_fib,
			  u32 address_length, u32 sw_if_index)
{
  ip_interface_address_t *a, *prev, *next;

  a = pool_elt_at_index (lm->if_address_pool, address_index);

  if (a->sw_if_index != sw_if_index)
    {
      vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
      return clib_error_create ("%U not found for interface %U",
				lm->format_address_and_length,
				addr_fib, address_length,
				format_vnet_sw_if_index_name,
				vnet_get_main (), sw_if_index);
    }

  if (a->prev_this_sw_interface != ~0)
    {
      prev = pool_elt_at_index (lm->if_address_pool,
				a->prev_this_sw_interface);
      prev->next_this_sw_interface = a->next_this_sw_interface;
    }
  if (a->next_this_sw_interface != ~0)
    {
      next = pool_elt_at_index (lm->if_address_pool,
				a->next_this_sw_interface);
      next->prev_this_sw_interface = a->prev_this_sw_interface;

      if (a->prev_this_sw_interface == ~0)
	lm->if_address_pool_index_by_sw_if_index[a->sw_if_index] =
	  a->next_this_sw_interface;
    }

  if ((a->next_this_sw_interface == ~0) && (a->prev_this_sw_interface == ~0))
    lm->if_address_pool_index_by_sw_if_index[a->sw_if_index] = ~0;

  mhash_unset (&lm->address_to_if_address_index, addr_fib,
	       /* old_value */ 0);
  pool_put (lm->if_address_pool, a);
  return NULL;
}

u8
ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4)
{
  ip_interface_address_t *ia = 0;

  if (is_ip4)
    {
      ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
      ip4_address_t *ip4;
      /* *INDENT-OFF* */
      foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
      ({
        ip4 = ip_interface_address_get_address (lm4, ia);
        if (ip4_address_compare (ip4, &ip->ip4) == 0)
          return 1;
      }));
      /* *INDENT-ON* */
    }
  else
    {
      ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
      ip6_address_t *ip6;
      /* *INDENT-OFF* */
      foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
      ({
        ip6 = ip_interface_address_get_address (lm6, ia);
        if (ip6_address_compare (ip6, &ip->ip6) == 0)
          return 1;
      }));
      /* *INDENT-ON* */
    }
  return 0;
}

void *
ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
{
  ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
  ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
  ip_interface_address_t *ia = 0;

  if (is_ip4)
    {
      /* *INDENT-OFF* */
      foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
      ({
        return ip_interface_address_get_address (lm4, ia);
      }));
      /* *INDENT-ON* */
    }
  else
    {
      /* *INDENT-OFF* */
      foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
      ({
        ip6_address_t *rv;
        rv = ip_interface_address_get_address (lm6, ia);
        /* Trying to use a link-local ip6 src address is a fool's errand */
        if (!ip6_address_is_link_local_unicast (rv))
          return rv;
      }));
      /* *INDENT-ON* */
    }

  return 0;
}

walk_rc_t
ip_interface_address_mark_one_interface (vnet_main_t *vnm,
					 vnet_sw_interface_t *si, void *ctx)
{
  ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
  ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
  ip_interface_address_t *ia = 0;

  /* *INDENT-OFF* */
  foreach_ip_interface_address (lm4, ia, si->sw_if_index, 1 /* unnumbered */ ,
  ({
    ia->flags |= IP_INTERFACE_ADDRESS_FLAG_STALE;
  }));
  foreach_ip_interface_address (lm6, ia, si->sw_if_index, 1 /* unnumbered */ ,
  ({
    ia->flags |= IP_INTERFACE_ADDRESS_FLAG_STALE;
  }));
  /* *INDENT-ON* */

  return (WALK_CONTINUE);
}

void
ip_interface_address_mark (void)
{
  vnet_sw_interface_walk (vnet_get_main (),
			  ip_interface_address_mark_one_interface, NULL);
}

static walk_rc_t
ip_interface_address_sweep_one_interface (vnet_main_t * vnm,
					  vnet_sw_interface_t * si, void *ctx)
{
  vlib_main_t *vm = vlib_get_main ();
  ip4_address_t *ip4_addrs = 0;
  ip6_address_t *ip6_addrs = 0;
  ip4_main_t *im4 = &ip4_main;
  ip6_main_t *im6 = &ip6_main;
  ip_interface_address_t *ia;
  u32 *ip6_masks = 0;
  u32 *ip4_masks = 0;
  int i;

  /* *INDENT-OFF* */
  foreach_ip_interface_address (&im4->lookup_main, ia, si->sw_if_index, 1,
  ({
    if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE)
      {
        ip4_address_t * x = (ip4_address_t *)
          ip_interface_address_get_address (&im4->lookup_main, ia);
        vec_add1 (ip4_addrs, x[0]);
        vec_add1 (ip4_masks, ia->address_length);
      }
  }));

  foreach_ip_interface_address (&im6->lookup_main, ia, si->sw_if_index, 1,
  ({
    if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE)
      {
        ip6_address_t * x = (ip6_address_t *)
          ip_interface_address_get_address (&im6->lookup_main, ia);
        vec_add1 (ip6_addrs, x[0]);
        vec_add1 (ip6_masks, ia->address_length);
      }
  }));
  /* *INDENT-ON* */

  for (i = 0; i < vec_len (ip4_addrs); i++)
    ip4_add_del_interface_address (vm, si->sw_if_index, &ip4_addrs[i],
				   ip4_masks[i], 1 /* is_del */ );
  for (i = 0; i < vec_len (ip6_addrs); i++)
    ip6_add_del_interface_address (vm, si->sw_if_index, &ip6_addrs[i],
				   ip6_masks[i], 1 /* is_del */ );

  vec_free (ip4_addrs);
  vec_free (ip4_masks);
  vec_free (ip6_addrs);
  vec_free (ip6_masks);

  return (WALK_CONTINUE);
}

void
ip_interface_address_sweep (void)
{
  vnet_sw_interface_walk (vnet_get_main (),
			  ip_interface_address_sweep_one_interface, NULL);
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
0' href='#n1610'>1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
/*
 * Copyright (c) 2019 Arrcus Inc and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vppinfra/error.h>
#include <vppinfra/hash.h>
#include <srv6-mobile/mobile.h>

extern ip6_address_t sr_pr_encaps_src;

typedef struct
{
  ip6_address_t src, dst;
  ip6_address_t sr_prefix;
  u16 sr_prefixlen;
  u32 teid;
} srv6_end_rewrite_trace_t;

static u16 srh_tagfield[256] = {
  /* 0 */
  0x0,
  /* 1 : Echo Request */
  0x0004,
  /* 2 : Echo Reply */
  0x0008,
  /* 3 - 7 */
  0x0, 0x0, 0x0, 0x0, 0x0,
  /* 8 - 15 */
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  /* 16 - 23 */
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  /* 24 - 25 */
  0x0, 0x0,
  /* 26 : Error Indication */
  0x0002,
  /* 27 - 31 */
  0x0, 0x0, 0x0, 0x0, 0x0,
  /* 32 - 247 */
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  /* 248 - 253 */
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  /* 254 : End Maker */
  0x0001,
  /* 255 : G_PDU */
  0x0
};

static u8 *
format_srv6_end_rewrite_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  srv6_end_rewrite_trace_t *t = va_arg (*args, srv6_end_rewrite_trace_t *);

  return format (s, "SRv6-END-rewrite: src %U dst %U\n\tTEID: 0x%x",
		 format_ip4_address, &t->src, format_ip4_address, &t->dst,
		 clib_net_to_host_u32 (t->teid));
}

static u8 *
format_srv6_end_rewrite_trace6 (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  srv6_end_rewrite_trace_t *t = va_arg (*args, srv6_end_rewrite_trace_t *);

  return format (s,
		 "SRv6-END-rewrite: src %U dst %U\n\tTEID: 0x%x\n\tsr_prefix: %U/%d",
		 format_ip6_address, &t->src, format_ip6_address, &t->dst,
		 clib_net_to_host_u32 (t->teid), format_ip6_address,
		 &t->sr_prefix, t->sr_prefixlen);
}

#define foreach_srv6_end_v4_error \
  _(M_GTP4_E_PACKETS, "srv6 End.M.GTP4.E packets") \
  _(M_GTP4_E_BAD_PACKETS, "srv6 End.M.GTP4.E bad packets")

#define foreach_srv6_t_v4_d_error \
  _(M_GTP4_D_PACKETS, "srv6 T.M.GTP4.D packets") \
  _(M_GTP4_D_BAD_PACKETS, "srv6 T.M.GTP4.D bad packets")

#define foreach_srv6_end_v6_e_error \
  _(M_GTP6_E_PACKETS, "srv6 End.M.GTP6.E packets") \
  _(M_GTP6_E_BAD_PACKETS, "srv6 End.M.GTP6.E bad packets")

#define foreach_srv6_end_v6_d_error \
  _(M_GTP6_D_PACKETS, "srv6 End.M.GTP6.D packets") \
  _(M_GTP6_D_BAD_PACKETS, "srv6 End.M.GTP6.D bad packets")

#define foreach_srv6_end_v6_d_di_error \
  _(M_GTP6_D_DI_PACKETS, "srv6 End.M.GTP6.D.DI packets") \
  _(M_GTP6_D_DI_BAD_PACKETS, "srv6 End.M.GTP6.D.DI bad packets")

#define foreach_srv6_end_v6_dt_error \
  _(M_GTP6_DT_PACKETS, "srv6 End.M.GTP6.DT packets") \
  _(M_GTP6_DT_BAD_PACKETS, "srv6 End.M.GTP6.DT bad packets")

#define foreach_srv6_t_v4_dt_error \
  _(M_GTP4_DT_PACKETS, "srv6 T.M.GTP4.DT packets") \
  _(M_GTP4_DT_BAD_PACKETS, "srv6 T.M.GTP4.DT bad packets")

typedef enum
{
#define _(sym,str) SRV6_END_ERROR_##sym,
  foreach_srv6_end_v4_error
#undef _
    SRV6_END_N_V4_ERROR,
} srv6_end_error_v4_t;

typedef enum
{
#define _(sym,str) SRV6_T_ERROR_##sym,
  foreach_srv6_t_v4_d_error
#undef _
    SRV6_T_N_V4_D_ERROR,
} srv6_t_error_v4_d_t;

typedef enum
{
#define _(sym,str) SRV6_END_ERROR_##sym,
  foreach_srv6_end_v6_e_error
#undef _
    SRV6_END_N_V6_E_ERROR,
} srv6_end_error_v6_e_t;

typedef enum
{
#define _(sym,str) SRV6_END_ERROR_##sym,
  foreach_srv6_end_v6_d_error
#undef _
    SRV6_END_N_V6_D_ERROR,
} srv6_end_error_v6_d_t;

typedef enum
{
#define _(sym,str) SRV6_END_ERROR_##sym,
  foreach_srv6_end_v6_d_di_error
#undef _
    SRV6_END_N_V6_D_DI_ERROR,
} srv6_end_error_v6_d_di_t;

typedef enum
{
#define _(sym,str) SRV6_END_ERROR_##sym,
  foreach_srv6_end_v6_dt_error
#undef _
    SRV6_END_N_V6_DT_ERROR,
} srv6_end_error_v6_dt_t;

typedef enum
{
#define _(sym,str) SRV6_T_ERROR_##sym,
  foreach_srv6_t_v4_dt_error
#undef _
    SRV6_T_N_V4_DT_ERROR,
} srv6_t_error_v4_dt_t;

static char *srv6_end_error_v4_strings[] = {
#define _(sym,string) string,
  foreach_srv6_end_v4_error
#undef _
};

static char *srv6_t_error_v4_d_strings[] = {
#define _(sym,string) string,
  foreach_srv6_t_v4_d_error
#undef _
};

static char *srv6_end_error_v6_e_strings[] = {
#define _(sym,string) string,
  foreach_srv6_end_v6_e_error
#undef _
};

static char *srv6_end_error_v6_d_strings[] = {
#define _(sym,string) string,
  foreach_srv6_end_v6_d_error
#undef _
};

static char *srv6_end_error_v6_d_di_strings[] = {
#define _(sym,string) string,
  foreach_srv6_end_v6_d_di_error
#undef _
};

static char *srv6_end_error_v6_dt_strings[] = {
#define _(sym,string) string,
  foreach_srv6_end_v6_dt_error
#undef _
};

static char *srv6_t_error_v4_dt_strings[] = {
#define _(sym,string) string,
  foreach_srv6_t_v4_dt_error
#undef _
};

typedef enum
{
  SRV6_END_M_GTP4_E_NEXT_DROP,
  SRV6_END_M_GTP4_E_NEXT_LOOKUP,
  SRV6_END_M_GTP4_E_N_NEXT,
} srv6_end_m_gtp4_e_next_t;

typedef enum
{
  SRV6_T_M_GTP4_D_NEXT_DROP,
  SRV6_T_M_GTP4_D_NEXT_LOOKUP,
  SRV6_T_M_GTP4_D_N_NEXT,
} srv6_T_m_gtp4_d_next_t;

typedef enum
{
  SRV6_END_M_GTP6_E_NEXT_DROP,
  SRV6_END_M_GTP6_E_NEXT_LOOKUP,
  SRV6_END_M_GTP6_E_N_NEXT,
} srv6_end_m_gtp6_e_next_t;

typedef enum
{
  SRV6_END_M_GTP6_D_NEXT_DROP,
  SRV6_END_M_GTP6_D_NEXT_LOOKUP,
  SRV6_END_M_GTP6_D_N_NEXT,
} srv6_end_m_gtp6_d_next_t;

typedef enum
{
  SRV6_END_M_GTP6_D_DI_NEXT_DROP,
  SRV6_END_M_GTP6_D_DI_NEXT_LOOKUP,
  SRV6_END_M_GTP6_D_DI_N_NEXT,
} srv6_end_m_gtp6_d_di_next_t;

typedef enum
{
  SRV6_END_M_GTP6_DT_NEXT_DROP,
  SRV6_END_M_GTP6_DT_NEXT_LOOKUP4,
  SRV6_END_M_GTP6_DT_NEXT_LOOKUP6,
  SRV6_END_M_GTP6_DT_N_NEXT,
} srv6_end_m_gtp6_dt_next_t;

typedef enum
{
  SRV6_T_M_GTP4_DT_NEXT_DROP,
  SRV6_T_M_GTP4_DT_NEXT_LOOKUP4,
  SRV6_T_M_GTP4_DT_NEXT_LOOKUP6,
  SRV6_T_M_GTP4_DT_N_NEXT,
} srv6_t_m_gtp4_dt_next_t;

static inline u16
hash_uword_to_u16 (uword * key)
{
  u16 *val;
  val = (u16 *) key;
#if uword_bits == 64
  return val[0] ^ val[1] ^ val[2] ^ val[3];
#else
  return val[0] ^ val[1];
#endif
}

static inline u8
gtpu_type_get (u16 tag)
{
  u16 val;

  val = clib_net_to_host_u16 (tag);
  if (val & SRH_TAG_ECHO_REPLY)
    return GTPU_TYPE_ECHO_REPLY;
  else if (val & SRH_TAG_ECHO_REQUEST)
    return GTPU_TYPE_ECHO_REQUEST;
  else if (val & SRH_TAG_ERROR_INDICATION)
    return GTPU_TYPE_ERROR_INDICATION;
  else if (val & SRH_TAG_END_MARKER)
    return GTPU_TYPE_END_MARKER;

  return GTPU_TYPE_GTPU;
}

// Function for SRv6 GTP4.E function.
VLIB_NODE_FN (srv6_end_m_gtp4_e) (vlib_main_t * vm,
				  vlib_node_runtime_t * node,
				  vlib_frame_t * frame)
{
  srv6_end_main_v4_t *sm = &srv6_end_main_v4;
  ip6_sr_main_t *sm2 = &sr_main;
  u32 n_left_from, next_index, *from, *to_next;
  u32 thread_index = vm->thread_index;

  u32 good_n = 0, bad_n = 0;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  ip6_sr_localsid_t *ls0;
	  srv6_end_gtp4_param_t *ls_param;

	  ip6srv_combo_header_t *ip6srv0;
	  ip6_address_t src0, dst0;

	  ip4_gtpu_header_t *hdr0 = NULL;
	  uword len0;

	  u32 next0 = SRV6_END_M_GTP4_E_NEXT_LOOKUP;

	  // defaults
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  ls0 =
	    pool_elt_at_index (sm2->localsids,
			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);

	  ls_param = (srv6_end_gtp4_param_t *) ls0->plugin_mem;

	  ip6srv0 = vlib_buffer_get_current (b0);
	  src0 = ip6srv0->ip.src_address;
	  dst0 = ip6srv0->ip.dst_address;

	  len0 = vlib_buffer_length_in_chain (vm, b0);

	  if ((ip6srv0->ip.protocol == IPPROTO_IPV6_ROUTE
	       && len0 <
	       sizeof (ip6srv_combo_header_t) + ip6srv0->sr.length * 8)
	      || (len0 < sizeof (ip6_header_t)))
	    {
	      next0 = SRV6_END_M_GTP4_E_NEXT_DROP;

	      bad_n++;
	    }
	  else
	    {
	      u8 gtpu_type = 0;
	      u16 tag = 0;
	      u32 teid = 0;
	      u8 *teid8p = (u8 *) & teid;
	      u8 qfi = 0;
	      u16 seq = 0;
	      u32 index;
	      u32 offset, shift;
	      u32 hdrlen = 0;
	      uword key;
	      u16 port;
	      ip4_address_t dst4;
	      u16 ie_size = 0;
	      u8 ie_buf[GTPU_IE_MAX_SIZ];
	      void *p;
	      uword plen;

	      if (ip6srv0->ip.protocol == IPPROTO_IPV6_ROUTE)
		{
		  tag = ip6srv0->sr.tag;
		}

	      offset = ls0->localsid_prefix_len / 8;
	      shift = ls0->localsid_prefix_len % 8;

	      gtpu_type = gtpu_type_get (tag);

	      if (PREDICT_TRUE (shift == 0))
		{
		  clib_memcpy_fast (&dst4.as_u8[0], &dst0.as_u8[offset], 4);

		  qfi = dst0.as_u8[offset + 4];

		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      clib_memcpy_fast (&seq, &dst0.as_u8[offset + 5], 2);
		    }
		  else
		    {
		      clib_memcpy_fast (teid8p, &dst0.as_u8[offset + 5], 4);
		    }
		}
	      else
		{
		  u8 *sp;

		  for (index = 0; index < 4; index++)
		    {
		      dst4.as_u8[index] = dst0.as_u8[offset + index] << shift;
		      dst4.as_u8[index] |=
			dst0.as_u8[offset + index + 1] >> (8 - shift);
		    }

		  qfi |= dst0.as_u8[offset + 4] << shift;
		  qfi |= dst0.as_u8[offset + 5] >> (8 - shift);

		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      sp = (u8 *) & seq;
		      for (index = 0; index < 2; index++)
			{
			  sp[index] = dst0.as_u8[offset + 5 + index] << shift;
			  sp[index] |=
			    dst0.as_u8[offset + 6 + index] >> (8 - shift);
			}
		    }
		  else
		    {
		      for (index = 0; index < 4; index++)
			{
			  *teid8p = dst0.as_u8[offset + 5 + index] << shift;
			  *teid8p |=
			    dst0.as_u8[offset + 6 + index] >> (8 - shift);
			  teid8p++;
			}
		    }
		}

	      if (qfi)
		{
		  hdrlen =
		    sizeof (gtpu_exthdr_t) + sizeof (gtpu_pdu_session_t);
		}
	      else if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		       || gtpu_type == GTPU_TYPE_ECHO_REPLY
		       || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		{
		  hdrlen = sizeof (gtpu_exthdr_t);
		}

	      if (PREDICT_FALSE (gtpu_type == GTPU_TYPE_ECHO_REPLY))
		{
		  hdrlen += sizeof (gtpu_recovery_ie);
		}

	      if (PREDICT_FALSE (gtpu_type == GTPU_TYPE_ERROR_INDICATION))
		{
		  ip6_sr_tlv_t *tlv;
		  u16 ext_len;

		  ext_len = ip6srv0->sr.length * 8;

		  if (ext_len >
		      sizeof (ip6_address_t) * (ip6srv0->sr.last_entry + 1))
		    {
		      tlv =
			(ip6_sr_tlv_t *) ((u8 *) & ip6srv0->sr +
					  sizeof (ip6_sr_header_t) +
					  sizeof (ip6_address_t) *
					  (ip6srv0->sr.last_entry + 1));

		      if (tlv->type == SRH_TLV_USER_PLANE_CONTAINER)
			{
			  user_plane_sub_tlv_t *sub_tlv;

			  sub_tlv = (user_plane_sub_tlv_t *) tlv->value;

			  ie_size = sub_tlv->length;
			  clib_memcpy_fast (ie_buf, sub_tlv->value, ie_size);

			  hdrlen += ie_size;
			}
		    }
		}

	      if (ip6srv0->ip.protocol == IPPROTO_IPV6_ROUTE)
		{
		  vlib_buffer_advance (b0,
				       (word) sizeof (ip6srv_combo_header_t) +
				       ip6srv0->sr.length * 8);
		}
	      else
		{
		  vlib_buffer_advance (b0, (word) sizeof (ip6_header_t));
		}

	      // get length of encapsulated IPv6 packet (the remaining part)
	      p = vlib_buffer_get_current (b0);

	      plen = len0 = vlib_buffer_length_in_chain (vm, b0);

	      len0 += hdrlen;

	      hdrlen += sizeof (ip4_gtpu_header_t);

	      // IPv4 GTP-U header creation.
	      vlib_buffer_advance (b0, -(word) hdrlen);

	      hdr0 = vlib_buffer_get_current (b0);

	      clib_memcpy_fast (hdr0, &sm->cache_hdr,
				sizeof (ip4_gtpu_header_t));

	      hdr0->ip4.dst_address.as_u32 = dst4.as_u32;

	      hdr0->gtpu.teid = teid;
	      hdr0->gtpu.length = clib_host_to_net_u16 (len0);

	      hdr0->gtpu.type = gtpu_type;

	      if (qfi)
		{
		  u8 type = 0;
		  gtpu_pdu_session_t *sess;

		  hdr0->gtpu.ver_flags |= GTPU_EXTHDR_FLAG;

		  hdr0->gtpu.ext->seq = 0;

		  hdr0->gtpu.ext->npdu_num = 0;
		  hdr0->gtpu.ext->nextexthdr = GTPU_EXTHDR_PDU_SESSION;

		  type = qfi & SRV6_PDU_SESSION_U_BIT_MASK;

		  qfi =
		    ((qfi & SRV6_PDU_SESSION_QFI_MASK) >> 2) |
		    ((qfi & SRV6_PDU_SESSION_R_BIT_MASK) << 5);

		  sess =
		    (gtpu_pdu_session_t *) (((char *) hdr0) +
					    sizeof (ip4_gtpu_header_t) +
					    sizeof (gtpu_exthdr_t));
		  sess->exthdrlen = 1;
		  sess->type = type;
		  sess->spare = 0;
		  sess->u.val = qfi;
		  sess->nextexthdr = 0;
		}

	      if (gtpu_type == GTPU_TYPE_ECHO_REPLY
		  || gtpu_type == GTPU_TYPE_ECHO_REQUEST
		  || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		{
		  hdr0->gtpu.ver_flags |= GTPU_SEQ_FLAG;
		  hdr0->gtpu.ext->seq = seq;
		  hdr0->gtpu.ext->npdu_num = 0;
		  hdr0->gtpu.ext->nextexthdr = 0;

		  if (gtpu_type == GTPU_TYPE_ECHO_REPLY)
		    {
		      gtpu_recovery_ie *recovery;

		      recovery =
			(gtpu_recovery_ie *) ((u8 *) hdr0 +
					      (hdrlen -
					       sizeof (gtpu_recovery_ie)));
		      recovery->type = GTPU_RECOVERY_IE_TYPE;
		      recovery->restart_counter = 0;
		    }
		  else if (gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      if (ie_size)
			{
			  u8 *ie_ptr;

			  ie_ptr = (u8 *) ((u8 *) hdr0 + (hdrlen - ie_size));
			  clib_memcpy_fast (ie_ptr, ie_buf, ie_size);
			}
		    }
		}

	      offset = ls_param->v4src_position / 8;
	      shift = ls_param->v4src_position % 8;

	      if (PREDICT_TRUE (shift == 0))
		{
		  for (index = 0; index < 4; index++)
		    {
		      hdr0->ip4.src_address.as_u8[index] =
			src0.as_u8[offset + index];
		    }
		}
	      else
		{
		  for (index = 0; index < 4; index++)
		    {
		      hdr0->ip4.src_address.as_u8[index] =
			src0.as_u8[offset + index] << shift;
		      hdr0->ip4.src_address.as_u8[index] |=
			src0.as_u8[offset + index + 1] >> (8 - shift);
		    }
		}

	      key = hash_memory (p, plen < 40 ? plen : 40, 0);
	      port = hash_uword_to_u16 (&key);
	      hdr0->udp.src_port = port;

	      hdr0->udp.length = clib_host_to_net_u16 (len0 +
						       sizeof (udp_header_t) +
						       sizeof
						       (gtpu_header_t));

	      hdr0->ip4.length = clib_host_to_net_u16 (len0 +
						       sizeof
						       (ip4_gtpu_header_t));

	      hdr0->ip4.checksum = ip4_header_checksum (&hdr0->ip4);

	      good_n++;

	      if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
		  PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  srv6_end_rewrite_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  clib_memcpy (tr->src.as_u8, hdr0->ip4.src_address.as_u8,
			       sizeof (tr->src.as_u8));
		  clib_memcpy (tr->dst.as_u8, hdr0->ip4.dst_address.as_u8,
			       sizeof (tr->dst.as_u8));
		  tr->teid = hdr0->gtpu.teid;
		}
	    }

	  vlib_increment_combined_counter
	    (((next0 ==
	       SRV6_END_M_GTP4_E_NEXT_DROP) ? &(sm2->sr_ls_invalid_counters) :
	      &(sm2->sr_ls_valid_counters)), thread_index,
	     ls0 - sm2->localsids, 1, vlib_buffer_length_in_chain (vm, b0));

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, sm->end_m_gtp4_e_node_index,
			       SRV6_END_ERROR_M_GTP4_E_BAD_PACKETS, bad_n);

  vlib_node_increment_counter (vm, sm->end_m_gtp4_e_node_index,
			       SRV6_END_ERROR_M_GTP4_E_PACKETS, good_n);

  return frame->n_vectors;
}

// Function for SRv6 GTP4.D function.
VLIB_NODE_FN (srv6_t_m_gtp4_d) (vlib_main_t * vm,
				vlib_node_runtime_t * node,
				vlib_frame_t * frame)
{
  srv6_t_main_v4_decap_t *sm = &srv6_t_main_v4_decap;
  ip6_sr_main_t *sm2 = &sr_main;
  u32 n_left_from, next_index, *from, *to_next;

  u32 good_n = 0, bad_n = 0;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  ip6_sr_sl_t *sl0;
	  srv6_end_gtp4_param_t *ls_param;
	  ip4_header_t *ip4;

	  uword len0;

	  u32 next0 = SRV6_T_M_GTP4_D_NEXT_LOOKUP;

	  // defaults
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);

	  sl0 =
	    pool_elt_at_index (sm2->sid_lists,
			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);

	  ls_param = (srv6_end_gtp4_param_t *) sl0->plugin_mem;

	  len0 = vlib_buffer_length_in_chain (vm, b0);

	  ip4 = vlib_buffer_get_current (b0);

	  if (ip4->protocol != IP_PROTOCOL_UDP
	      || len0 < sizeof (ip4_gtpu_header_t))
	    {
	      next0 = SRV6_T_M_GTP4_D_NEXT_DROP;

	      bad_n++;
	    }
	  else
	    {
	      uword *p;
	      ip6_sr_policy_t *sr_policy = NULL;
	      ip6_sr_sl_t *sl = NULL;
	      u32 *sl_index;
	      u32 hdr_len;

	      ip4_gtpu_header_t *hdr;
	      ip4_address_t src, dst;
	      u8 *srcp, *dstp;
	      ip6_header_t *encap = NULL;
	      ip6_address_t seg;
	      ip6_address_t src6;
	      u8 gtpu_type;
	      u32 teid;
	      u8 *teidp;
	      u8 qfi = 0;
	      u8 *qfip = NULL;
	      u16 seq = 0;
	      u8 *seqp;
	      u32 offset, shift, index;
	      ip6srv_combo_header_t *ip6srv;
	      gtpu_pdu_session_t *sess = NULL;
	      int ie_size = 0;
	      u16 tlv_siz = 0;
	      u8 ie_buf[GTPU_IE_MAX_SIZ];

	      // Decap from GTP-U.
	      hdr = (ip4_gtpu_header_t *) ip4;

	      hdr_len = sizeof (ip4_gtpu_header_t);

	      teid = hdr->gtpu.teid;
	      teidp = (u8 *) & teid;

	      seqp = (u8 *) & seq;

	      gtpu_type = hdr->gtpu.type;

	      if (hdr->gtpu.ver_flags & (GTPU_EXTHDR_FLAG | GTPU_SEQ_FLAG))
		{
		  // Extention header.
		  hdr_len += sizeof (gtpu_exthdr_t);

		  seq = hdr->gtpu.ext->seq;

		  if (hdr->gtpu.ext->nextexthdr == GTPU_EXTHDR_PDU_SESSION)
		    {
		      // PDU Session Container.
		      sess =
			(gtpu_pdu_session_t *) (((char *) hdr) + hdr_len);
		      qfi = sess->u.val & ~GTPU_PDU_SESSION_P_BIT_MASK;
		      qfip = (u8 *) & qfi;

		      hdr_len += sizeof (gtpu_pdu_session_t);

		      if (sess->u.val & GTPU_PDU_SESSION_P_BIT_MASK)
			{
			  hdr_len += sizeof (gtpu_paging_policy_t);
			}
		    }
		}

	      src = hdr->ip4.src_address;
	      srcp = (u8 *) & src;

	      dst = hdr->ip4.dst_address;
	      dstp = (u8 *) & dst;

	      seg = ls_param->sr_prefix;

	      offset = ls_param->sr_prefixlen / 8;
	      shift = ls_param->sr_prefixlen % 8;

	      if (PREDICT_TRUE (shift == 0))
		{
		  clib_memcpy_fast (&seg.as_u8[offset], dstp, 4);

		  if (qfip)
		    {
		      qfi =
			((qfi & GTPU_PDU_SESSION_QFI_MASK) << 2) |
			((qfi & GTPU_PDU_SESSION_R_BIT_MASK) >> 5);

		      if (sess->type)
			{
			  qfi |= SRV6_PDU_SESSION_U_BIT_MASK;
			}

		      seg.as_u8[offset + 4] = qfi;
		    }

		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      clib_memcpy_fast (&seg.as_u8[offset + 5], seqp, 2);
		    }
		  else
		    {
		      clib_memcpy_fast (&seg.as_u8[offset + 5], teidp, 4);
		    }
		}
	      else
		{
		  for (index = 0; index < 4; index++)
		    {
		      seg.as_u8[offset + index] |= dstp[index] >> shift;
		      seg.as_u8[offset + index + 1] |=
			dstp[index] << (8 - shift);
		    }

		  if (qfip)
		    {
		      qfi =
			((qfi & GTPU_PDU_SESSION_QFI_MASK) << 2) |
			((qfi & GTPU_PDU_SESSION_R_BIT_MASK) >> 5);

		      if (sess->type)
			{
			  qfi |= SRV6_PDU_SESSION_U_BIT_MASK;
			}

		      seg.as_u8[offset + 4] |= qfi >> shift;
		      seg.as_u8[offset + 5] |= qfi << (8 - shift);
		    }

		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      for (index = 0; index < 2; index++)
			{
			  seg.as_u8[offset + 5 + index] |=
			    seqp[index] >> shift;
			  seg.as_u8[offset + 6 + index] |=
			    seqp[index] << (8 - shift);
			}
		    }
		  else
		    {
		      for (index = 0; index < 4; index++)
			{
			  seg.as_u8[offset + index + 5] |=
			    teidp[index] >> shift;
			  seg.as_u8[offset + index + 6] |=
			    teidp[index] << (8 - shift);
			}
		    }
		}

	      if (PREDICT_FALSE (gtpu_type == GTPU_TYPE_ERROR_INDICATION))
		{
		  u16 payload_len;

		  payload_len = clib_net_to_host_u16 (hdr->gtpu.length);
		  if (payload_len != 0)
		    {
		      ie_size =
			payload_len - (hdr_len - sizeof (ip4_gtpu_header_t));
		      if (ie_size > 0)
			{
			  u8 *ies;

			  ies = (u8 *) ((u8 *) hdr + hdr_len);
			  clib_memcpy_fast (ie_buf, ies, ie_size);
			  hdr_len += ie_size;
			}
		    }
		}

	      src6 = ls_param->v6src_prefix;

	      offset = ls_param->v6src_prefixlen / 8;
	      shift = ls_param->v6src_prefixlen % 8;

	      if (PREDICT_TRUE (shift == 0))
		{
		  clib_memcpy_fast (&src6.as_u8[offset], srcp, 4);
		}
	      else
		{
		  for (index = 0; index < 4; index++)
		    {
		      src6.as_u8[offset + index] |= srcp[offset] >> shift;
		      src6.as_u8[offset + index + 1] |=
			srcp[offset] << (8 - shift);
		    }
		}

	      vlib_buffer_advance (b0, (word) hdr_len);

	      // Encap to SRv6.
	      if (PREDICT_TRUE (gtpu_type == GTPU_TYPE_GTPU))
		{
		  encap = vlib_buffer_get_current (b0);
		}

	      len0 = vlib_buffer_length_in_chain (vm, b0);

	      p =
		mhash_get (&sm2->sr_policies_index_hash,
			   &ls_param->sr_prefix);
	      if (p)
		{
		  sr_policy = pool_elt_at_index (sm2->sr_policies, p[0]);
		}

	      if (sr_policy)
		{
		  vec_foreach (sl_index, sr_policy->segments_lists)
		  {
		    sl = pool_elt_at_index (sm2->sid_lists, *sl_index);
		    if (sl != NULL)
		      break;
		  }
		}

	      if (sl)
		{
		  hdr_len = sizeof (ip6srv_combo_header_t);
		  hdr_len += vec_len (sl->segments) * sizeof (ip6_address_t);
		  hdr_len += sizeof (ip6_address_t);
		}
	      else
		{
		  hdr_len = sizeof (ip6_header_t);

		  if (PREDICT_FALSE (gtpu_type != GTPU_TYPE_GTPU))
		    {
		      hdr_len += sizeof (ip6_sr_header_t);
		      hdr_len += sizeof (ip6_address_t);
		    }
		}

	      if (ie_size)
		{
		  tlv_siz =
		    sizeof (ip6_sr_tlv_t) + sizeof (user_plane_sub_tlv_t) +
		    ie_size;

		  tlv_siz = (tlv_siz & ~0x07) + (tlv_siz & 0x07 ? 0x08 : 0x0);
		  hdr_len += tlv_siz;
		}

	      vlib_buffer_advance (b0, -(word) hdr_len);
	      ip6srv = vlib_buffer_get_current (b0);

	      if (sl)
		{
		  clib_memcpy_fast (ip6srv, sl->rewrite,
				    vec_len (sl->rewrite));

		  if (vec_len (sl->segments) > 1)
		    {
		      ip6srv->sr.tag =
			clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		      ip6srv->sr.segments_left += 1;
		      ip6srv->sr.last_entry += 1;

		      ip6srv->sr.length += sizeof (ip6_address_t) / 8;
		      ip6srv->sr.segments[0] = seg;

		      clib_memcpy_fast (&ip6srv->sr.segments[1],
					(u8 *) (sl->rewrite +
						sizeof (ip6_header_t) +
						sizeof (ip6_sr_header_t)),
					vec_len (sl->segments) *
					sizeof (ip6_address_t));
		    }
		  else
		    {
		      ip6srv->ip.protocol = IP_PROTOCOL_IPV6_ROUTE;

		      ip6srv->sr.type = ROUTING_HEADER_TYPE_SR;

		      ip6srv->sr.segments_left = 1;
		      ip6srv->sr.last_entry = 0;

		      ip6srv->sr.length =
			((sizeof (ip6_sr_header_t) +
			  sizeof (ip6_address_t)) / 8) - 1;
		      ip6srv->sr.flags = 0;

		      ip6srv->sr.tag =
			clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		      ip6srv->sr.segments[0] = seg;
		      if (vec_len (sl->segments))
			{
			  ip6srv->sr.segments[1] = sl->segments[0];
			  ip6srv->sr.length += sizeof (ip6_address_t) / 8;
			  ip6srv->sr.last_entry++;
			}
		    }

		  if (PREDICT_TRUE (encap != NULL))
		    {
		      if (ls_param->nhtype == SRV6_NHTYPE_NONE)
			{
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) == 6)
			    ip6srv->sr.protocol = IP_PROTOCOL_IPV6;
			  else
			    ip6srv->sr.protocol = IP_PROTOCOL_IP_IN_IP;
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_IPV4)
			{
			  ip6srv->sr.protocol = IP_PROTOCOL_IP_IN_IP;
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 4)
			    {
			      // Bad encap packet.
			      next0 = SRV6_T_M_GTP4_D_NEXT_DROP;
			      bad_n++;
			      goto DONE;
			    }
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_IPV6)
			{
			  ip6srv->sr.protocol = IP_PROTOCOL_IPV6;
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 6)
			    {
			      // Bad encap packet.
			      next0 = SRV6_T_M_GTP4_D_NEXT_DROP;
			      bad_n++;
			      goto DONE;
			    }
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_NON_IP)
			{
			  ip6srv->sr.protocol = IP_PROTOCOL_IP6_ETHERNET;
			}
		    }
		  else
		    {
		      ip6srv->sr.protocol = IP_PROTOCOL_IP6_ETHERNET;
		    }
		}
	      else
		{
		  clib_memcpy_fast (ip6srv, &sm->cache_hdr,
				    sizeof (ip6_header_t));

		  ip6srv->ip.dst_address = seg;

		  if (PREDICT_FALSE (gtpu_type != GTPU_TYPE_GTPU))
		    {
		      ip6srv->ip.protocol = IP_PROTOCOL_IPV6_ROUTE;

		      ip6srv->sr.protocol = IP_PROTOCOL_IP6_ETHERNET;

		      ip6srv->sr.tag =
			clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		      ip6srv->sr.segments_left = 0;
		      ip6srv->sr.last_entry = 0;

		      ip6srv->sr.length = sizeof (ip6_address_t) / 8;
		      ip6srv->sr.segments[0] = seg;
		    }
		  else
		    {
		      if (ls_param->nhtype == SRV6_NHTYPE_NONE)
			{
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) == 6)
			    ip6srv->ip.protocol = IP_PROTOCOL_IPV6;
			  else
			    ip6srv->ip.protocol = IP_PROTOCOL_IP_IN_IP;
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_IPV4)
			{
			  ip6srv->ip.protocol = IP_PROTOCOL_IP_IN_IP;
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 4)
			    {
			      // Bad encap packet.
			      next0 = SRV6_T_M_GTP4_D_NEXT_DROP;
			      bad_n++;
			      goto DONE;
			    }
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_IPV6)
			{
			  ip6srv->ip.protocol = IP_PROTOCOL_IPV6;
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 6)
			    {
			      // Bad encap packet.
			      next0 = SRV6_T_M_GTP4_D_NEXT_DROP;
			      bad_n++;
			      goto DONE;
			    }
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_NON_IP)
			{
			  ip6srv->ip.protocol = IP_PROTOCOL_IP6_ETHERNET;
			}
		    }
		}

	      ip6srv->ip.src_address = src6;

	      if (PREDICT_FALSE (ie_size))
		{
		  ip6_sr_tlv_t *tlv;
		  user_plane_sub_tlv_t *sub_tlv;

		  tlv =
		    (ip6_sr_tlv_t *) ((u8 *) ip6srv + (hdr_len - tlv_siz));
		  tlv->type = SRH_TLV_USER_PLANE_CONTAINER;
		  tlv->length = (u8) (tlv_siz - sizeof (ip6_sr_tlv_t));
		  clib_memset (tlv->value, 0, tlv->length);

		  sub_tlv = (user_plane_sub_tlv_t *) tlv->value;
		  sub_tlv->type = USER_PLANE_SUB_TLV_IE;
		  sub_tlv->length = (u8) ie_size;
		  clib_memcpy_fast (sub_tlv->value, ie_buf, ie_size);

		  ip6srv->sr.length += (u8) (tlv_siz / 8);
		}

	      ip6srv->ip.payload_length =
		clib_host_to_net_u16 (len0 + hdr_len - sizeof (ip6_header_t));

	      good_n++;

	      if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
		  PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  srv6_end_rewrite_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  clib_memcpy (tr->src.as_u8, ip6srv->ip.src_address.as_u8,
			       sizeof (tr->src.as_u8));
		  clib_memcpy (tr->dst.as_u8, ip6srv->ip.dst_address.as_u8,
			       sizeof (tr->dst.as_u8));
		}
	    }

	DONE:
	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, sm->t_m_gtp4_d_node_index,
			       SRV6_T_ERROR_M_GTP4_D_BAD_PACKETS, bad_n);

  vlib_node_increment_counter (vm, sm->t_m_gtp4_d_node_index,
			       SRV6_T_ERROR_M_GTP4_D_PACKETS, good_n);

  return frame->n_vectors;
}

VLIB_REGISTER_NODE (srv6_end_m_gtp4_e) =
{
  .name = "srv6-end-m-gtp4-e",.vector_size = sizeof (u32),.format_trace =
    format_srv6_end_rewrite_trace,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARRAY_LEN (srv6_end_error_v4_strings),.error_strings =
    srv6_end_error_v4_strings,.n_next_nodes =
    SRV6_END_M_GTP4_E_N_NEXT,.next_nodes =
  {
  [SRV6_END_M_GTP4_E_NEXT_DROP] =
      "error-drop",[SRV6_END_M_GTP4_E_NEXT_LOOKUP] = "ip4-lookup",}
,};

VLIB_REGISTER_NODE (srv6_t_m_gtp4_d) =
{
  .name = "srv6-t-m-gtp4-d",.vector_size = sizeof (u32),.format_trace =
    format_srv6_end_rewrite_trace,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARRAY_LEN (srv6_t_error_v4_d_strings),.error_strings =
    srv6_t_error_v4_d_strings,.n_next_nodes =
    SRV6_T_M_GTP4_D_N_NEXT,.next_nodes =
  {
  [SRV6_T_M_GTP4_D_NEXT_DROP] =
      "error-drop",[SRV6_T_M_GTP4_D_NEXT_LOOKUP] = "ip6-lookup",}
,};

// Function for SRv6 GTP6.E function
VLIB_NODE_FN (srv6_end_m_gtp6_e) (vlib_main_t * vm,
				  vlib_node_runtime_t * node,
				  vlib_frame_t * frame)
{
  srv6_end_main_v6_t *sm = &srv6_end_main_v6;
  ip6_sr_main_t *sm2 = &sr_main;
  u32 n_left_from, next_index, *from, *to_next;
  u32 thread_index = vm->thread_index;

  u32 good_n = 0, bad_n = 0;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  ip6_sr_localsid_t *ls0;

	  ip6srv_combo_header_t *ip6srv0;
	  ip6_address_t dst0, src0, seg0;

	  ip6_gtpu_header_t *hdr0 = NULL;
	  uword len0;
	  uword key;
	  u16 port;
	  u16 tag;
	  void *p;
	  uword plen;

	  u32 next0 = SRV6_END_M_GTP6_E_NEXT_LOOKUP;

	  // defaults
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  ls0 =
	    pool_elt_at_index (sm2->localsids,
			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);

	  ip6srv0 = vlib_buffer_get_current (b0);
	  dst0 = ip6srv0->ip.dst_address;
	  src0 = ip6srv0->ip.src_address;
	  seg0 = ip6srv0->sr.segments[0];

	  tag = ip6srv0->sr.tag;

	  len0 = vlib_buffer_length_in_chain (vm, b0);

	  if ((ip6srv0->ip.protocol != IPPROTO_IPV6_ROUTE)
	      || (len0 <
		  sizeof (ip6srv_combo_header_t) + 8 * ip6srv0->sr.length))
	    {
	      next0 = SRV6_END_M_GTP6_E_NEXT_DROP;

	      bad_n++;
	    }
	  else
	    {
	      // we need to be sure there is enough space before
	      // ip6srv0 header, there is some extra space
	      // in the pre_data area for this kind of
	      // logic

	      u32 teid = 0;
	      u8 *teid8p = (u8 *) & teid;
	      u8 qfi = 0;
	      u16 seq = 0;
	      u8 gtpu_type = 0;
	      u16 index;
	      u16 offset, shift;
	      u32 hdrlen = 0;
	      u16 ie_size = 0;
	      u8 ie_buf[GTPU_IE_MAX_SIZ];

	      index = ls0->localsid_prefix_len;
	      index += 8;
	      offset = index / 8;
	      shift = index % 8;

	      gtpu_type = gtpu_type_get (tag);

	      if (PREDICT_TRUE (shift == 0))
		{
		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      clib_memcpy_fast (&seq, &dst0.as_u8[offset], 2);
		    }
		  else
		    {
		      clib_memcpy_fast (teid8p, &dst0.as_u8[offset], 4);
		    }

		  qfi = dst0.as_u8[offset + 4];
		}
	      else
		{
		  u8 *sp;

		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      sp = (u8 *) & seq;
		      for (index = 0; index < 2; index++)
			{
			  sp[index] = dst0.as_u8[offset + index] << shift;
			  sp[index] |=
			    dst0.as_u8[offset + index + 1] >> (8 - shift);
			}
		    }
		  else
		    {
		      for (index = 0; index < 4; index++)
			{
			  *teid8p = dst0.as_u8[offset + index] << shift;
			  *teid8p |=
			    dst0.as_u8[offset + index + 1] >> (8 - shift);
			  teid8p++;
			}
		    }

		  qfi |= dst0.as_u8[offset + 4] << shift;
		  qfi |= dst0.as_u8[offset + 5] >> (8 - shift);
		}

	      if (qfi)
		{
		  hdrlen =
		    sizeof (gtpu_exthdr_t) + sizeof (gtpu_pdu_session_t);
		}
	      else if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		       || gtpu_type == GTPU_TYPE_ECHO_REPLY
		       || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		{
		  hdrlen = sizeof (gtpu_exthdr_t);
		}

	      if (gtpu_type == GTPU_TYPE_ECHO_REPLY)
		{
		  hdrlen += sizeof (gtpu_recovery_ie);
		}

	      if (PREDICT_FALSE (gtpu_type == GTPU_TYPE_ERROR_INDICATION))
		{
		  ip6_sr_tlv_t *tlv;
		  u16 ext_len;

		  ext_len = ip6srv0->sr.length * 8;

		  if (ext_len >
		      sizeof (ip6_address_t) * (ip6srv0->sr.last_entry + 1))
		    {
		      tlv =
			(ip6_sr_tlv_t *) ((u8 *) & ip6srv0->sr +
					  sizeof (ip6_sr_header_t) +
					  sizeof (ip6_address_t) *
					  (ip6srv0->sr.last_entry + 1));

		      if (tlv->type == SRH_TLV_USER_PLANE_CONTAINER)
			{
			  user_plane_sub_tlv_t *sub_tlv;

			  sub_tlv = (user_plane_sub_tlv_t *) tlv->value;

			  ie_size = sub_tlv->length;
			  clib_memcpy_fast (ie_buf, sub_tlv->value, ie_size);

			  hdrlen += ie_size;
			}
		    }
		}

	      vlib_buffer_advance (b0,
				   (word) sizeof (ip6srv_combo_header_t) +
				   ip6srv0->sr.length * 8);

	      // get length of encapsulated IPv6 packet (the remaining part)
	      p = vlib_buffer_get_current (b0);

	      plen = len0 = vlib_buffer_length_in_chain (vm, b0);

	      len0 += hdrlen;

	      hdrlen += sizeof (ip6_gtpu_header_t);

	      vlib_buffer_advance (b0, -(word) hdrlen);

	      hdr0 = vlib_buffer_get_current (b0);

	      clib_memcpy_fast (hdr0, &sm->cache_hdr,
				sizeof (ip6_gtpu_header_t));

	      hdr0->gtpu.teid = teid;
	      hdr0->gtpu.length = clib_host_to_net_u16 (len0);

	      hdr0->gtpu.type = gtpu_type;

	      if (qfi)
		{
		  u8 type = 0;
		  gtpu_pdu_session_t *sess;

		  hdr0->gtpu.ver_flags |= GTPU_EXTHDR_FLAG;

		  hdr0->gtpu.ext->seq = 0;
		  hdr0->gtpu.ext->npdu_num = 0;
		  hdr0->gtpu.ext->nextexthdr = GTPU_EXTHDR_PDU_SESSION;

		  type = qfi & SRV6_PDU_SESSION_U_BIT_MASK;

		  qfi =
		    ((qfi & SRV6_PDU_SESSION_QFI_MASK) >> 2) |
		    ((qfi & SRV6_PDU_SESSION_R_BIT_MASK) << 5);

		  sess =
		    (gtpu_pdu_session_t *) (((char *) hdr0) +
					    sizeof (ip6_gtpu_header_t) +
					    sizeof (gtpu_exthdr_t));
		  sess->exthdrlen = 1;
		  sess->type = type;
		  sess->spare = 0;
		  sess->u.val = qfi;
		  sess->nextexthdr = 0;
		}

	      if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		  || gtpu_type == GTPU_TYPE_ECHO_REPLY
		  || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		{
		  hdr0->gtpu.ver_flags |= GTPU_SEQ_FLAG;
		  hdr0->gtpu.ext->seq = seq;
		  hdr0->gtpu.ext->npdu_num = 0;
		  hdr0->gtpu.ext->nextexthdr = 0;

		  if (gtpu_type == GTPU_TYPE_ECHO_REPLY)
		    {
		      gtpu_recovery_ie *recovery;

		      recovery =
			(gtpu_recovery_ie *) ((u8 *) hdr0 +
					      (hdrlen -
					       sizeof (gtpu_recovery_ie)));
		      recovery->type = GTPU_RECOVERY_IE_TYPE;
		      recovery->restart_counter = 0;
		    }
		  else if (gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      if (ie_size)
			{
			  u8 *ie_ptr;

			  ie_ptr = (u8 *) ((u8 *) hdr0 + (hdrlen - ie_size));
			  clib_memcpy_fast (ie_ptr, ie_buf, ie_size);
			}
		    }
		}

	      hdr0->udp.length = clib_host_to_net_u16 (len0 +
						       sizeof (udp_header_t) +
						       sizeof
						       (gtpu_header_t));

	      clib_memcpy_fast (hdr0->ip6.src_address.as_u8, src0.as_u8,
				sizeof (ip6_address_t));
	      clib_memcpy_fast (hdr0->ip6.dst_address.as_u8, &seg0.as_u8,
				sizeof (ip6_address_t));

	      hdr0->ip6.payload_length = clib_host_to_net_u16 (len0 +
							       sizeof
							       (udp_header_t)
							       +
							       sizeof
							       (gtpu_header_t));

	      // UDP source port.
	      key = hash_memory (p, plen < 40 ? plen : 40, 0);
	      port = hash_uword_to_u16 (&key);
	      hdr0->udp.src_port = port;

	      good_n++;

	      if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
		  PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  srv6_end_rewrite_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  clib_memcpy (tr->src.as_u8, hdr0->ip6.src_address.as_u8,
			       sizeof (ip6_address_t));
		  clib_memcpy (tr->dst.as_u8, hdr0->ip6.dst_address.as_u8,
			       sizeof (ip6_address_t));
		  tr->teid = hdr0->gtpu.teid;
		}
	    }

	  vlib_increment_combined_counter
	    (((next0 ==
	       SRV6_END_M_GTP6_E_NEXT_DROP) ? &(sm2->sr_ls_invalid_counters) :
	      &(sm2->sr_ls_valid_counters)), thread_index,
	     ls0 - sm2->localsids, 1, vlib_buffer_length_in_chain (vm, b0));

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, sm->end_m_gtp6_e_node_index,
			       SRV6_END_ERROR_M_GTP6_E_BAD_PACKETS, bad_n);

  vlib_node_increment_counter (vm, sm->end_m_gtp6_e_node_index,
			       SRV6_END_ERROR_M_GTP6_E_PACKETS, good_n);

  return frame->n_vectors;
}

// Function for SRv6 GTP6.D function
VLIB_NODE_FN (srv6_end_m_gtp6_d) (vlib_main_t * vm,
				  vlib_node_runtime_t * node,
				  vlib_frame_t * frame)
{
  srv6_end_main_v6_decap_t *sm = &srv6_end_main_v6_decap;
  ip6_sr_main_t *sm2 = &sr_main;
  u32 n_left_from, next_index, *from, *to_next;
  u32 thread_index = vm->thread_index;

  u32 good_n = 0, bad_n = 0;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  ip6_sr_localsid_t *ls0;
	  srv6_end_gtp6_param_t *ls_param;

	  ip6_gtpu_header_t *hdr0 = NULL;
	  uword len0;

	  ip6_address_t seg0, src0;
	  u32 teid = 0;
	  u8 *teidp;
	  u8 gtpu_type = 0;
	  u8 qfi;
	  u8 *qfip = NULL;
	  u16 seq = 0;
	  u8 *seqp;
	  u32 offset, shift;
	  u32 hdrlen;
	  ip6_header_t *encap = NULL;
	  gtpu_pdu_session_t *sess = NULL;
	  int ie_size = 0;
	  u16 tlv_siz = 0;
	  u8 ie_buf[GTPU_IE_MAX_SIZ];

	  u32 next0 = SRV6_END_M_GTP6_D_NEXT_LOOKUP;

	  // defaults
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  ls0 =
	    pool_elt_at_index (sm2->localsids,
			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);

	  ls_param = (srv6_end_gtp6_param_t *) ls0->plugin_mem;

	  hdr0 = vlib_buffer_get_current (b0);

	  hdrlen = sizeof (ip6_gtpu_header_t);

	  len0 = vlib_buffer_length_in_chain (vm, b0);

	  if ((hdr0->ip6.protocol != IP_PROTOCOL_UDP)
	      || (hdr0->udp.dst_port !=
		  clib_host_to_net_u16 (SRV6_GTP_UDP_DST_PORT))
	      || (len0 < sizeof (ip6_gtpu_header_t)))
	    {
	      next0 = SRV6_END_M_GTP6_D_NEXT_DROP;

	      bad_n++;
	    }
	  else
	    {
	      seg0 = ls_param->sr_prefix;
	      src0 = hdr0->ip6.src_address;

	      gtpu_type = hdr0->gtpu.type;

	      teid = hdr0->gtpu.teid;
	      teidp = (u8 *) & teid;

	      seqp = (u8 *) & seq;

	      if (hdr0->gtpu.ver_flags & (GTPU_EXTHDR_FLAG | GTPU_SEQ_FLAG))
		{
		  // Extention header.
		  hdrlen += sizeof (gtpu_exthdr_t);

		  seq = hdr0->gtpu.ext->seq;

		  if (hdr0->gtpu.ext->nextexthdr == GTPU_EXTHDR_PDU_SESSION)
		    {
		      // PDU Session Container.
		      sess =
			(gtpu_pdu_session_t *) (((char *) hdr0) +
						sizeof (ip6_gtpu_header_t) +
						sizeof (gtpu_exthdr_t));
		      qfi = sess->u.val & ~GTPU_PDU_SESSION_P_BIT_MASK;
		      qfip = (u8 *) & qfi;

		      hdrlen += sizeof (gtpu_pdu_session_t);

		      if (sess->u.val & GTPU_PDU_SESSION_P_BIT_MASK)
			{
			  hdrlen += sizeof (gtpu_paging_policy_t);
			}
		    }
		}

	      offset = ls_param->sr_prefixlen / 8;
	      shift = ls_param->sr_prefixlen % 8;

	      offset += 1;
	      if (PREDICT_TRUE (shift == 0))
		{
		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      clib_memcpy_fast (&seg0.as_u8[offset], seqp, 2);
		    }
		  else
		    {
		      clib_memcpy_fast (&seg0.as_u8[offset], teidp, 4);
		    }

		  if (qfip)
		    {
		      qfi =
			((qfi & GTPU_PDU_SESSION_QFI_MASK) << 2) |
			((qfi & GTPU_PDU_SESSION_R_BIT_MASK) >> 5);

		      if (sess->type)
			{
			  qfi |= SRV6_PDU_SESSION_U_BIT_MASK;
			}

		      seg0.as_u8[offset + 4] = qfi;
		    }
		}
	      else
		{
		  int idx;

		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      for (idx = 0; idx < 2; idx++)
			{
			  seg0.as_u8[offset + idx] |= seqp[idx] >> shift;
			  seg0.as_u8[offset + idx + 1] |=
			    seqp[idx] << (8 - shift);
			}
		    }
		  else
		    {
		      for (idx = 0; idx < 4; idx++)
			{
			  seg0.as_u8[offset + idx] |= teidp[idx] >> shift;
			  seg0.as_u8[offset + idx + 1] |=
			    teidp[idx] << (8 - shift);
			}
		    }

		  if (qfip)
		    {
		      qfi =
			((qfi & GTPU_PDU_SESSION_QFI_MASK) << 2) |
			((qfi & ~GTPU_PDU_SESSION_R_BIT_MASK) >> 5);

		      if (sess->type)
			{
			  qfi |= SRV6_PDU_SESSION_U_BIT_MASK;
			}

		      seg0.as_u8[offset + 4] |= qfi >> shift;
		      seg0.as_u8[offset + 5] |= qfi << (8 - shift);
		    }
		}

	      if (PREDICT_FALSE (gtpu_type == GTPU_TYPE_ERROR_INDICATION))
		{
		  u16 payload_len;

		  payload_len = clib_net_to_host_u16 (hdr0->gtpu.length);
		  if (payload_len != 0)
		    {
		      ie_size =
			payload_len - (hdrlen - sizeof (ip6_gtpu_header_t));
		      if (ie_size > 0)
			{
			  u8 *ies;

			  ies = (u8 *) ((u8 *) hdr0 + hdrlen);
			  clib_memcpy_fast (ie_buf, ies, ie_size);
			  hdrlen += ie_size;
			}
		    }
		}

	      // jump over variable length data
	      vlib_buffer_advance (b0, (word) hdrlen);

	      // get length of encapsulated IPv6 packet (the remaining part)
	      len0 = vlib_buffer_length_in_chain (vm, b0);

	      if (PREDICT_TRUE (gtpu_type == GTPU_TYPE_GTPU))
		{
		  encap = vlib_buffer_get_current (b0);
		}

	      uword *p;
	      ip6srv_combo_header_t *ip6srv;
	      ip6_sr_policy_t *sr_policy = NULL;
	      ip6_sr_sl_t *sl = NULL;
	      u32 *sl_index;
	      u32 hdr_len;

	      p =
		mhash_get (&sm2->sr_policies_index_hash,
			   &ls_param->sr_prefix);
	      if (p)
		{
		  sr_policy = pool_elt_at_index (sm2->sr_policies, p[0]);
		}

	      if (sr_policy)
		{
		  vec_foreach (sl_index, sr_policy->segments_lists)
		  {
		    sl = pool_elt_at_index (sm2->sid_lists, *sl_index);
		    if (sl != NULL)
		      break;
		  }
		}

	      if (sl)
		{
		  hdr_len = sizeof (ip6srv_combo_header_t);
		  hdr_len += vec_len (sl->segments) * sizeof (ip6_address_t);
		  hdr_len += sizeof (ip6_address_t);
		}
	      else
		{
		  hdr_len = sizeof (ip6_header_t);
		  if (PREDICT_FALSE (gtpu_type) != GTPU_TYPE_GTPU)
		    {
		      hdr_len += sizeof (ip6_sr_header_t);
		      hdr_len += sizeof (ip6_address_t);
		    }
		}

	      if (ie_size)
		{
		  tlv_siz =
		    sizeof (ip6_sr_tlv_t) + sizeof (user_plane_sub_tlv_t) +
		    ie_size;

		  tlv_siz = (tlv_siz & ~0x07) + (tlv_siz & 0x07 ? 0x08 : 0x0);
		  hdr_len += tlv_siz;
		}

	      // jump back to data[0] or pre_data if required
	      vlib_buffer_advance (b0, -(word) hdr_len);

	      ip6srv = vlib_buffer_get_current (b0);

	      if (sl)
		{
		  clib_memcpy_fast (ip6srv, sl->rewrite,
				    vec_len (sl->rewrite));

		  if (vec_len (sl->segments) > 1)
		    {
		      ip6srv->ip.src_address = src0;

		      ip6srv->sr.tag =
			clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		      ip6srv->sr.segments_left += 1;
		      ip6srv->sr.last_entry += 1;

		      ip6srv->sr.length += sizeof (ip6_address_t) / 8;
		      ip6srv->sr.segments[0] = seg0;

		      clib_memcpy_fast (&ip6srv->sr.segments[1],
					(u8 *) (sl->rewrite +
						sizeof (ip6_header_t) +
						sizeof (ip6_sr_header_t)),
					vec_len (sl->segments) *
					sizeof (ip6_address_t));
		    }
		  else
		    {
		      ip6srv->ip.src_address = src0;
		      ip6srv->ip.protocol = IP_PROTOCOL_IPV6_ROUTE;

		      ip6srv->sr.type = ROUTING_HEADER_TYPE_SR;
		      ip6srv->sr.segments_left = 1;
		      ip6srv->sr.last_entry = 0;
		      ip6srv->sr.length =
			((sizeof (ip6_sr_header_t) +
			  sizeof (ip6_address_t)) / 8) - 1;
		      ip6srv->sr.flags = 0;

		      ip6srv->sr.tag =
			clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		      ip6srv->sr.segments[0] = seg0;

		      if (vec_len (sl->segments))
			{
			  ip6srv->sr.segments[1] = sl->segments[0];
			  ip6srv->sr.last_entry++;
			  ip6srv->sr.length += sizeof (ip6_address_t) / 8;
			}
		    }

		  if (PREDICT_TRUE (encap != NULL))
		    {
		      if (ls_param->nhtype == SRV6_NHTYPE_NONE)
			{
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) == 6)
			    ip6srv->sr.protocol = IP_PROTOCOL_IPV6;
			  else
			    ip6srv->sr.protocol = IP_PROTOCOL_IP_IN_IP;
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_IPV4)
			{
			  ip6srv->sr.protocol = IP_PROTOCOL_IP_IN_IP;
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 4)
			    {
			      // Bad encap packet.
			      next0 = SRV6_END_M_GTP6_D_NEXT_DROP;
			      bad_n++;
			      goto DONE;
			    }
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_IPV6)
			{
			  ip6srv->sr.protocol = IP_PROTOCOL_IPV6;
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 6)
			    {
			      // Bad encap packet.
			      next0 = SRV6_END_M_GTP6_D_NEXT_DROP;
			      bad_n++;
			      goto DONE;
			    }
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_NON_IP)
			{
			  ip6srv->sr.protocol = IP_PROTOCOL_IP6_ETHERNET;
			}
		    }
		  else
		    {
		      ip6srv->sr.protocol = IP_PROTOCOL_IP6_ETHERNET;
		    }
		}
	      else
		{
		  clib_memcpy_fast (ip6srv, &sm->cache_hdr,
				    sizeof (ip6_header_t));

		  ip6srv->ip.src_address = src0;
		  ip6srv->ip.dst_address = seg0;

		  if (PREDICT_FALSE (gtpu_type) != GTPU_TYPE_GTPU)
		    {
		      ip6srv->ip.protocol = IP_PROTOCOL_IPV6_ROUTE;

		      ip6srv->sr.protocol = IP_PROTOCOL_IP6_ETHERNET;

		      ip6srv->sr.tag =
			clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		      ip6srv->sr.segments_left = 0;
		      ip6srv->sr.last_entry = 0;

		      ip6srv->sr.length = sizeof (ip6_address_t) / 8;
		      ip6srv->sr.segments[0] = seg0;
		    }
		  else
		    {
		      if (ls_param->nhtype == SRV6_NHTYPE_NONE)
			{
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 6)
			    ip6srv->ip.protocol = IP_PROTOCOL_IP_IN_IP;
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_IPV4)
			{
			  ip6srv->ip.protocol = IP_PROTOCOL_IP_IN_IP;
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 4)
			    {
			      // Bad encap packet.
			      next0 = SRV6_END_M_GTP6_D_NEXT_DROP;
			      bad_n++;
			      goto DONE;
			    }
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_IPV6)
			{
			  ip6srv->ip.protocol = IP_PROTOCOL_IPV6;
			  if ((clib_net_to_host_u32
			       (encap->ip_version_traffic_class_and_flow_label)
			       >> 28) != 6)
			    {
			      // Bad encap packet.
			      next0 = SRV6_END_M_GTP6_D_NEXT_DROP;
			      bad_n++;
			      goto DONE;
			    }
			}
		      else if (ls_param->nhtype == SRV6_NHTYPE_NON_IP)
			{
			  ip6srv->ip.protocol = IP_PROTOCOL_IP6_ETHERNET;
			}
		    }
		}

	      if (PREDICT_FALSE (ie_size))
		{
		  ip6_sr_tlv_t *tlv;
		  user_plane_sub_tlv_t *sub_tlv;

		  tlv =
		    (ip6_sr_tlv_t *) ((u8 *) ip6srv + (hdr_len - tlv_siz));
		  tlv->type = SRH_TLV_USER_PLANE_CONTAINER;
		  tlv->length = (u8) (tlv_siz - sizeof (ip6_sr_tlv_t));
		  clib_memset (tlv->value, 0, tlv->length);

		  sub_tlv = (user_plane_sub_tlv_t *) tlv->value;
		  sub_tlv->type = USER_PLANE_SUB_TLV_IE;
		  sub_tlv->length = (u8) ie_size;
		  clib_memcpy_fast (sub_tlv->value, ie_buf, ie_size);

		  ip6srv->sr.length += (u8) (tlv_siz / 8);
		}

	      ip6srv->ip.payload_length =
		clib_host_to_net_u16 (len0 + hdr_len - sizeof (ip6_header_t));

	      good_n++;

	      if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
		  PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  srv6_end_rewrite_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  clib_memcpy (tr->src.as_u8, ip6srv->ip.src_address.as_u8,
			       sizeof (ip6_address_t));
		  clib_memcpy (tr->dst.as_u8, ip6srv->ip.dst_address.as_u8,
			       sizeof (ip6_address_t));
		  tr->teid = teid;
		  clib_memcpy (tr->sr_prefix.as_u8, ls_param->sr_prefix.as_u8,
			       sizeof (ip6_address_t));
		  tr->sr_prefixlen = ls_param->sr_prefixlen;
		}
	    }

	DONE:
	  vlib_increment_combined_counter
	    (((next0 ==
	       SRV6_END_M_GTP6_D_NEXT_DROP) ? &(sm2->sr_ls_invalid_counters) :
	      &(sm2->sr_ls_valid_counters)), thread_index,
	     ls0 - sm2->localsids, 1, vlib_buffer_length_in_chain (vm, b0));

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, sm->end_m_gtp6_d_node_index,
			       SRV6_END_ERROR_M_GTP6_D_BAD_PACKETS, bad_n);

  vlib_node_increment_counter (vm, sm->end_m_gtp6_d_node_index,
			       SRV6_END_ERROR_M_GTP6_D_PACKETS, good_n);

  return frame->n_vectors;
}

// Function for SRv6 GTP6.D.DI function
VLIB_NODE_FN (srv6_end_m_gtp6_d_di) (vlib_main_t * vm,
				     vlib_node_runtime_t * node,
				     vlib_frame_t * frame)
{
  srv6_end_main_v6_decap_di_t *sm = &srv6_end_main_v6_decap_di;
  ip6_sr_main_t *sm2 = &sr_main;
  u32 n_left_from, next_index, *from, *to_next;
  u32 thread_index = vm->thread_index;
  srv6_end_gtp6_param_t *ls_param;

  u32 good_n = 0, bad_n = 0;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  ip6_sr_localsid_t *ls0;

	  ip6_gtpu_header_t *hdr0 = NULL;
	  uword len0;

	  ip6_address_t dst0;
	  ip6_address_t src0;
	  ip6_address_t seg0;
	  u32 teid = 0;
	  u8 *teidp;
	  u8 gtpu_type = 0;
	  u8 qfi = 0;
	  u8 *qfip = NULL;
	  u16 seq = 0;
	  u8 *seqp;
	  u32 offset, shift;
	  u32 hdrlen;
	  ip6_header_t *encap = NULL;
	  gtpu_pdu_session_t *sess;
	  int ie_size = 0;
	  u16 tlv_siz = 0;
	  u8 ie_buf[GTPU_IE_MAX_SIZ];

	  u32 next0 = SRV6_END_M_GTP6_D_DI_NEXT_LOOKUP;

	  // defaults
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  ls0 =
	    pool_elt_at_index (sm2->localsids,
			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);

	  ls_param = (srv6_end_gtp6_param_t *) ls0->plugin_mem;

	  hdr0 = vlib_buffer_get_current (b0);

	  hdrlen = sizeof (ip6_gtpu_header_t);

	  len0 = vlib_buffer_length_in_chain (vm, b0);

	  if ((hdr0->ip6.protocol != IP_PROTOCOL_UDP)
	      || (hdr0->udp.dst_port !=
		  clib_host_to_net_u16 (SRV6_GTP_UDP_DST_PORT))
	      || (len0 < sizeof (ip6_gtpu_header_t)))
	    {
	      next0 = SRV6_END_M_GTP6_D_DI_NEXT_DROP;

	      bad_n++;
	    }
	  else
	    {
	      dst0 = hdr0->ip6.dst_address;
	      src0 = hdr0->ip6.src_address;

	      gtpu_type = hdr0->gtpu.type;

	      seg0 = ls_param->sr_prefix;
	      teid = hdr0->gtpu.teid;
	      teidp = (u8 *) & teid;

	      seqp = (u8 *) & seq;

	      if (hdr0->gtpu.ver_flags & (GTPU_EXTHDR_FLAG | GTPU_SEQ_FLAG))
		{
		  // Extention header.
		  hdrlen += sizeof (gtpu_exthdr_t);

		  seq = hdr0->gtpu.ext->seq;

		  if (hdr0->gtpu.ext->nextexthdr == GTPU_EXTHDR_PDU_SESSION)
		    {
		      // PDU Session Container.
		      sess =
			(gtpu_pdu_session_t *) (((char *) hdr0) + hdrlen);
		      qfi = sess->u.val & ~GTPU_PDU_SESSION_P_BIT_MASK;
		      qfip = &qfi;

		      hdrlen += sizeof (gtpu_pdu_session_t);

		      if (sess->u.val & GTPU_PDU_SESSION_P_BIT_MASK)
			{
			  hdrlen += sizeof (gtpu_paging_policy_t);
			}
		    }
		}

	      offset = ls_param->sr_prefixlen / 8;
	      shift = ls_param->sr_prefixlen % 8;

	      offset += 1;
	      if (PREDICT_TRUE (shift == 0))
		{
		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      clib_memcpy_fast (&seg0.as_u8[offset], seqp, 2);
		    }
		  else
		    {
		      clib_memcpy_fast (&seg0.as_u8[offset], teidp, 4);
		    }

		  if (qfip)
		    {
		      qfi =
			((qfi & GTPU_PDU_SESSION_QFI_MASK) << 2) |
			((qfi & GTPU_PDU_SESSION_R_BIT_MASK) >> 5);

		      if (sess->type)
			{
			  qfi |= SRV6_PDU_SESSION_U_BIT_MASK;
			}

		      seg0.as_u8[offset + 4] = qfi;
		    }
		}
	      else
		{
		  int idx;

		  if (gtpu_type == GTPU_TYPE_ECHO_REQUEST
		      || gtpu_type == GTPU_TYPE_ECHO_REPLY
		      || gtpu_type == GTPU_TYPE_ERROR_INDICATION)
		    {
		      for (idx = 0; idx < 2; idx++)
			{
			  seg0.as_u8[offset + idx] |= seqp[idx] >> shift;
			  seg0.as_u8[offset + idx + 1] |=
			    seqp[idx] << (8 - shift);
			}
		    }
		  else
		    {
		      for (idx = 0; idx < 4; idx++)
			{
			  seg0.as_u8[offset + idx] |= teidp[idx] >> shift;
			  seg0.as_u8[offset + idx + 1] |=
			    teidp[idx] << (8 - shift);
			}
		    }

		  if (qfip)
		    {
		      qfi =
			((qfi & GTPU_PDU_SESSION_QFI_MASK) << 2) |
			((qfi & GTPU_PDU_SESSION_R_BIT_MASK) >> 5);

		      if (sess->type)
			{
			  qfi |= SRV6_PDU_SESSION_U_BIT_MASK;
			}

		      seg0.as_u8[offset + 4] |= qfi >> shift;
		      seg0.as_u8[offset + 5] |= qfi << (8 - shift);
		    }
		}

	      if (PREDICT_FALSE (gtpu_type == GTPU_TYPE_ERROR_INDICATION))
		{
		  u16 payload_len;

		  payload_len = clib_net_to_host_u16 (hdr0->gtpu.length);
		  if (payload_len != 0)
		    {
		      ie_size =
			payload_len - (hdrlen - sizeof (ip6_gtpu_header_t));
		      if (ie_size > 0)
			{
			  u8 *ies;

			  ies = (u8 *) ((u8 *) hdr0 + hdrlen);
			  clib_memcpy_fast (ie_buf, ies, ie_size);
			  hdrlen += ie_size;
			}
		    }
		}

	      // jump over variable length data
	      vlib_buffer_advance (b0, (word) hdrlen);

	      // get length of encapsulated IPv6 packet (the remaining part)
	      len0 = vlib_buffer_length_in_chain (vm, b0);

	      if (PREDICT_TRUE (gtpu_type == GTPU_TYPE_GTPU))
		{
		  encap = vlib_buffer_get_current (b0);
		}

	      uword *p;
	      ip6srv_combo_header_t *ip6srv;
	      ip6_sr_policy_t *sr_policy = NULL;
	      ip6_sr_sl_t *sl = NULL;
	      u32 *sl_index;
	      u32 hdr_len;

	      p =
		mhash_get (&sm2->sr_policies_index_hash,
			   &ls_param->sr_prefix);
	      if (p)
		{
		  sr_policy = pool_elt_at_index (sm2->sr_policies, p[0]);
		}

	      if (sr_policy)
		{
		  vec_foreach (sl_index, sr_policy->segments_lists)
		  {
		    sl = pool_elt_at_index (sm2->sid_lists, *sl_index);
		    if (sl != NULL)
		      break;
		  }
		}

	      hdr_len = sizeof (ip6srv_combo_header_t);

	      if (sl)
		hdr_len += vec_len (sl->segments) * sizeof (ip6_address_t);

	      hdr_len += sizeof (ip6_address_t) * 2;

	      if (ie_size)
		{
		  tlv_siz =
		    sizeof (ip6_sr_tlv_t) + sizeof (user_plane_sub_tlv_t) +
		    ie_size;

		  tlv_siz = (tlv_siz & ~0x07) + (tlv_siz & 0x07 ? 0x08 : 0x0);
		  hdr_len += tlv_siz;
		}

	      // jump back to data[0] or pre_data if required
	      vlib_buffer_advance (b0, -(word) hdr_len);

	      ip6srv = vlib_buffer_get_current (b0);

	      if (sl)
		{
		  clib_memcpy_fast (ip6srv, sl->rewrite,
				    vec_len (sl->rewrite));

		  if (vec_len (sl->segments) > 1)
		    {
		      ip6srv->ip.src_address = src0;

		      ip6srv->sr.tag =
			clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		      ip6srv->sr.segments_left += 2;
		      ip6srv->sr.last_entry += 2;

		      ip6srv->sr.length += ((sizeof (ip6_address_t) * 2) / 8);

		      ip6srv->sr.segments[0] = dst0;
		      ip6srv->sr.segments[1] = seg0;

		      clib_memcpy_fast (&ip6srv->sr.segments[2],
					(u8 *) (sl->rewrite +
						sizeof (ip6_header_t) +
						sizeof (ip6_sr_header_t)),
					vec_len (sl->segments) *
					sizeof (ip6_address_t));
		    }
		  else
		    {
		      ip6srv->ip.src_address = src0;
		      ip6srv->ip.protocol = IP_PROTOCOL_IPV6_ROUTE;

		      ip6srv->sr.type = ROUTING_HEADER_TYPE_SR;
		      ip6srv->sr.segments_left = 2;
		      ip6srv->sr.last_entry = 1;
		      ip6srv->sr.length =
			((sizeof (ip6_sr_header_t) +
			  2 * sizeof (ip6_address_t)) / 8) - 1;
		      ip6srv->sr.flags = 0;

		      ip6srv->sr.tag =
			clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		      ip6srv->sr.segments[0] = dst0;
		      ip6srv->sr.segments[1] = seg0;

		      if (vec_len (sl->segments))
			{
			  ip6srv->sr.segments[2] = sl->segments[0];
			  ip6srv->sr.last_entry++;
			  ip6srv->sr.length += sizeof (ip6_address_t) / 8;
			}
		    }
		}
	      else
		{
		  clib_memcpy_fast (ip6srv, &sm->cache_hdr,
				    sizeof (ip6_header_t));

		  ip6srv->ip.src_address = src0;
		  ip6srv->ip.dst_address = seg0;

		  ip6srv->sr.type = ROUTING_HEADER_TYPE_SR;
		  ip6srv->sr.segments_left = 1;
		  ip6srv->sr.last_entry = 0;
		  ip6srv->sr.length =
		    ((sizeof (ip6_sr_header_t) +
		      sizeof (ip6_address_t)) / 8) - 1;
		  ip6srv->sr.flags = 0;

		  ip6srv->sr.tag =
		    clib_host_to_net_u16 (srh_tagfield[gtpu_type]);

		  ip6srv->sr.segments[0] = dst0;
		}

	      if (PREDICT_FALSE (ie_size))
		{
		  ip6_sr_tlv_t *tlv;
		  user_plane_sub_tlv_t *sub_tlv;

		  tlv =
		    (ip6_sr_tlv_t *) ((u8 *) ip6srv + (hdr_len - tlv_siz));
		  tlv->type = SRH_TLV_USER_PLANE_CONTAINER;
		  tlv->length = (u8) (tlv_siz - sizeof (ip6_sr_tlv_t));
		  clib_memset (tlv->value, 0, tlv->length);

		  sub_tlv = (user_plane_sub_tlv_t *) tlv->value;
		  sub_tlv->length = (u8) (ie_size);
		  clib_memcpy_fast (sub_tlv->value, ie_buf, ie_size);

		  ip6srv->sr.length += (u8) (tlv_siz / 8);
		}

	      ip6srv->ip.payload_length =
		clib_host_to_net_u16 (len0 + hdr_len - sizeof (ip6_header_t));
	      ip6srv->ip.protocol = IP_PROTOCOL_IPV6_ROUTE;

	      if (PREDICT_TRUE (encap != NULL))
		{
		  if (ls_param->nhtype == SRV6_NHTYPE_NONE)
		    {
		      if ((clib_net_to_host_u32
			   (encap->ip_version_traffic_class_and_flow_label) >>
			   28) == 6)
			ip6srv->sr.protocol = IP_PROTOCOL_IPV6;
		      else
			ip6srv->sr.protocol = IP_PROTOCOL_IP_IN_IP;
		    }
		  else if (ls_param->nhtype == SRV6_NHTYPE_IPV4)
		    {
		      ip6srv->sr.protocol = IP_PROTOCOL_IP_IN_IP;
		      if ((clib_net_to_host_u32
			   (encap->ip_version_traffic_class_and_flow_label) >>
			   28) != 4)
			{
			  // Bad encap packet.
			  next0 = SRV6_END_M_GTP6_D_DI_NEXT_DROP;
			  bad_n++;
			  goto DONE;
			}
		    }
		  else if (ls_param->nhtype == SRV6_NHTYPE_IPV6)
		    {
		      ip6srv->sr.protocol = IP_PROTOCOL_IPV6;
		      if ((clib_net_to_host_u32
			   (encap->ip_version_traffic_class_and_flow_label) >>
			   28) != 6)
			{
			  // Bad encap packet.
			  next0 = SRV6_END_M_GTP6_D_DI_NEXT_DROP;
			  bad_n++;
			  goto DONE;
			}
		    }
		  else if (ls_param->nhtype == SRV6_NHTYPE_NON_IP)
		    {
		      ip6srv->sr.protocol = IP_PROTOCOL_IP6_ETHERNET;
		    }
		}
	      else
		{
		  ip6srv->sr.protocol = IP_PROTOCOL_IP6_ETHERNET;
		}

	      good_n++;

	      if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
		  PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  srv6_end_rewrite_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  clib_memcpy (tr->src.as_u8, ip6srv->ip.src_address.as_u8,
			       sizeof (ip6_address_t));
		  clib_memcpy (tr->dst.as_u8, ip6srv->ip.dst_address.as_u8,
			       sizeof (ip6_address_t));
		  tr->teid = teid;
		  clib_memcpy (tr->sr_prefix.as_u8, ls_param->sr_prefix.as_u8,
			       sizeof (ip6_address_t));
		  tr->sr_prefixlen = ls_param->sr_prefixlen;
		}
	    }

	DONE:
	  vlib_increment_combined_counter
	    (((next0 ==
	       SRV6_END_M_GTP6_D_DI_NEXT_DROP) ?
	      &(sm2->sr_ls_invalid_counters) : &(sm2->sr_ls_valid_counters)),
	     thread_index, ls0 - sm2->localsids, 1,
	     vlib_buffer_length_in_chain (vm, b0));

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, sm->end_m_gtp6_d_di_node_index,
			       SRV6_END_ERROR_M_GTP6_D_DI_BAD_PACKETS, bad_n);

  vlib_node_increment_counter (vm, sm->end_m_gtp6_d_di_node_index,
			       SRV6_END_ERROR_M_GTP6_D_DI_PACKETS, good_n);

  return frame->n_vectors;
}

// Function for SRv6 GTP6.DT function
VLIB_NODE_FN (srv6_end_m_gtp6_dt) (vlib_main_t * vm,
				   vlib_node_runtime_t * node,
				   vlib_frame_t * frame)
{
  srv6_end_main_v6_dt_t *sm = &srv6_end_main_v6_dt;
  ip6_sr_main_t *sm2 = &sr_main;
  u32 n_left_from, next_index, *from, *to_next;
  u32 thread_index = vm->thread_index;

  u32 good_n = 0, bad_n = 0;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  srv6_end_gtp6_dt_param_t *ls_param;
	  ip6_sr_localsid_t *ls0;

	  ip6_gtpu_header_t *hdr0 = NULL;
	  ip4_header_t *ip4 = NULL;
	  ip6_header_t *ip6 = NULL;
	  ip6_address_t src, dst;
	  u32 teid;
	  u32 hdrlen;
	  u32 len0;

	  u32 next0 = SRV6_END_M_GTP6_DT_NEXT_DROP;

	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  ls0 =
	    pool_elt_at_index (sm2->localsids,
			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);

	  ls_param = (srv6_end_gtp6_dt_param_t *) ls0->plugin_mem;

	  hdr0 = vlib_buffer_get_current (b0);

	  hdrlen = sizeof (ip6_gtpu_header_t);

	  len0 = vlib_buffer_length_in_chain (vm, b0);

	  if ((hdr0->ip6.protocol != IP_PROTOCOL_UDP)
	      || (hdr0->udp.dst_port !=
		  clib_host_to_net_u16 (SRV6_GTP_UDP_DST_PORT))
	      || (len0 < sizeof (ip6_gtpu_header_t)))
	    {
	      next0 = SRV6_END_M_GTP6_DT_NEXT_DROP;

	      bad_n++;
	    }
	  else
	    {
	      clib_memcpy_fast (src.as_u8, hdr0->ip6.src_address.as_u8,
				sizeof (ip6_address_t));
	      clib_memcpy_fast (dst.as_u8, hdr0->ip6.dst_address.as_u8,
				sizeof (ip6_address_t));

	      teid = hdr0->gtpu.teid;

	      if (hdr0->gtpu.ver_flags & GTPU_EXTHDR_FLAG)
		{
		  hdrlen += sizeof (gtpu_exthdr_t);
		  if (hdr0->gtpu.ext->nextexthdr == GTPU_EXTHDR_PDU_SESSION)
		    {
		      gtpu_pdu_session_t *sess;

		      sess =
			(gtpu_pdu_session_t *) (((char *) hdr0) +
						sizeof (ip6_gtpu_header_t) +
						sizeof (gtpu_exthdr_t));

		      hdrlen += sizeof (gtpu_pdu_session_t);
		      if (sess->u.val & GTPU_PDU_SESSION_P_BIT_MASK)
			{
			  hdrlen += sizeof (gtpu_paging_policy_t);
			}
		    }
		}

	      if (ls_param->type == SRV6_GTP6_DT4)
		{
		  vlib_buffer_advance (b0, (word) hdrlen);
		  ip4 = vlib_buffer_get_current (b0);
		  if ((ip4->ip_version_and_header_length & 0xf0) != 0x40)
		    {
		      next0 = SRV6_END_M_GTP6_DT_NEXT_DROP;
		      bad_n++;
		      goto DONE;
		    }

		  next0 = SRV6_END_M_GTP6_DT_NEXT_LOOKUP4;
		  vnet_buffer (b0)->sw_if_index[VLIB_TX] =
		    ls_param->fib4_index;
		}
	      else if (ls_param->type == SRV6_GTP6_DT6)
		{
		  ip6 = (ip6_header_t *) ((u8 *) hdr0 + hdrlen);
		  if ((clib_net_to_host_u32
		       (ip6->ip_version_traffic_class_and_flow_label) >> 28)
		      != 6)
		    {
		      next0 = SRV6_END_M_GTP6_DT_NEXT_DROP;
		      bad_n++;
		      goto DONE;
		    }

		  next0 = SRV6_END_M_GTP6_DT_NEXT_LOOKUP6;
		  if ((ip6->dst_address.as_u8[0] == 0xff)
		      && ((ip6->dst_address.as_u8[1] & 0xc0) == 0x80))
		    {
		      vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			ls_param->local_fib_index;
		    }
		  else
		    {
		      vlib_buffer_advance (b0, (word) hdrlen);
		      vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			ls_param->fib6_index;
		    }
		}
	      else if (ls_param->type == SRV6_GTP6_DT46)
		{
		  ip6 = (ip6_header_t *) ((u8 *) hdr0 + hdrlen);
		  if ((clib_net_to_host_u32
		       (ip6->ip_version_traffic_class_and_flow_label) >> 28)
		      == 6)
		    {
		      next0 = SRV6_END_M_GTP6_DT_NEXT_LOOKUP6;
		      if ((ip6->dst_address.as_u8[0] == 0xff)
			  && ((ip6->dst_address.as_u8[1] & 0xc0) == 0x80))
			{
			  vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			    ls_param->local_fib_index;
			}
		      else
			{
			  vlib_buffer_advance (b0, (word) hdrlen);
			  vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			    ls_param->fib6_index;
			}
		    }
		  else
		    if ((clib_net_to_host_u32
			 (ip6->ip_version_traffic_class_and_flow_label) >> 28)
			== 4)
		    {
		      vlib_buffer_advance (b0, (word) hdrlen);
		      next0 = SRV6_END_M_GTP6_DT_NEXT_LOOKUP4;
		      vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			ls_param->fib4_index;
		    }
		  else
		    {
		      next0 = SRV6_END_M_GTP6_DT_NEXT_DROP;
		      bad_n++;
		      goto DONE;
		    }
		}
	      else
		{
		  next0 = SRV6_END_M_GTP6_DT_NEXT_DROP;
		  bad_n++;
		  goto DONE;
		}

	      good_n++;

	      if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
		  PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  srv6_end_rewrite_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  clib_memcpy (tr->src.as_u8, src.as_u8,
			       sizeof (ip6_address_t));
		  clib_memcpy (tr->dst.as_u8, dst.as_u8,
			       sizeof (ip6_address_t));
		  tr->teid = teid;
		}
	    }

	DONE:
	  vlib_increment_combined_counter
	    (((next0 ==
	       SRV6_END_M_GTP6_DT_NEXT_DROP) ? &(sm2->sr_ls_invalid_counters)
	      : &(sm2->sr_ls_valid_counters)), thread_index,
	     ls0 - sm2->localsids, 1, vlib_buffer_length_in_chain (vm, b0));

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, sm->end_m_gtp6_dt_node_index,
			       SRV6_END_ERROR_M_GTP6_DT_BAD_PACKETS, bad_n);

  vlib_node_increment_counter (vm, sm->end_m_gtp6_dt_node_index,
			       SRV6_END_ERROR_M_GTP6_DT_PACKETS, good_n);

  return frame->n_vectors;
}

// Function for SRv6 GTP4.DT function
VLIB_NODE_FN (srv6_t_m_gtp4_dt) (vlib_main_t * vm,
				 vlib_node_runtime_t * node,
				 vlib_frame_t * frame)
{
  srv6_t_main_v4_dt_t *sm = &srv6_t_main_v4_dt;
  ip6_sr_main_t *sm2 = &sr_main;
  u32 n_left_from, next_index, *from, *to_next;

  u32 good_n = 0, bad_n = 0;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  srv6_t_gtp4_dt_param_t *ls_param;
	  ip6_sr_sl_t *ls0;

	  ip4_gtpu_header_t *hdr0 = NULL;
	  ip4_header_t *ip4 = NULL;
	  ip6_header_t *ip6 = NULL;
	  ip6_address_t src, dst;
	  u32 teid;
	  u32 hdrlen;
	  u32 len0;

	  u32 next0 = SRV6_T_M_GTP4_DT_NEXT_DROP;

	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  ls0 =
	    pool_elt_at_index (sm2->sid_lists,
			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);

	  ls_param = (srv6_t_gtp4_dt_param_t *) ls0->plugin_mem;

	  hdr0 = vlib_buffer_get_current (b0);

	  hdrlen = sizeof (ip4_gtpu_header_t);

	  len0 = vlib_buffer_length_in_chain (vm, b0);

	  if ((hdr0->ip4.protocol != IP_PROTOCOL_UDP)
	      || (hdr0->udp.dst_port !=
		  clib_host_to_net_u16 (SRV6_GTP_UDP_DST_PORT))
	      || (len0 < sizeof (ip4_gtpu_header_t)))
	    {
	      next0 = SRV6_T_M_GTP4_DT_NEXT_DROP;

	      bad_n++;
	    }
	  else
	    {
	      clib_memcpy_fast (src.as_u8, hdr0->ip4.src_address.as_u8,
				sizeof (ip4_address_t));
	      clib_memcpy_fast (dst.as_u8, hdr0->ip4.dst_address.as_u8,
				sizeof (ip4_address_t));

	      teid = hdr0->gtpu.teid;

	      if (hdr0->gtpu.ver_flags & GTPU_EXTHDR_FLAG)
		{
		  hdrlen += sizeof (gtpu_exthdr_t);
		  if (hdr0->gtpu.ext->nextexthdr == GTPU_EXTHDR_PDU_SESSION)
		    {
		      gtpu_pdu_session_t *sess;

		      sess =
			(gtpu_pdu_session_t *) (((char *) hdr0) +
						sizeof (ip6_gtpu_header_t) +
						sizeof (gtpu_exthdr_t));

		      hdrlen += sizeof (gtpu_pdu_session_t);
		      if (sess->u.val & GTPU_PDU_SESSION_P_BIT_MASK)
			{
			  hdrlen += sizeof (gtpu_paging_policy_t);
			}
		    }
		}

	      if (ls_param->type == SRV6_GTP4_DT4)
		{
		  vlib_buffer_advance (b0, (word) hdrlen);
		  ip4 = vlib_buffer_get_current (b0);
		  if ((ip4->ip_version_and_header_length & 0xf0) != 0x40)
		    {
		      next0 = SRV6_T_M_GTP4_DT_NEXT_DROP;
		      bad_n++;
		      goto DONE;
		    }

		  next0 = SRV6_T_M_GTP4_DT_NEXT_LOOKUP4;
		  vnet_buffer (b0)->sw_if_index[VLIB_TX] =
		    ls_param->fib4_index;
		}
	      else if (ls_param->type == SRV6_GTP4_DT6)
		{
		  ip6 = (ip6_header_t *) ((u8 *) hdr0 + hdrlen);
		  if ((clib_net_to_host_u32
		       (ip6->ip_version_traffic_class_and_flow_label) >> 28)
		      != 6)
		    {
		      next0 = SRV6_T_M_GTP4_DT_NEXT_DROP;
		      bad_n++;
		      goto DONE;
		    }

		  next0 = SRV6_T_M_GTP4_DT_NEXT_LOOKUP6;
		  if ((ip6->dst_address.as_u8[0] == 0xff)
		      && ((ip6->dst_address.as_u8[1] & 0xc0) == 0x80))
		    {
		      next0 = SRV6_T_M_GTP4_DT_NEXT_LOOKUP4;
		      vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			ls_param->local_fib_index;
		    }
		  else
		    {
		      vlib_buffer_advance (b0, (word) hdrlen);
		      vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			ls_param->fib6_index;
		    }
		}
	      else if (ls_param->type == SRV6_GTP4_DT46)
		{
		  ip6 = (ip6_header_t *) ((u8 *) hdr0 + hdrlen);
		  if ((clib_net_to_host_u32
		       (ip6->ip_version_traffic_class_and_flow_label) >> 28)
		      == 6)
		    {
		      next0 = SRV6_T_M_GTP4_DT_NEXT_LOOKUP6;
		      if ((ip6->dst_address.as_u8[0] == 0xff)
			  && ((ip6->dst_address.as_u8[1] & 0xc0) == 0x80))
			{
			  next0 = SRV6_T_M_GTP4_DT_NEXT_LOOKUP4;
			  vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			    ls_param->local_fib_index;
			}
		      else
			{
			  vlib_buffer_advance (b0, (word) hdrlen);
			  vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			    ls_param->fib6_index;
			}
		    }
		  else
		    if ((clib_net_to_host_u32
			 (ip6->ip_version_traffic_class_and_flow_label) >> 28)
			== 4)
		    {
		      vlib_buffer_advance (b0, (word) hdrlen);
		      next0 = SRV6_T_M_GTP4_DT_NEXT_LOOKUP4;
		      vnet_buffer (b0)->sw_if_index[VLIB_TX] =
			ls_param->fib4_index;
		    }
		  else
		    {
		      next0 = SRV6_T_M_GTP4_DT_NEXT_DROP;
		      bad_n++;
		      goto DONE;
		    }
		}
	      else
		{
		  next0 = SRV6_T_M_GTP4_DT_NEXT_DROP;
		  bad_n++;
		  goto DONE;
		}

	      good_n++;

	      if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
		  PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		{
		  srv6_end_rewrite_trace_t *tr =
		    vlib_add_trace (vm, node, b0, sizeof (*tr));
		  clib_memcpy (tr->src.as_u8, src.as_u8,
			       sizeof (ip6_address_t));
		  clib_memcpy (tr->dst.as_u8, dst.as_u8,
			       sizeof (ip6_address_t));
		  tr->teid = teid;
		}
	    }

	DONE:
	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, sm->t_m_gtp4_dt_node_index,
			       SRV6_T_ERROR_M_GTP4_DT_BAD_PACKETS, bad_n);

  vlib_node_increment_counter (vm, sm->t_m_gtp4_dt_node_index,
			       SRV6_T_ERROR_M_GTP4_DT_PACKETS, good_n);

  return frame->n_vectors;
}

VLIB_REGISTER_NODE (srv6_end_m_gtp6_e) =
{
  .name = "srv6-end-m-gtp6-e",.vector_size = sizeof (u32),.format_trace =
    format_srv6_end_rewrite_trace6,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARRAY_LEN (srv6_end_error_v6_e_strings),.error_strings =
    srv6_end_error_v6_e_strings,.n_next_nodes =
    SRV6_END_M_GTP6_E_N_NEXT,.next_nodes =
  {
  [SRV6_END_M_GTP6_E_NEXT_DROP] =
      "error-drop",[SRV6_END_M_GTP6_E_NEXT_LOOKUP] = "ip6-lookup",}
,};

VLIB_REGISTER_NODE (srv6_end_m_gtp6_d) =
{
  .name = "srv6-end-m-gtp6-d",.vector_size = sizeof (u32),.format_trace =
    format_srv6_end_rewrite_trace6,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARRAY_LEN (srv6_end_error_v6_d_strings),.error_strings =
    srv6_end_error_v6_d_strings,.n_next_nodes =
    SRV6_END_M_GTP6_D_N_NEXT,.next_nodes =
  {
  [SRV6_END_M_GTP6_D_NEXT_DROP] =
      "error-drop",[SRV6_END_M_GTP6_D_NEXT_LOOKUP] = "ip6-lookup",}
,};

VLIB_REGISTER_NODE (srv6_end_m_gtp6_d_di) =
{
  .name = "srv6-end-m-gtp6-d-di",.vector_size = sizeof (u32),.format_trace =
    format_srv6_end_rewrite_trace6,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARRAY_LEN (srv6_end_error_v6_d_di_strings),.error_strings =
    srv6_end_error_v6_d_di_strings,.n_next_nodes =
    SRV6_END_M_GTP6_D_DI_N_NEXT,.next_nodes =
  {
  [SRV6_END_M_GTP6_D_DI_NEXT_DROP] = "error-drop",
      [SRV6_END_M_GTP6_D_DI_NEXT_LOOKUP] = "ip6-lookup",}
,};

VLIB_REGISTER_NODE (srv6_end_m_gtp6_dt) =
{
  .name = "srv6-end-m-gtp6-dt",.vector_size = sizeof (u32),.format_trace =
    format_srv6_end_rewrite_trace6,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARRAY_LEN (srv6_end_error_v6_dt_strings),.error_strings =
    srv6_end_error_v6_dt_strings,.n_next_nodes =
    SRV6_END_M_GTP6_DT_N_NEXT,.next_nodes =
  {
  [SRV6_END_M_GTP6_DT_NEXT_DROP] =
      "error-drop",
      [SRV6_END_M_GTP6_DT_NEXT_LOOKUP4]
      = "ip4-lookup",[SRV6_END_M_GTP6_DT_NEXT_LOOKUP6] = "ip6-lookup",}
,};

VLIB_REGISTER_NODE (srv6_t_m_gtp4_dt) =
{
  .name = "srv6-t-m-gtp4-dt",.vector_size = sizeof (u32),.format_trace =
    format_srv6_end_rewrite_trace6,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARRAY_LEN (srv6_t_error_v4_dt_strings),.error_strings =
    srv6_t_error_v4_dt_strings,.n_next_nodes =
    SRV6_T_M_GTP4_DT_N_NEXT,.next_nodes =
  {
  [SRV6_T_M_GTP4_DT_NEXT_DROP] =
      "error-drop",
      [SRV6_T_M_GTP4_DT_NEXT_LOOKUP4] =
      "ip4-lookup",[SRV6_T_M_GTP4_DT_NEXT_LOOKUP6] = "ip6-lookup",}
,};

/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/