seg6_local.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. static int input_action_end_bpf(struct sk_buff *skb,
  350. struct seg6_local_lwt *slwt)
  351. {
  352. struct seg6_bpf_srh_state *srh_state =
  353. this_cpu_ptr(&seg6_bpf_srh_states);
  354. struct seg6_bpf_srh_state local_srh_state;
  355. struct ipv6_sr_hdr *srh;
  356. int srhoff = 0;
  357. int ret;
  358. srh = get_and_validate_srh(skb);
  359. if (!srh)
  360. goto drop;
  361. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  362. /* preempt_disable is needed to protect the per-CPU buffer srh_state,
  363. * which is also accessed by the bpf_lwt_seg6_* helpers
  364. */
  365. preempt_disable();
  366. srh_state->hdrlen = srh->hdrlen << 3;
  367. srh_state->valid = 1;
  368. rcu_read_lock();
  369. bpf_compute_data_pointers(skb);
  370. ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
  371. rcu_read_unlock();
  372. local_srh_state = *srh_state;
  373. preempt_enable();
  374. switch (ret) {
  375. case BPF_OK:
  376. case BPF_REDIRECT:
  377. break;
  378. case BPF_DROP:
  379. goto drop;
  380. default:
  381. pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
  382. goto drop;
  383. }
  384. if (unlikely((local_srh_state.hdrlen & 7) != 0))
  385. goto drop;
  386. if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
  387. goto drop;
  388. srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
  389. srh->hdrlen = (u8)(local_srh_state.hdrlen >> 3);
  390. if (!local_srh_state.valid &&
  391. unlikely(!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3)))
  392. goto drop;
  393. if (ret != BPF_REDIRECT)
  394. seg6_lookup_nexthop(skb, NULL, 0);
  395. return dst_input(skb);
  396. drop:
  397. kfree_skb(skb);
  398. return -EINVAL;
  399. }
  400. static struct seg6_action_desc seg6_action_table[] = {
  401. {
  402. .action = SEG6_LOCAL_ACTION_END,
  403. .attrs = 0,
  404. .input = input_action_end,
  405. },
  406. {
  407. .action = SEG6_LOCAL_ACTION_END_X,
  408. .attrs = (1 << SEG6_LOCAL_NH6),
  409. .input = input_action_end_x,
  410. },
  411. {
  412. .action = SEG6_LOCAL_ACTION_END_T,
  413. .attrs = (1 << SEG6_LOCAL_TABLE),
  414. .input = input_action_end_t,
  415. },
  416. {
  417. .action = SEG6_LOCAL_ACTION_END_DX2,
  418. .attrs = (1 << SEG6_LOCAL_OIF),
  419. .input = input_action_end_dx2,
  420. },
  421. {
  422. .action = SEG6_LOCAL_ACTION_END_DX6,
  423. .attrs = (1 << SEG6_LOCAL_NH6),
  424. .input = input_action_end_dx6,
  425. },
  426. {
  427. .action = SEG6_LOCAL_ACTION_END_DX4,
  428. .attrs = (1 << SEG6_LOCAL_NH4),
  429. .input = input_action_end_dx4,
  430. },
  431. {
  432. .action = SEG6_LOCAL_ACTION_END_DT6,
  433. .attrs = (1 << SEG6_LOCAL_TABLE),
  434. .input = input_action_end_dt6,
  435. },
  436. {
  437. .action = SEG6_LOCAL_ACTION_END_B6,
  438. .attrs = (1 << SEG6_LOCAL_SRH),
  439. .input = input_action_end_b6,
  440. },
  441. {
  442. .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
  443. .attrs = (1 << SEG6_LOCAL_SRH),
  444. .input = input_action_end_b6_encap,
  445. .static_headroom = sizeof(struct ipv6hdr),
  446. },
  447. {
  448. .action = SEG6_LOCAL_ACTION_END_BPF,
  449. .attrs = (1 << SEG6_LOCAL_BPF),
  450. .input = input_action_end_bpf,
  451. },
  452. };
  453. static struct seg6_action_desc *__get_action_desc(int action)
  454. {
  455. struct seg6_action_desc *desc;
  456. int i, count;
  457. count = ARRAY_SIZE(seg6_action_table);
  458. for (i = 0; i < count; i++) {
  459. desc = &seg6_action_table[i];
  460. if (desc->action == action)
  461. return desc;
  462. }
  463. return NULL;
  464. }
  465. static int seg6_local_input(struct sk_buff *skb)
  466. {
  467. struct dst_entry *orig_dst = skb_dst(skb);
  468. struct seg6_action_desc *desc;
  469. struct seg6_local_lwt *slwt;
  470. if (skb->protocol != htons(ETH_P_IPV6)) {
  471. kfree_skb(skb);
  472. return -EINVAL;
  473. }
  474. slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
  475. desc = slwt->desc;
  476. return desc->input(skb, slwt);
  477. }
  478. static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
  479. [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
  480. [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
  481. [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
  482. [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
  483. .len = sizeof(struct in_addr) },
  484. [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
  485. .len = sizeof(struct in6_addr) },
  486. [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
  487. [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
  488. [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
  489. };
  490. static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  491. {
  492. struct ipv6_sr_hdr *srh;
  493. int len;
  494. srh = nla_data(attrs[SEG6_LOCAL_SRH]);
  495. len = nla_len(attrs[SEG6_LOCAL_SRH]);
  496. /* SRH must contain at least one segment */
  497. if (len < sizeof(*srh) + sizeof(struct in6_addr))
  498. return -EINVAL;
  499. if (!seg6_validate_srh(srh, len))
  500. return -EINVAL;
  501. slwt->srh = kmemdup(srh, len, GFP_KERNEL);
  502. if (!slwt->srh)
  503. return -ENOMEM;
  504. slwt->headroom += len;
  505. return 0;
  506. }
  507. static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  508. {
  509. struct ipv6_sr_hdr *srh;
  510. struct nlattr *nla;
  511. int len;
  512. srh = slwt->srh;
  513. len = (srh->hdrlen + 1) << 3;
  514. nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
  515. if (!nla)
  516. return -EMSGSIZE;
  517. memcpy(nla_data(nla), srh, len);
  518. return 0;
  519. }
  520. static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  521. {
  522. int len = (a->srh->hdrlen + 1) << 3;
  523. if (len != ((b->srh->hdrlen + 1) << 3))
  524. return 1;
  525. return memcmp(a->srh, b->srh, len);
  526. }
  527. static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  528. {
  529. slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
  530. return 0;
  531. }
  532. static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  533. {
  534. if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
  535. return -EMSGSIZE;
  536. return 0;
  537. }
  538. static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  539. {
  540. if (a->table != b->table)
  541. return 1;
  542. return 0;
  543. }
  544. static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  545. {
  546. memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
  547. sizeof(struct in_addr));
  548. return 0;
  549. }
  550. static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  551. {
  552. struct nlattr *nla;
  553. nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
  554. if (!nla)
  555. return -EMSGSIZE;
  556. memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
  557. return 0;
  558. }
  559. static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  560. {
  561. return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
  562. }
  563. static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  564. {
  565. memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
  566. sizeof(struct in6_addr));
  567. return 0;
  568. }
  569. static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  570. {
  571. struct nlattr *nla;
  572. nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
  573. if (!nla)
  574. return -EMSGSIZE;
  575. memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
  576. return 0;
  577. }
  578. static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  579. {
  580. return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
  581. }
  582. static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  583. {
  584. slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
  585. return 0;
  586. }
  587. static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  588. {
  589. if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
  590. return -EMSGSIZE;
  591. return 0;
  592. }
  593. static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  594. {
  595. if (a->iif != b->iif)
  596. return 1;
  597. return 0;
  598. }
  599. static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  600. {
  601. slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
  602. return 0;
  603. }
  604. static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  605. {
  606. if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
  607. return -EMSGSIZE;
  608. return 0;
  609. }
  610. static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  611. {
  612. if (a->oif != b->oif)
  613. return 1;
  614. return 0;
  615. }
  616. #define MAX_PROG_NAME 256
  617. static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
  618. [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
  619. [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
  620. .len = MAX_PROG_NAME },
  621. };
  622. static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  623. {
  624. struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
  625. struct bpf_prog *p;
  626. int ret;
  627. u32 fd;
  628. ret = nla_parse_nested(tb, SEG6_LOCAL_BPF_PROG_MAX,
  629. attrs[SEG6_LOCAL_BPF], bpf_prog_policy, NULL);
  630. if (ret < 0)
  631. return ret;
  632. if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
  633. return -EINVAL;
  634. slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
  635. if (!slwt->bpf.name)
  636. return -ENOMEM;
  637. fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
  638. p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
  639. if (IS_ERR(p)) {
  640. kfree(slwt->bpf.name);
  641. return PTR_ERR(p);
  642. }
  643. slwt->bpf.prog = p;
  644. return 0;
  645. }
  646. static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  647. {
  648. struct nlattr *nest;
  649. if (!slwt->bpf.prog)
  650. return 0;
  651. nest = nla_nest_start(skb, SEG6_LOCAL_BPF);
  652. if (!nest)
  653. return -EMSGSIZE;
  654. if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
  655. return -EMSGSIZE;
  656. if (slwt->bpf.name &&
  657. nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
  658. return -EMSGSIZE;
  659. return nla_nest_end(skb, nest);
  660. }
  661. static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  662. {
  663. if (!a->bpf.name && !b->bpf.name)
  664. return 0;
  665. if (!a->bpf.name || !b->bpf.name)
  666. return 1;
  667. return strcmp(a->bpf.name, b->bpf.name);
  668. }
  669. struct seg6_action_param {
  670. int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
  671. int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
  672. int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
  673. };
  674. static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
  675. [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
  676. .put = put_nla_srh,
  677. .cmp = cmp_nla_srh },
  678. [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
  679. .put = put_nla_table,
  680. .cmp = cmp_nla_table },
  681. [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
  682. .put = put_nla_nh4,
  683. .cmp = cmp_nla_nh4 },
  684. [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
  685. .put = put_nla_nh6,
  686. .cmp = cmp_nla_nh6 },
  687. [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
  688. .put = put_nla_iif,
  689. .cmp = cmp_nla_iif },
  690. [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
  691. .put = put_nla_oif,
  692. .cmp = cmp_nla_oif },
  693. [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
  694. .put = put_nla_bpf,
  695. .cmp = cmp_nla_bpf },
  696. };
  697. static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  698. {
  699. struct seg6_action_param *param;
  700. struct seg6_action_desc *desc;
  701. int i, err;
  702. desc = __get_action_desc(slwt->action);
  703. if (!desc)
  704. return -EINVAL;
  705. if (!desc->input)
  706. return -EOPNOTSUPP;
  707. slwt->desc = desc;
  708. slwt->headroom += desc->static_headroom;
  709. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  710. if (desc->attrs & (1 << i)) {
  711. if (!attrs[i])
  712. return -EINVAL;
  713. param = &seg6_action_params[i];
  714. err = param->parse(attrs, slwt);
  715. if (err < 0)
  716. return err;
  717. }
  718. }
  719. return 0;
  720. }
  721. static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
  722. const void *cfg, struct lwtunnel_state **ts,
  723. struct netlink_ext_ack *extack)
  724. {
  725. struct nlattr *tb[SEG6_LOCAL_MAX + 1];
  726. struct lwtunnel_state *newts;
  727. struct seg6_local_lwt *slwt;
  728. int err;
  729. if (family != AF_INET6)
  730. return -EINVAL;
  731. err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy,
  732. extack);
  733. if (err < 0)
  734. return err;
  735. if (!tb[SEG6_LOCAL_ACTION])
  736. return -EINVAL;
  737. newts = lwtunnel_state_alloc(sizeof(*slwt));
  738. if (!newts)
  739. return -ENOMEM;
  740. slwt = seg6_local_lwtunnel(newts);
  741. slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
  742. err = parse_nla_action(tb, slwt);
  743. if (err < 0)
  744. goto out_free;
  745. newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
  746. newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
  747. newts->headroom = slwt->headroom;
  748. *ts = newts;
  749. return 0;
  750. out_free:
  751. kfree(slwt->srh);
  752. kfree(newts);
  753. return err;
  754. }
  755. static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
  756. {
  757. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  758. kfree(slwt->srh);
  759. if (slwt->desc->attrs & (1 << SEG6_LOCAL_BPF)) {
  760. kfree(slwt->bpf.name);
  761. bpf_prog_put(slwt->bpf.prog);
  762. }
  763. return;
  764. }
  765. static int seg6_local_fill_encap(struct sk_buff *skb,
  766. struct lwtunnel_state *lwt)
  767. {
  768. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  769. struct seg6_action_param *param;
  770. int i, err;
  771. if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
  772. return -EMSGSIZE;
  773. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  774. if (slwt->desc->attrs & (1 << i)) {
  775. param = &seg6_action_params[i];
  776. err = param->put(skb, slwt);
  777. if (err < 0)
  778. return err;
  779. }
  780. }
  781. return 0;
  782. }
  783. static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
  784. {
  785. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  786. unsigned long attrs;
  787. int nlsize;
  788. nlsize = nla_total_size(4); /* action */
  789. attrs = slwt->desc->attrs;
  790. if (attrs & (1 << SEG6_LOCAL_SRH))
  791. nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
  792. if (attrs & (1 << SEG6_LOCAL_TABLE))
  793. nlsize += nla_total_size(4);
  794. if (attrs & (1 << SEG6_LOCAL_NH4))
  795. nlsize += nla_total_size(4);
  796. if (attrs & (1 << SEG6_LOCAL_NH6))
  797. nlsize += nla_total_size(16);
  798. if (attrs & (1 << SEG6_LOCAL_IIF))
  799. nlsize += nla_total_size(4);
  800. if (attrs & (1 << SEG6_LOCAL_OIF))
  801. nlsize += nla_total_size(4);
  802. if (attrs & (1 << SEG6_LOCAL_BPF))
  803. nlsize += nla_total_size(sizeof(struct nlattr)) +
  804. nla_total_size(MAX_PROG_NAME) +
  805. nla_total_size(4);
  806. return nlsize;
  807. }
  808. static int seg6_local_cmp_encap(struct lwtunnel_state *a,
  809. struct lwtunnel_state *b)
  810. {
  811. struct seg6_local_lwt *slwt_a, *slwt_b;
  812. struct seg6_action_param *param;
  813. int i;
  814. slwt_a = seg6_local_lwtunnel(a);
  815. slwt_b = seg6_local_lwtunnel(b);
  816. if (slwt_a->action != slwt_b->action)
  817. return 1;
  818. if (slwt_a->desc->attrs != slwt_b->desc->attrs)
  819. return 1;
  820. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  821. if (slwt_a->desc->attrs & (1 << i)) {
  822. param = &seg6_action_params[i];
  823. if (param->cmp(slwt_a, slwt_b))
  824. return 1;
  825. }
  826. }
  827. return 0;
  828. }
  829. static const struct lwtunnel_encap_ops seg6_local_ops = {
  830. .build_state = seg6_local_build_state,
  831. .destroy_state = seg6_local_destroy_state,
  832. .input = seg6_local_input,
  833. .fill_encap = seg6_local_fill_encap,
  834. .get_encap_size = seg6_local_get_encap_size,
  835. .cmp_encap = seg6_local_cmp_encap,
  836. .owner = THIS_MODULE,
  837. };
  838. int __init seg6_local_init(void)
  839. {
  840. return lwtunnel_encap_add_ops(&seg6_local_ops,
  841. LWTUNNEL_ENCAP_SEG6_LOCAL);
  842. }
  843. void seg6_local_exit(void)
  844. {
  845. lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
  846. }