lwt_bpf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
  3. */
  4. #include <linux/filter.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/types.h>
  9. #include <linux/bpf.h>
  10. #include <net/flow.h>
  11. #include <net/lwtunnel.h>
  12. #include <net/gre.h>
  13. #include <net/ip.h>
  14. #include <net/ip6_route.h>
  15. #include <net/ipv6_stubs.h>
  16. struct bpf_lwt_prog {
  17. struct bpf_prog *prog;
  18. char *name;
  19. };
  20. struct bpf_lwt {
  21. struct bpf_lwt_prog in;
  22. struct bpf_lwt_prog out;
  23. struct bpf_lwt_prog xmit;
  24. int family;
  25. };
  26. #define MAX_PROG_NAME 256
  27. static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt)
  28. {
  29. return (struct bpf_lwt *)lwt->data;
  30. }
  31. #define NO_REDIRECT false
  32. #define CAN_REDIRECT true
  33. static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
  34. struct dst_entry *dst, bool can_redirect)
  35. {
  36. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  37. int ret;
  38. /* Disabling BH is needed to protect per-CPU bpf_redirect_info between
  39. * BPF prog and skb_do_redirect().
  40. */
  41. local_bh_disable();
  42. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  43. bpf_compute_data_pointers(skb);
  44. ret = bpf_prog_run_save_cb(lwt->prog, skb);
  45. switch (ret) {
  46. case BPF_OK:
  47. case BPF_LWT_REROUTE:
  48. break;
  49. case BPF_REDIRECT:
  50. if (unlikely(!can_redirect)) {
  51. pr_warn_once("Illegal redirect return code in prog %s\n",
  52. lwt->name ? : "<unknown>");
  53. ret = BPF_OK;
  54. } else {
  55. skb_reset_mac_header(skb);
  56. skb_do_redirect(skb);
  57. ret = BPF_REDIRECT;
  58. }
  59. break;
  60. case BPF_DROP:
  61. kfree_skb(skb);
  62. ret = -EPERM;
  63. break;
  64. default:
  65. pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret);
  66. kfree_skb(skb);
  67. ret = -EINVAL;
  68. break;
  69. }
  70. bpf_net_ctx_clear(bpf_net_ctx);
  71. local_bh_enable();
  72. return ret;
  73. }
  74. static int bpf_lwt_input_reroute(struct sk_buff *skb)
  75. {
  76. enum skb_drop_reason reason;
  77. int err = -EINVAL;
  78. if (skb->protocol == htons(ETH_P_IP)) {
  79. struct net_device *dev = skb_dst(skb)->dev;
  80. const struct iphdr *iph = ip_hdr(skb);
  81. dev_hold(dev);
  82. skb_dst_drop(skb);
  83. reason = ip_route_input_noref(skb, iph->daddr, iph->saddr,
  84. ip4h_dscp(iph), dev);
  85. err = reason ? -EINVAL : 0;
  86. dev_put(dev);
  87. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  88. skb_dst_drop(skb);
  89. err = ipv6_stub->ipv6_route_input(skb);
  90. } else {
  91. err = -EAFNOSUPPORT;
  92. }
  93. if (err)
  94. goto err;
  95. return dst_input(skb);
  96. err:
  97. kfree_skb(skb);
  98. return err;
  99. }
  100. static int bpf_input(struct sk_buff *skb)
  101. {
  102. struct dst_entry *dst = skb_dst(skb);
  103. struct bpf_lwt *bpf;
  104. int ret;
  105. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  106. if (bpf->in.prog) {
  107. ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
  108. if (ret < 0)
  109. return ret;
  110. if (ret == BPF_LWT_REROUTE)
  111. return bpf_lwt_input_reroute(skb);
  112. }
  113. if (unlikely(!dst->lwtstate->orig_input)) {
  114. kfree_skb(skb);
  115. return -EINVAL;
  116. }
  117. return dst->lwtstate->orig_input(skb);
  118. }
  119. static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  120. {
  121. struct dst_entry *dst = skb_dst(skb);
  122. struct bpf_lwt *bpf;
  123. int ret;
  124. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  125. if (bpf->out.prog) {
  126. ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
  127. if (ret < 0)
  128. return ret;
  129. }
  130. if (unlikely(!dst->lwtstate->orig_output)) {
  131. pr_warn_once("orig_output not set on dst for prog %s\n",
  132. bpf->out.name);
  133. kfree_skb(skb);
  134. return -EINVAL;
  135. }
  136. return dst->lwtstate->orig_output(net, sk, skb);
  137. }
  138. static int xmit_check_hhlen(struct sk_buff *skb, int hh_len)
  139. {
  140. if (skb_headroom(skb) < hh_len) {
  141. int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
  142. if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
  143. return -ENOMEM;
  144. }
  145. return 0;
  146. }
  147. static int bpf_lwt_xmit_reroute(struct sk_buff *skb)
  148. {
  149. struct net_device *l3mdev = l3mdev_master_dev_rcu(skb_dst(skb)->dev);
  150. int oif = l3mdev ? l3mdev->ifindex : 0;
  151. struct dst_entry *dst = NULL;
  152. int err = -EAFNOSUPPORT;
  153. struct sock *sk;
  154. struct net *net;
  155. bool ipv4;
  156. if (skb->protocol == htons(ETH_P_IP))
  157. ipv4 = true;
  158. else if (skb->protocol == htons(ETH_P_IPV6))
  159. ipv4 = false;
  160. else
  161. goto err;
  162. sk = sk_to_full_sk(skb->sk);
  163. if (sk) {
  164. if (sk->sk_bound_dev_if)
  165. oif = sk->sk_bound_dev_if;
  166. net = sock_net(sk);
  167. } else {
  168. net = dev_net(skb_dst(skb)->dev);
  169. }
  170. if (ipv4) {
  171. struct iphdr *iph = ip_hdr(skb);
  172. struct flowi4 fl4 = {};
  173. struct rtable *rt;
  174. fl4.flowi4_oif = oif;
  175. fl4.flowi4_mark = skb->mark;
  176. fl4.flowi4_uid = sock_net_uid(net, sk);
  177. fl4.flowi4_dscp = ip4h_dscp(iph);
  178. fl4.flowi4_flags = FLOWI_FLAG_ANYSRC;
  179. fl4.flowi4_proto = iph->protocol;
  180. fl4.daddr = iph->daddr;
  181. fl4.saddr = iph->saddr;
  182. rt = ip_route_output_key(net, &fl4);
  183. if (IS_ERR(rt)) {
  184. err = PTR_ERR(rt);
  185. goto err;
  186. }
  187. dst = &rt->dst;
  188. } else {
  189. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  190. struct flowi6 fl6 = {};
  191. fl6.flowi6_oif = oif;
  192. fl6.flowi6_mark = skb->mark;
  193. fl6.flowi6_uid = sock_net_uid(net, sk);
  194. fl6.flowlabel = ip6_flowinfo(iph6);
  195. fl6.flowi6_proto = iph6->nexthdr;
  196. fl6.daddr = iph6->daddr;
  197. fl6.saddr = iph6->saddr;
  198. dst = ipv6_stub->ipv6_dst_lookup_flow(net, skb->sk, &fl6, NULL);
  199. if (IS_ERR(dst)) {
  200. err = PTR_ERR(dst);
  201. goto err;
  202. }
  203. }
  204. if (unlikely(dst->error)) {
  205. err = dst->error;
  206. dst_release(dst);
  207. goto err;
  208. }
  209. /* Although skb header was reserved in bpf_lwt_push_ip_encap(), it
  210. * was done for the previous dst, so we are doing it here again, in
  211. * case the new dst needs much more space. The call below is a noop
  212. * if there is enough header space in skb.
  213. */
  214. err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
  215. if (unlikely(err))
  216. goto err;
  217. skb_dst_drop(skb);
  218. skb_dst_set(skb, dst);
  219. err = dst_output(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  220. if (unlikely(err))
  221. return net_xmit_errno(err);
  222. /* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */
  223. return LWTUNNEL_XMIT_DONE;
  224. err:
  225. kfree_skb(skb);
  226. return err;
  227. }
  228. static int bpf_xmit(struct sk_buff *skb)
  229. {
  230. struct dst_entry *dst = skb_dst(skb);
  231. struct bpf_lwt *bpf;
  232. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  233. if (bpf->xmit.prog) {
  234. int hh_len = dst->dev->hard_header_len;
  235. __be16 proto = skb->protocol;
  236. int ret;
  237. ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
  238. switch (ret) {
  239. case BPF_OK:
  240. /* If the header changed, e.g. via bpf_lwt_push_encap,
  241. * BPF_LWT_REROUTE below should have been used if the
  242. * protocol was also changed.
  243. */
  244. if (skb->protocol != proto) {
  245. kfree_skb(skb);
  246. return -EINVAL;
  247. }
  248. /* If the header was expanded, headroom might be too
  249. * small for L2 header to come, expand as needed.
  250. */
  251. ret = xmit_check_hhlen(skb, hh_len);
  252. if (unlikely(ret))
  253. return ret;
  254. return LWTUNNEL_XMIT_CONTINUE;
  255. case BPF_REDIRECT:
  256. return LWTUNNEL_XMIT_DONE;
  257. case BPF_LWT_REROUTE:
  258. return bpf_lwt_xmit_reroute(skb);
  259. default:
  260. return ret;
  261. }
  262. }
  263. return LWTUNNEL_XMIT_CONTINUE;
  264. }
  265. static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
  266. {
  267. if (prog->prog)
  268. bpf_prog_put(prog->prog);
  269. kfree(prog->name);
  270. }
  271. static void bpf_destroy_state(struct lwtunnel_state *lwt)
  272. {
  273. struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
  274. bpf_lwt_prog_destroy(&bpf->in);
  275. bpf_lwt_prog_destroy(&bpf->out);
  276. bpf_lwt_prog_destroy(&bpf->xmit);
  277. }
  278. static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
  279. [LWT_BPF_PROG_FD] = { .type = NLA_U32, },
  280. [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
  281. .len = MAX_PROG_NAME },
  282. };
  283. static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
  284. enum bpf_prog_type type)
  285. {
  286. struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
  287. struct bpf_prog *p;
  288. int ret;
  289. u32 fd;
  290. ret = nla_parse_nested_deprecated(tb, LWT_BPF_PROG_MAX, attr,
  291. bpf_prog_policy, NULL);
  292. if (ret < 0)
  293. return ret;
  294. if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
  295. return -EINVAL;
  296. prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC);
  297. if (!prog->name)
  298. return -ENOMEM;
  299. fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
  300. p = bpf_prog_get_type(fd, type);
  301. if (IS_ERR(p))
  302. return PTR_ERR(p);
  303. prog->prog = p;
  304. return 0;
  305. }
  306. static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
  307. [LWT_BPF_IN] = { .type = NLA_NESTED, },
  308. [LWT_BPF_OUT] = { .type = NLA_NESTED, },
  309. [LWT_BPF_XMIT] = { .type = NLA_NESTED, },
  310. [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 },
  311. };
  312. static int bpf_build_state(struct net *net, struct nlattr *nla,
  313. unsigned int family, const void *cfg,
  314. struct lwtunnel_state **ts,
  315. struct netlink_ext_ack *extack)
  316. {
  317. struct nlattr *tb[LWT_BPF_MAX + 1];
  318. struct lwtunnel_state *newts;
  319. struct bpf_lwt *bpf;
  320. int ret;
  321. if (family != AF_INET && family != AF_INET6)
  322. return -EAFNOSUPPORT;
  323. ret = nla_parse_nested_deprecated(tb, LWT_BPF_MAX, nla, bpf_nl_policy,
  324. extack);
  325. if (ret < 0)
  326. return ret;
  327. if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
  328. return -EINVAL;
  329. newts = lwtunnel_state_alloc(sizeof(*bpf));
  330. if (!newts)
  331. return -ENOMEM;
  332. newts->type = LWTUNNEL_ENCAP_BPF;
  333. bpf = bpf_lwt_lwtunnel(newts);
  334. if (tb[LWT_BPF_IN]) {
  335. newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
  336. ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
  337. BPF_PROG_TYPE_LWT_IN);
  338. if (ret < 0)
  339. goto errout;
  340. }
  341. if (tb[LWT_BPF_OUT]) {
  342. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
  343. ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
  344. BPF_PROG_TYPE_LWT_OUT);
  345. if (ret < 0)
  346. goto errout;
  347. }
  348. if (tb[LWT_BPF_XMIT]) {
  349. newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
  350. ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
  351. BPF_PROG_TYPE_LWT_XMIT);
  352. if (ret < 0)
  353. goto errout;
  354. }
  355. if (tb[LWT_BPF_XMIT_HEADROOM]) {
  356. u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
  357. if (headroom > LWT_BPF_MAX_HEADROOM) {
  358. ret = -ERANGE;
  359. goto errout;
  360. }
  361. newts->headroom = headroom;
  362. }
  363. bpf->family = family;
  364. *ts = newts;
  365. return 0;
  366. errout:
  367. bpf_destroy_state(newts);
  368. kfree(newts);
  369. return ret;
  370. }
  371. static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
  372. struct bpf_lwt_prog *prog)
  373. {
  374. struct nlattr *nest;
  375. if (!prog->prog)
  376. return 0;
  377. nest = nla_nest_start_noflag(skb, attr);
  378. if (!nest)
  379. return -EMSGSIZE;
  380. if (prog->name &&
  381. nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
  382. return -EMSGSIZE;
  383. return nla_nest_end(skb, nest);
  384. }
  385. static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
  386. {
  387. struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
  388. if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
  389. bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
  390. bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
  391. return -EMSGSIZE;
  392. return 0;
  393. }
  394. static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
  395. {
  396. int nest_len = nla_total_size(sizeof(struct nlattr)) +
  397. nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
  398. 0;
  399. return nest_len + /* LWT_BPF_IN */
  400. nest_len + /* LWT_BPF_OUT */
  401. nest_len + /* LWT_BPF_XMIT */
  402. 0;
  403. }
  404. static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
  405. {
  406. /* FIXME:
  407. * The LWT state is currently rebuilt for delete requests which
  408. * results in a new bpf_prog instance. Comparing names for now.
  409. */
  410. if (!a->name && !b->name)
  411. return 0;
  412. if (!a->name || !b->name)
  413. return 1;
  414. return strcmp(a->name, b->name);
  415. }
  416. static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  417. {
  418. struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
  419. struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
  420. return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
  421. bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
  422. bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
  423. }
  424. static const struct lwtunnel_encap_ops bpf_encap_ops = {
  425. .build_state = bpf_build_state,
  426. .destroy_state = bpf_destroy_state,
  427. .input = bpf_input,
  428. .output = bpf_output,
  429. .xmit = bpf_xmit,
  430. .fill_encap = bpf_fill_encap_info,
  431. .get_encap_size = bpf_encap_nlsize,
  432. .cmp_encap = bpf_encap_cmp,
  433. .owner = THIS_MODULE,
  434. };
  435. static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type,
  436. int encap_len)
  437. {
  438. struct skb_shared_info *shinfo = skb_shinfo(skb);
  439. gso_type |= SKB_GSO_DODGY;
  440. shinfo->gso_type |= gso_type;
  441. skb_decrease_gso_size(shinfo, encap_len);
  442. shinfo->gso_segs = 0;
  443. return 0;
  444. }
  445. static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len)
  446. {
  447. int next_hdr_offset;
  448. void *next_hdr;
  449. __u8 protocol;
  450. /* SCTP and UDP_L4 gso need more nuanced handling than what
  451. * handle_gso_type() does above: skb_decrease_gso_size() is not enough.
  452. * So at the moment only TCP GSO packets are let through.
  453. */
  454. if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
  455. return -ENOTSUPP;
  456. if (ipv4) {
  457. protocol = ip_hdr(skb)->protocol;
  458. next_hdr_offset = sizeof(struct iphdr);
  459. next_hdr = skb_network_header(skb) + next_hdr_offset;
  460. } else {
  461. protocol = ipv6_hdr(skb)->nexthdr;
  462. next_hdr_offset = sizeof(struct ipv6hdr);
  463. next_hdr = skb_network_header(skb) + next_hdr_offset;
  464. }
  465. switch (protocol) {
  466. case IPPROTO_GRE:
  467. next_hdr_offset += sizeof(struct gre_base_hdr);
  468. if (next_hdr_offset > encap_len)
  469. return -EINVAL;
  470. if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM)
  471. return handle_gso_type(skb, SKB_GSO_GRE_CSUM,
  472. encap_len);
  473. return handle_gso_type(skb, SKB_GSO_GRE, encap_len);
  474. case IPPROTO_UDP:
  475. next_hdr_offset += sizeof(struct udphdr);
  476. if (next_hdr_offset > encap_len)
  477. return -EINVAL;
  478. if (((struct udphdr *)next_hdr)->check)
  479. return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM,
  480. encap_len);
  481. return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len);
  482. case IPPROTO_IP:
  483. case IPPROTO_IPV6:
  484. if (ipv4)
  485. return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len);
  486. else
  487. return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len);
  488. default:
  489. return -EPROTONOSUPPORT;
  490. }
  491. }
  492. int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
  493. {
  494. struct iphdr *iph;
  495. bool ipv4;
  496. int err;
  497. if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM))
  498. return -EINVAL;
  499. /* validate protocol and length */
  500. iph = (struct iphdr *)hdr;
  501. if (iph->version == 4) {
  502. ipv4 = true;
  503. if (unlikely(len < iph->ihl * 4))
  504. return -EINVAL;
  505. } else if (iph->version == 6) {
  506. ipv4 = false;
  507. if (unlikely(len < sizeof(struct ipv6hdr)))
  508. return -EINVAL;
  509. } else {
  510. return -EINVAL;
  511. }
  512. if (ingress)
  513. err = skb_cow_head(skb, len + skb->mac_len);
  514. else
  515. err = skb_cow_head(skb,
  516. len + LL_RESERVED_SPACE(skb_dst(skb)->dev));
  517. if (unlikely(err))
  518. return err;
  519. /* push the encap headers and fix pointers */
  520. skb_reset_inner_headers(skb);
  521. skb_reset_inner_mac_header(skb); /* mac header is not yet set */
  522. skb_set_inner_protocol(skb, skb->protocol);
  523. skb->encapsulation = 1;
  524. skb_push(skb, len);
  525. if (ingress)
  526. skb_postpush_rcsum(skb, iph, len);
  527. skb_reset_network_header(skb);
  528. memcpy(skb_network_header(skb), hdr, len);
  529. bpf_compute_data_pointers(skb);
  530. skb_clear_hash(skb);
  531. if (ipv4) {
  532. skb->protocol = htons(ETH_P_IP);
  533. iph = ip_hdr(skb);
  534. if (!iph->check)
  535. iph->check = ip_fast_csum((unsigned char *)iph,
  536. iph->ihl);
  537. } else {
  538. skb->protocol = htons(ETH_P_IPV6);
  539. }
  540. if (skb_is_gso(skb))
  541. return handle_gso_encap(skb, ipv4, len);
  542. return 0;
  543. }
  544. static int __init bpf_lwt_init(void)
  545. {
  546. return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
  547. }
  548. subsys_initcall(bpf_lwt_init)