seg6_local.c 19 KB

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