seg6_local.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*
  2. * SR-IPv6 implementation
  3. *
  4. * Authors:
  5. * David Lebrun <david.lebrun@uclouvain.be>
  6. * eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/net.h>
  17. #include <linux/module.h>
  18. #include <net/ip.h>
  19. #include <net/lwtunnel.h>
  20. #include <net/netevent.h>
  21. #include <net/netns/generic.h>
  22. #include <net/ip6_fib.h>
  23. #include <net/route.h>
  24. #include <net/seg6.h>
  25. #include <linux/seg6.h>
  26. #include <linux/seg6_local.h>
  27. #include <net/addrconf.h>
  28. #include <net/ip6_route.h>
  29. #include <net/dst_cache.h>
  30. #ifdef CONFIG_IPV6_SEG6_HMAC
  31. #include <net/seg6_hmac.h>
  32. #endif
  33. #include <net/seg6_local.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/bpf.h>
  36. struct seg6_local_lwt;
  37. struct seg6_action_desc {
  38. int action;
  39. unsigned long attrs;
  40. int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
  41. int static_headroom;
  42. };
  43. struct bpf_lwt_prog {
  44. struct bpf_prog *prog;
  45. char *name;
  46. };
  47. struct seg6_local_lwt {
  48. int action;
  49. struct ipv6_sr_hdr *srh;
  50. int table;
  51. struct in_addr nh4;
  52. struct in6_addr nh6;
  53. int iif;
  54. int oif;
  55. struct bpf_lwt_prog bpf;
  56. int headroom;
  57. struct seg6_action_desc *desc;
  58. };
  59. static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
  60. {
  61. return (struct seg6_local_lwt *)lwt->data;
  62. }
  63. static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
  64. {
  65. struct ipv6_sr_hdr *srh;
  66. int len, srhoff = 0;
  67. if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
  68. return NULL;
  69. if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
  70. return NULL;
  71. srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
  72. len = (srh->hdrlen + 1) << 3;
  73. if (!pskb_may_pull(skb, srhoff + len))
  74. return NULL;
  75. if (!seg6_validate_srh(srh, len))
  76. return NULL;
  77. return srh;
  78. }
  79. static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
  80. {
  81. struct ipv6_sr_hdr *srh;
  82. srh = get_srh(skb);
  83. if (!srh)
  84. return NULL;
  85. if (srh->segments_left == 0)
  86. return NULL;
  87. #ifdef CONFIG_IPV6_SEG6_HMAC
  88. if (!seg6_hmac_validate_skb(skb))
  89. return NULL;
  90. #endif
  91. return srh;
  92. }
  93. static bool decap_and_validate(struct sk_buff *skb, int proto)
  94. {
  95. struct ipv6_sr_hdr *srh;
  96. unsigned int off = 0;
  97. srh = get_srh(skb);
  98. if (srh && srh->segments_left > 0)
  99. return false;
  100. #ifdef CONFIG_IPV6_SEG6_HMAC
  101. if (srh && !seg6_hmac_validate_skb(skb))
  102. return false;
  103. #endif
  104. if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
  105. return false;
  106. if (!pskb_pull(skb, off))
  107. return false;
  108. skb_postpull_rcsum(skb, skb_network_header(skb), off);
  109. skb_reset_network_header(skb);
  110. skb_reset_transport_header(skb);
  111. skb->encapsulation = 0;
  112. return true;
  113. }
  114. static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
  115. {
  116. struct in6_addr *addr;
  117. srh->segments_left--;
  118. addr = srh->segments + srh->segments_left;
  119. *daddr = *addr;
  120. }
  121. int seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
  122. u32 tbl_id)
  123. {
  124. struct net *net = dev_net(skb->dev);
  125. struct ipv6hdr *hdr = ipv6_hdr(skb);
  126. int flags = RT6_LOOKUP_F_HAS_SADDR;
  127. struct dst_entry *dst = NULL;
  128. struct rt6_info *rt;
  129. struct flowi6 fl6;
  130. fl6.flowi6_iif = skb->dev->ifindex;
  131. fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
  132. fl6.saddr = hdr->saddr;
  133. fl6.flowlabel = ip6_flowinfo(hdr);
  134. fl6.flowi6_mark = skb->mark;
  135. fl6.flowi6_proto = hdr->nexthdr;
  136. if (nhaddr)
  137. fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
  138. if (!tbl_id) {
  139. dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
  140. } else {
  141. struct fib6_table *table;
  142. table = fib6_get_table(net, tbl_id);
  143. if (!table)
  144. goto out;
  145. rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
  146. dst = &rt->dst;
  147. }
  148. if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) {
  149. dst_release(dst);
  150. dst = NULL;
  151. }
  152. out:
  153. if (!dst) {
  154. rt = net->ipv6.ip6_blk_hole_entry;
  155. dst = &rt->dst;
  156. dst_hold(dst);
  157. }
  158. skb_dst_drop(skb);
  159. skb_dst_set(skb, dst);
  160. return dst->error;
  161. }
  162. /* regular endpoint function */
  163. static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  164. {
  165. struct ipv6_sr_hdr *srh;
  166. srh = get_and_validate_srh(skb);
  167. if (!srh)
  168. goto drop;
  169. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  170. seg6_lookup_nexthop(skb, NULL, 0);
  171. return dst_input(skb);
  172. drop:
  173. kfree_skb(skb);
  174. return -EINVAL;
  175. }
  176. /* regular endpoint, and forward to specified nexthop */
  177. static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  178. {
  179. struct ipv6_sr_hdr *srh;
  180. srh = get_and_validate_srh(skb);
  181. if (!srh)
  182. goto drop;
  183. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  184. seg6_lookup_nexthop(skb, &slwt->nh6, 0);
  185. return dst_input(skb);
  186. drop:
  187. kfree_skb(skb);
  188. return -EINVAL;
  189. }
  190. static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  191. {
  192. struct ipv6_sr_hdr *srh;
  193. srh = get_and_validate_srh(skb);
  194. if (!srh)
  195. goto drop;
  196. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  197. seg6_lookup_nexthop(skb, NULL, slwt->table);
  198. return dst_input(skb);
  199. drop:
  200. kfree_skb(skb);
  201. return -EINVAL;
  202. }
  203. /* decapsulate and forward inner L2 frame on specified interface */
  204. static int input_action_end_dx2(struct sk_buff *skb,
  205. struct seg6_local_lwt *slwt)
  206. {
  207. struct net *net = dev_net(skb->dev);
  208. struct net_device *odev;
  209. struct ethhdr *eth;
  210. if (!decap_and_validate(skb, NEXTHDR_NONE))
  211. goto drop;
  212. if (!pskb_may_pull(skb, ETH_HLEN))
  213. goto drop;
  214. skb_reset_mac_header(skb);
  215. eth = (struct ethhdr *)skb->data;
  216. /* To determine the frame's protocol, we assume it is 802.3. This avoids
  217. * a call to eth_type_trans(), which is not really relevant for our
  218. * use case.
  219. */
  220. if (!eth_proto_is_802_3(eth->h_proto))
  221. goto drop;
  222. odev = dev_get_by_index_rcu(net, slwt->oif);
  223. if (!odev)
  224. goto drop;
  225. /* As we accept Ethernet frames, make sure the egress device is of
  226. * the correct type.
  227. */
  228. if (odev->type != ARPHRD_ETHER)
  229. goto drop;
  230. if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
  231. goto drop;
  232. skb_orphan(skb);
  233. if (skb_warn_if_lro(skb))
  234. goto drop;
  235. skb_forward_csum(skb);
  236. if (skb->len - ETH_HLEN > odev->mtu)
  237. goto drop;
  238. skb->dev = odev;
  239. skb->protocol = eth->h_proto;
  240. return dev_queue_xmit(skb);
  241. drop:
  242. kfree_skb(skb);
  243. return -EINVAL;
  244. }
  245. /* decapsulate and forward to specified nexthop */
  246. static int input_action_end_dx6(struct sk_buff *skb,
  247. struct seg6_local_lwt *slwt)
  248. {
  249. struct in6_addr *nhaddr = NULL;
  250. /* this function accepts IPv6 encapsulated packets, with either
  251. * an SRH with SL=0, or no SRH.
  252. */
  253. if (!decap_and_validate(skb, IPPROTO_IPV6))
  254. goto drop;
  255. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  256. goto drop;
  257. /* The inner packet is not associated to any local interface,
  258. * so we do not call netif_rx().
  259. *
  260. * If slwt->nh6 is set to ::, then lookup the nexthop for the
  261. * inner packet's DA. Otherwise, use the specified nexthop.
  262. */
  263. if (!ipv6_addr_any(&slwt->nh6))
  264. nhaddr = &slwt->nh6;
  265. seg6_lookup_nexthop(skb, nhaddr, 0);
  266. return dst_input(skb);
  267. drop:
  268. kfree_skb(skb);
  269. return -EINVAL;
  270. }
  271. static int input_action_end_dx4(struct sk_buff *skb,
  272. struct seg6_local_lwt *slwt)
  273. {
  274. struct iphdr *iph;
  275. __be32 nhaddr;
  276. int err;
  277. if (!decap_and_validate(skb, IPPROTO_IPIP))
  278. goto drop;
  279. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  280. goto drop;
  281. skb->protocol = htons(ETH_P_IP);
  282. iph = ip_hdr(skb);
  283. nhaddr = slwt->nh4.s_addr ?: iph->daddr;
  284. skb_dst_drop(skb);
  285. err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
  286. if (err)
  287. goto drop;
  288. return dst_input(skb);
  289. drop:
  290. kfree_skb(skb);
  291. return -EINVAL;
  292. }
  293. static int input_action_end_dt6(struct sk_buff *skb,
  294. struct seg6_local_lwt *slwt)
  295. {
  296. if (!decap_and_validate(skb, IPPROTO_IPV6))
  297. goto drop;
  298. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  299. goto drop;
  300. seg6_lookup_nexthop(skb, NULL, slwt->table);
  301. return dst_input(skb);
  302. drop:
  303. kfree_skb(skb);
  304. return -EINVAL;
  305. }
  306. /* push an SRH on top of the current one */
  307. static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  308. {
  309. struct ipv6_sr_hdr *srh;
  310. int err = -EINVAL;
  311. srh = get_and_validate_srh(skb);
  312. if (!srh)
  313. goto drop;
  314. err = seg6_do_srh_inline(skb, slwt->srh);
  315. if (err)
  316. goto drop;
  317. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  318. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  319. seg6_lookup_nexthop(skb, NULL, 0);
  320. return dst_input(skb);
  321. drop:
  322. kfree_skb(skb);
  323. return err;
  324. }
  325. /* encapsulate within an outer IPv6 header and a specified SRH */
  326. static int input_action_end_b6_encap(struct sk_buff *skb,
  327. struct seg6_local_lwt *slwt)
  328. {
  329. struct ipv6_sr_hdr *srh;
  330. int err = -EINVAL;
  331. srh = get_and_validate_srh(skb);
  332. if (!srh)
  333. goto drop;
  334. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  335. skb_reset_inner_headers(skb);
  336. skb->encapsulation = 1;
  337. err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
  338. if (err)
  339. goto drop;
  340. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  341. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  342. seg6_lookup_nexthop(skb, NULL, 0);
  343. return dst_input(skb);
  344. drop:
  345. kfree_skb(skb);
  346. return err;
  347. }
  348. DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
  349. bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
  350. {
  351. struct seg6_bpf_srh_state *srh_state =
  352. this_cpu_ptr(&seg6_bpf_srh_states);
  353. struct ipv6_sr_hdr *srh = srh_state->srh;
  354. if (unlikely(srh == NULL))
  355. return false;
  356. if (unlikely(!srh_state->valid)) {
  357. if ((srh_state->hdrlen & 7) != 0)
  358. return false;
  359. srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
  360. if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3))
  361. return false;
  362. srh_state->valid = true;
  363. }
  364. return true;
  365. }
  366. static int input_action_end_bpf(struct sk_buff *skb,
  367. struct seg6_local_lwt *slwt)
  368. {
  369. struct seg6_bpf_srh_state *srh_state =
  370. this_cpu_ptr(&seg6_bpf_srh_states);
  371. struct ipv6_sr_hdr *srh;
  372. int ret;
  373. srh = get_and_validate_srh(skb);
  374. if (!srh) {
  375. kfree_skb(skb);
  376. return -EINVAL;
  377. }
  378. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  379. /* preempt_disable is needed to protect the per-CPU buffer srh_state,
  380. * which is also accessed by the bpf_lwt_seg6_* helpers
  381. */
  382. preempt_disable();
  383. srh_state->srh = srh;
  384. srh_state->hdrlen = srh->hdrlen << 3;
  385. srh_state->valid = true;
  386. rcu_read_lock();
  387. bpf_compute_data_pointers(skb);
  388. ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
  389. rcu_read_unlock();
  390. switch (ret) {
  391. case BPF_OK:
  392. case BPF_REDIRECT:
  393. break;
  394. case BPF_DROP:
  395. goto drop;
  396. default:
  397. pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
  398. goto drop;
  399. }
  400. if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
  401. goto drop;
  402. preempt_enable();
  403. if (ret != BPF_REDIRECT)
  404. seg6_lookup_nexthop(skb, NULL, 0);
  405. return dst_input(skb);
  406. drop:
  407. preempt_enable();
  408. kfree_skb(skb);
  409. return -EINVAL;
  410. }
  411. static struct seg6_action_desc seg6_action_table[] = {
  412. {
  413. .action = SEG6_LOCAL_ACTION_END,
  414. .attrs = 0,
  415. .input = input_action_end,
  416. },
  417. {
  418. .action = SEG6_LOCAL_ACTION_END_X,
  419. .attrs = (1 << SEG6_LOCAL_NH6),
  420. .input = input_action_end_x,
  421. },
  422. {
  423. .action = SEG6_LOCAL_ACTION_END_T,
  424. .attrs = (1 << SEG6_LOCAL_TABLE),
  425. .input = input_action_end_t,
  426. },
  427. {
  428. .action = SEG6_LOCAL_ACTION_END_DX2,
  429. .attrs = (1 << SEG6_LOCAL_OIF),
  430. .input = input_action_end_dx2,
  431. },
  432. {
  433. .action = SEG6_LOCAL_ACTION_END_DX6,
  434. .attrs = (1 << SEG6_LOCAL_NH6),
  435. .input = input_action_end_dx6,
  436. },
  437. {
  438. .action = SEG6_LOCAL_ACTION_END_DX4,
  439. .attrs = (1 << SEG6_LOCAL_NH4),
  440. .input = input_action_end_dx4,
  441. },
  442. {
  443. .action = SEG6_LOCAL_ACTION_END_DT6,
  444. .attrs = (1 << SEG6_LOCAL_TABLE),
  445. .input = input_action_end_dt6,
  446. },
  447. {
  448. .action = SEG6_LOCAL_ACTION_END_B6,
  449. .attrs = (1 << SEG6_LOCAL_SRH),
  450. .input = input_action_end_b6,
  451. },
  452. {
  453. .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
  454. .attrs = (1 << SEG6_LOCAL_SRH),
  455. .input = input_action_end_b6_encap,
  456. .static_headroom = sizeof(struct ipv6hdr),
  457. },
  458. {
  459. .action = SEG6_LOCAL_ACTION_END_BPF,
  460. .attrs = (1 << SEG6_LOCAL_BPF),
  461. .input = input_action_end_bpf,
  462. },
  463. };
  464. static struct seg6_action_desc *__get_action_desc(int action)
  465. {
  466. struct seg6_action_desc *desc;
  467. int i, count;
  468. count = ARRAY_SIZE(seg6_action_table);
  469. for (i = 0; i < count; i++) {
  470. desc = &seg6_action_table[i];
  471. if (desc->action == action)
  472. return desc;
  473. }
  474. return NULL;
  475. }
  476. static int seg6_local_input(struct sk_buff *skb)
  477. {
  478. struct dst_entry *orig_dst = skb_dst(skb);
  479. struct seg6_action_desc *desc;
  480. struct seg6_local_lwt *slwt;
  481. if (skb->protocol != htons(ETH_P_IPV6)) {
  482. kfree_skb(skb);
  483. return -EINVAL;
  484. }
  485. slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
  486. desc = slwt->desc;
  487. return desc->input(skb, slwt);
  488. }
  489. static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
  490. [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
  491. [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
  492. [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
  493. [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
  494. .len = sizeof(struct in_addr) },
  495. [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
  496. .len = sizeof(struct in6_addr) },
  497. [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
  498. [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
  499. [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
  500. };
  501. static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  502. {
  503. struct ipv6_sr_hdr *srh;
  504. int len;
  505. srh = nla_data(attrs[SEG6_LOCAL_SRH]);
  506. len = nla_len(attrs[SEG6_LOCAL_SRH]);
  507. /* SRH must contain at least one segment */
  508. if (len < sizeof(*srh) + sizeof(struct in6_addr))
  509. return -EINVAL;
  510. if (!seg6_validate_srh(srh, len))
  511. return -EINVAL;
  512. slwt->srh = kmemdup(srh, len, GFP_KERNEL);
  513. if (!slwt->srh)
  514. return -ENOMEM;
  515. slwt->headroom += len;
  516. return 0;
  517. }
  518. static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  519. {
  520. struct ipv6_sr_hdr *srh;
  521. struct nlattr *nla;
  522. int len;
  523. srh = slwt->srh;
  524. len = (srh->hdrlen + 1) << 3;
  525. nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
  526. if (!nla)
  527. return -EMSGSIZE;
  528. memcpy(nla_data(nla), srh, len);
  529. return 0;
  530. }
  531. static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  532. {
  533. int len = (a->srh->hdrlen + 1) << 3;
  534. if (len != ((b->srh->hdrlen + 1) << 3))
  535. return 1;
  536. return memcmp(a->srh, b->srh, len);
  537. }
  538. static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  539. {
  540. slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
  541. return 0;
  542. }
  543. static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  544. {
  545. if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
  546. return -EMSGSIZE;
  547. return 0;
  548. }
  549. static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  550. {
  551. if (a->table != b->table)
  552. return 1;
  553. return 0;
  554. }
  555. static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  556. {
  557. memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
  558. sizeof(struct in_addr));
  559. return 0;
  560. }
  561. static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  562. {
  563. struct nlattr *nla;
  564. nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
  565. if (!nla)
  566. return -EMSGSIZE;
  567. memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
  568. return 0;
  569. }
  570. static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  571. {
  572. return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
  573. }
  574. static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  575. {
  576. memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
  577. sizeof(struct in6_addr));
  578. return 0;
  579. }
  580. static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  581. {
  582. struct nlattr *nla;
  583. nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
  584. if (!nla)
  585. return -EMSGSIZE;
  586. memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
  587. return 0;
  588. }
  589. static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  590. {
  591. return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
  592. }
  593. static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  594. {
  595. slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
  596. return 0;
  597. }
  598. static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  599. {
  600. if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
  601. return -EMSGSIZE;
  602. return 0;
  603. }
  604. static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  605. {
  606. if (a->iif != b->iif)
  607. return 1;
  608. return 0;
  609. }
  610. static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  611. {
  612. slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
  613. return 0;
  614. }
  615. static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  616. {
  617. if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
  618. return -EMSGSIZE;
  619. return 0;
  620. }
  621. static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  622. {
  623. if (a->oif != b->oif)
  624. return 1;
  625. return 0;
  626. }
  627. #define MAX_PROG_NAME 256
  628. static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
  629. [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
  630. [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
  631. .len = MAX_PROG_NAME },
  632. };
  633. static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  634. {
  635. struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
  636. struct bpf_prog *p;
  637. int ret;
  638. u32 fd;
  639. ret = nla_parse_nested(tb, SEG6_LOCAL_BPF_PROG_MAX,
  640. attrs[SEG6_LOCAL_BPF], bpf_prog_policy, NULL);
  641. if (ret < 0)
  642. return ret;
  643. if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
  644. return -EINVAL;
  645. slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
  646. if (!slwt->bpf.name)
  647. return -ENOMEM;
  648. fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
  649. p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
  650. if (IS_ERR(p)) {
  651. kfree(slwt->bpf.name);
  652. return PTR_ERR(p);
  653. }
  654. slwt->bpf.prog = p;
  655. return 0;
  656. }
  657. static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  658. {
  659. struct nlattr *nest;
  660. if (!slwt->bpf.prog)
  661. return 0;
  662. nest = nla_nest_start(skb, SEG6_LOCAL_BPF);
  663. if (!nest)
  664. return -EMSGSIZE;
  665. if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
  666. return -EMSGSIZE;
  667. if (slwt->bpf.name &&
  668. nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
  669. return -EMSGSIZE;
  670. return nla_nest_end(skb, nest);
  671. }
  672. static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  673. {
  674. if (!a->bpf.name && !b->bpf.name)
  675. return 0;
  676. if (!a->bpf.name || !b->bpf.name)
  677. return 1;
  678. return strcmp(a->bpf.name, b->bpf.name);
  679. }
  680. struct seg6_action_param {
  681. int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
  682. int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
  683. int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
  684. };
  685. static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
  686. [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
  687. .put = put_nla_srh,
  688. .cmp = cmp_nla_srh },
  689. [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
  690. .put = put_nla_table,
  691. .cmp = cmp_nla_table },
  692. [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
  693. .put = put_nla_nh4,
  694. .cmp = cmp_nla_nh4 },
  695. [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
  696. .put = put_nla_nh6,
  697. .cmp = cmp_nla_nh6 },
  698. [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
  699. .put = put_nla_iif,
  700. .cmp = cmp_nla_iif },
  701. [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
  702. .put = put_nla_oif,
  703. .cmp = cmp_nla_oif },
  704. [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
  705. .put = put_nla_bpf,
  706. .cmp = cmp_nla_bpf },
  707. };
  708. static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  709. {
  710. struct seg6_action_param *param;
  711. struct seg6_action_desc *desc;
  712. int i, err;
  713. desc = __get_action_desc(slwt->action);
  714. if (!desc)
  715. return -EINVAL;
  716. if (!desc->input)
  717. return -EOPNOTSUPP;
  718. slwt->desc = desc;
  719. slwt->headroom += desc->static_headroom;
  720. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  721. if (desc->attrs & (1 << i)) {
  722. if (!attrs[i])
  723. return -EINVAL;
  724. param = &seg6_action_params[i];
  725. err = param->parse(attrs, slwt);
  726. if (err < 0)
  727. return err;
  728. }
  729. }
  730. return 0;
  731. }
  732. static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
  733. const void *cfg, struct lwtunnel_state **ts,
  734. struct netlink_ext_ack *extack)
  735. {
  736. struct nlattr *tb[SEG6_LOCAL_MAX + 1];
  737. struct lwtunnel_state *newts;
  738. struct seg6_local_lwt *slwt;
  739. int err;
  740. if (family != AF_INET6)
  741. return -EINVAL;
  742. err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy,
  743. extack);
  744. if (err < 0)
  745. return err;
  746. if (!tb[SEG6_LOCAL_ACTION])
  747. return -EINVAL;
  748. newts = lwtunnel_state_alloc(sizeof(*slwt));
  749. if (!newts)
  750. return -ENOMEM;
  751. slwt = seg6_local_lwtunnel(newts);
  752. slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
  753. err = parse_nla_action(tb, slwt);
  754. if (err < 0)
  755. goto out_free;
  756. newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
  757. newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
  758. newts->headroom = slwt->headroom;
  759. *ts = newts;
  760. return 0;
  761. out_free:
  762. kfree(slwt->srh);
  763. kfree(newts);
  764. return err;
  765. }
  766. static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
  767. {
  768. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  769. kfree(slwt->srh);
  770. if (slwt->desc->attrs & (1 << SEG6_LOCAL_BPF)) {
  771. kfree(slwt->bpf.name);
  772. bpf_prog_put(slwt->bpf.prog);
  773. }
  774. return;
  775. }
  776. static int seg6_local_fill_encap(struct sk_buff *skb,
  777. struct lwtunnel_state *lwt)
  778. {
  779. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  780. struct seg6_action_param *param;
  781. int i, err;
  782. if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
  783. return -EMSGSIZE;
  784. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  785. if (slwt->desc->attrs & (1 << i)) {
  786. param = &seg6_action_params[i];
  787. err = param->put(skb, slwt);
  788. if (err < 0)
  789. return err;
  790. }
  791. }
  792. return 0;
  793. }
  794. static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
  795. {
  796. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  797. unsigned long attrs;
  798. int nlsize;
  799. nlsize = nla_total_size(4); /* action */
  800. attrs = slwt->desc->attrs;
  801. if (attrs & (1 << SEG6_LOCAL_SRH))
  802. nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
  803. if (attrs & (1 << SEG6_LOCAL_TABLE))
  804. nlsize += nla_total_size(4);
  805. if (attrs & (1 << SEG6_LOCAL_NH4))
  806. nlsize += nla_total_size(4);
  807. if (attrs & (1 << SEG6_LOCAL_NH6))
  808. nlsize += nla_total_size(16);
  809. if (attrs & (1 << SEG6_LOCAL_IIF))
  810. nlsize += nla_total_size(4);
  811. if (attrs & (1 << SEG6_LOCAL_OIF))
  812. nlsize += nla_total_size(4);
  813. if (attrs & (1 << SEG6_LOCAL_BPF))
  814. nlsize += nla_total_size(sizeof(struct nlattr)) +
  815. nla_total_size(MAX_PROG_NAME) +
  816. nla_total_size(4);
  817. return nlsize;
  818. }
  819. static int seg6_local_cmp_encap(struct lwtunnel_state *a,
  820. struct lwtunnel_state *b)
  821. {
  822. struct seg6_local_lwt *slwt_a, *slwt_b;
  823. struct seg6_action_param *param;
  824. int i;
  825. slwt_a = seg6_local_lwtunnel(a);
  826. slwt_b = seg6_local_lwtunnel(b);
  827. if (slwt_a->action != slwt_b->action)
  828. return 1;
  829. if (slwt_a->desc->attrs != slwt_b->desc->attrs)
  830. return 1;
  831. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  832. if (slwt_a->desc->attrs & (1 << i)) {
  833. param = &seg6_action_params[i];
  834. if (param->cmp(slwt_a, slwt_b))
  835. return 1;
  836. }
  837. }
  838. return 0;
  839. }
  840. static const struct lwtunnel_encap_ops seg6_local_ops = {
  841. .build_state = seg6_local_build_state,
  842. .destroy_state = seg6_local_destroy_state,
  843. .input = seg6_local_input,
  844. .fill_encap = seg6_local_fill_encap,
  845. .get_encap_size = seg6_local_get_encap_size,
  846. .cmp_encap = seg6_local_cmp_encap,
  847. .owner = THIS_MODULE,
  848. };
  849. int __init seg6_local_init(void)
  850. {
  851. return lwtunnel_encap_add_ops(&seg6_local_ops,
  852. LWTUNNEL_ENCAP_SEG6_LOCAL);
  853. }
  854. void seg6_local_exit(void)
  855. {
  856. lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
  857. }