seg6.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/errno.h>
  14. #include <linux/types.h>
  15. #include <linux/socket.h>
  16. #include <linux/net.h>
  17. #include <linux/in6.h>
  18. #include <linux/slab.h>
  19. #include <net/ipv6.h>
  20. #include <net/protocol.h>
  21. #include <net/seg6.h>
  22. #include <net/genetlink.h>
  23. #include <linux/seg6.h>
  24. #include <linux/seg6_genl.h>
  25. #ifdef CONFIG_IPV6_SEG6_HMAC
  26. #include <net/seg6_hmac.h>
  27. #endif
  28. bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len)
  29. {
  30. int trailing;
  31. unsigned int tlv_offset;
  32. if (srh->type != IPV6_SRCRT_TYPE_4)
  33. return false;
  34. if (((srh->hdrlen + 1) << 3) != len)
  35. return false;
  36. if (srh->segments_left > srh->first_segment)
  37. return false;
  38. tlv_offset = sizeof(*srh) + ((srh->first_segment + 1) << 4);
  39. trailing = len - tlv_offset;
  40. if (trailing < 0)
  41. return false;
  42. while (trailing) {
  43. struct sr6_tlv *tlv;
  44. unsigned int tlv_len;
  45. if (trailing < sizeof(*tlv))
  46. return false;
  47. tlv = (struct sr6_tlv *)((unsigned char *)srh + tlv_offset);
  48. tlv_len = sizeof(*tlv) + tlv->len;
  49. trailing -= tlv_len;
  50. if (trailing < 0)
  51. return false;
  52. tlv_offset += tlv_len;
  53. }
  54. return true;
  55. }
  56. static struct genl_family seg6_genl_family;
  57. static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = {
  58. [SEG6_ATTR_DST] = { .type = NLA_BINARY,
  59. .len = sizeof(struct in6_addr) },
  60. [SEG6_ATTR_DSTLEN] = { .type = NLA_S32, },
  61. [SEG6_ATTR_HMACKEYID] = { .type = NLA_U32, },
  62. [SEG6_ATTR_SECRET] = { .type = NLA_BINARY, },
  63. [SEG6_ATTR_SECRETLEN] = { .type = NLA_U8, },
  64. [SEG6_ATTR_ALGID] = { .type = NLA_U8, },
  65. [SEG6_ATTR_HMACINFO] = { .type = NLA_NESTED, },
  66. };
  67. #ifdef CONFIG_IPV6_SEG6_HMAC
  68. static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
  69. {
  70. struct net *net = genl_info_net(info);
  71. struct seg6_pernet_data *sdata;
  72. struct seg6_hmac_info *hinfo;
  73. u32 hmackeyid;
  74. char *secret;
  75. int err = 0;
  76. u8 algid;
  77. u8 slen;
  78. sdata = seg6_pernet(net);
  79. if (!info->attrs[SEG6_ATTR_HMACKEYID] ||
  80. !info->attrs[SEG6_ATTR_SECRETLEN] ||
  81. !info->attrs[SEG6_ATTR_ALGID])
  82. return -EINVAL;
  83. hmackeyid = nla_get_u32(info->attrs[SEG6_ATTR_HMACKEYID]);
  84. slen = nla_get_u8(info->attrs[SEG6_ATTR_SECRETLEN]);
  85. algid = nla_get_u8(info->attrs[SEG6_ATTR_ALGID]);
  86. if (hmackeyid == 0)
  87. return -EINVAL;
  88. if (slen > SEG6_HMAC_SECRET_LEN)
  89. return -EINVAL;
  90. mutex_lock(&sdata->lock);
  91. hinfo = seg6_hmac_info_lookup(net, hmackeyid);
  92. if (!slen) {
  93. if (!hinfo)
  94. err = -ENOENT;
  95. err = seg6_hmac_info_del(net, hmackeyid);
  96. goto out_unlock;
  97. }
  98. if (!info->attrs[SEG6_ATTR_SECRET]) {
  99. err = -EINVAL;
  100. goto out_unlock;
  101. }
  102. if (hinfo) {
  103. err = seg6_hmac_info_del(net, hmackeyid);
  104. if (err)
  105. goto out_unlock;
  106. }
  107. secret = (char *)nla_data(info->attrs[SEG6_ATTR_SECRET]);
  108. hinfo = kzalloc(sizeof(*hinfo), GFP_KERNEL);
  109. if (!hinfo) {
  110. err = -ENOMEM;
  111. goto out_unlock;
  112. }
  113. memcpy(hinfo->secret, secret, slen);
  114. hinfo->slen = slen;
  115. hinfo->alg_id = algid;
  116. hinfo->hmackeyid = hmackeyid;
  117. err = seg6_hmac_info_add(net, hmackeyid, hinfo);
  118. if (err)
  119. kfree(hinfo);
  120. out_unlock:
  121. mutex_unlock(&sdata->lock);
  122. return err;
  123. }
  124. #else
  125. static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
  126. {
  127. return -ENOTSUPP;
  128. }
  129. #endif
  130. static int seg6_genl_set_tunsrc(struct sk_buff *skb, struct genl_info *info)
  131. {
  132. struct net *net = genl_info_net(info);
  133. struct in6_addr *val, *t_old, *t_new;
  134. struct seg6_pernet_data *sdata;
  135. sdata = seg6_pernet(net);
  136. if (!info->attrs[SEG6_ATTR_DST])
  137. return -EINVAL;
  138. val = nla_data(info->attrs[SEG6_ATTR_DST]);
  139. t_new = kmemdup(val, sizeof(*val), GFP_KERNEL);
  140. if (!t_new)
  141. return -ENOMEM;
  142. mutex_lock(&sdata->lock);
  143. t_old = sdata->tun_src;
  144. rcu_assign_pointer(sdata->tun_src, t_new);
  145. mutex_unlock(&sdata->lock);
  146. synchronize_net();
  147. kfree(t_old);
  148. return 0;
  149. }
  150. static int seg6_genl_get_tunsrc(struct sk_buff *skb, struct genl_info *info)
  151. {
  152. struct net *net = genl_info_net(info);
  153. struct in6_addr *tun_src;
  154. struct sk_buff *msg;
  155. void *hdr;
  156. msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  157. if (!msg)
  158. return -ENOMEM;
  159. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  160. &seg6_genl_family, 0, SEG6_CMD_GET_TUNSRC);
  161. if (!hdr)
  162. goto free_msg;
  163. rcu_read_lock();
  164. tun_src = rcu_dereference(seg6_pernet(net)->tun_src);
  165. if (nla_put(msg, SEG6_ATTR_DST, sizeof(struct in6_addr), tun_src))
  166. goto nla_put_failure;
  167. rcu_read_unlock();
  168. genlmsg_end(msg, hdr);
  169. genlmsg_reply(msg, info);
  170. return 0;
  171. nla_put_failure:
  172. rcu_read_unlock();
  173. genlmsg_cancel(msg, hdr);
  174. free_msg:
  175. nlmsg_free(msg);
  176. return -ENOMEM;
  177. }
  178. #ifdef CONFIG_IPV6_SEG6_HMAC
  179. static int __seg6_hmac_fill_info(struct seg6_hmac_info *hinfo,
  180. struct sk_buff *msg)
  181. {
  182. if (nla_put_u32(msg, SEG6_ATTR_HMACKEYID, hinfo->hmackeyid) ||
  183. nla_put_u8(msg, SEG6_ATTR_SECRETLEN, hinfo->slen) ||
  184. nla_put(msg, SEG6_ATTR_SECRET, hinfo->slen, hinfo->secret) ||
  185. nla_put_u8(msg, SEG6_ATTR_ALGID, hinfo->alg_id))
  186. return -1;
  187. return 0;
  188. }
  189. static int __seg6_genl_dumphmac_element(struct seg6_hmac_info *hinfo,
  190. u32 portid, u32 seq, u32 flags,
  191. struct sk_buff *skb, u8 cmd)
  192. {
  193. void *hdr;
  194. hdr = genlmsg_put(skb, portid, seq, &seg6_genl_family, flags, cmd);
  195. if (!hdr)
  196. return -ENOMEM;
  197. if (__seg6_hmac_fill_info(hinfo, skb) < 0)
  198. goto nla_put_failure;
  199. genlmsg_end(skb, hdr);
  200. return 0;
  201. nla_put_failure:
  202. genlmsg_cancel(skb, hdr);
  203. return -EMSGSIZE;
  204. }
  205. static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
  206. {
  207. struct net *net = sock_net(cb->skb->sk);
  208. struct seg6_pernet_data *sdata;
  209. struct rhashtable_iter *iter;
  210. sdata = seg6_pernet(net);
  211. iter = (struct rhashtable_iter *)cb->args[0];
  212. if (!iter) {
  213. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  214. if (!iter)
  215. return -ENOMEM;
  216. cb->args[0] = (long)iter;
  217. }
  218. rhashtable_walk_enter(&sdata->hmac_infos, iter);
  219. return 0;
  220. }
  221. static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
  222. {
  223. struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
  224. rhashtable_walk_exit(iter);
  225. kfree(iter);
  226. return 0;
  227. }
  228. static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
  229. {
  230. struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
  231. struct seg6_hmac_info *hinfo;
  232. int ret;
  233. ret = rhashtable_walk_start(iter);
  234. if (ret && ret != -EAGAIN)
  235. goto done;
  236. for (;;) {
  237. hinfo = rhashtable_walk_next(iter);
  238. if (IS_ERR(hinfo)) {
  239. if (PTR_ERR(hinfo) == -EAGAIN)
  240. continue;
  241. ret = PTR_ERR(hinfo);
  242. goto done;
  243. } else if (!hinfo) {
  244. break;
  245. }
  246. ret = __seg6_genl_dumphmac_element(hinfo,
  247. NETLINK_CB(cb->skb).portid,
  248. cb->nlh->nlmsg_seq,
  249. NLM_F_MULTI,
  250. skb, SEG6_CMD_DUMPHMAC);
  251. if (ret)
  252. goto done;
  253. }
  254. ret = skb->len;
  255. done:
  256. rhashtable_walk_stop(iter);
  257. return ret;
  258. }
  259. #else
  260. static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
  261. {
  262. return 0;
  263. }
  264. static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
  265. {
  266. return 0;
  267. }
  268. static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
  269. {
  270. return -ENOTSUPP;
  271. }
  272. #endif
  273. static int __net_init seg6_net_init(struct net *net)
  274. {
  275. struct seg6_pernet_data *sdata;
  276. sdata = kzalloc(sizeof(*sdata), GFP_KERNEL);
  277. if (!sdata)
  278. return -ENOMEM;
  279. mutex_init(&sdata->lock);
  280. sdata->tun_src = kzalloc(sizeof(*sdata->tun_src), GFP_KERNEL);
  281. if (!sdata->tun_src) {
  282. kfree(sdata);
  283. return -ENOMEM;
  284. }
  285. net->ipv6.seg6_data = sdata;
  286. #ifdef CONFIG_IPV6_SEG6_HMAC
  287. seg6_hmac_net_init(net);
  288. #endif
  289. return 0;
  290. }
  291. static void __net_exit seg6_net_exit(struct net *net)
  292. {
  293. struct seg6_pernet_data *sdata = seg6_pernet(net);
  294. #ifdef CONFIG_IPV6_SEG6_HMAC
  295. seg6_hmac_net_exit(net);
  296. #endif
  297. kfree(sdata->tun_src);
  298. kfree(sdata);
  299. }
  300. static struct pernet_operations ip6_segments_ops = {
  301. .init = seg6_net_init,
  302. .exit = seg6_net_exit,
  303. };
  304. static const struct genl_ops seg6_genl_ops[] = {
  305. {
  306. .cmd = SEG6_CMD_SETHMAC,
  307. .doit = seg6_genl_sethmac,
  308. .policy = seg6_genl_policy,
  309. .flags = GENL_ADMIN_PERM,
  310. },
  311. {
  312. .cmd = SEG6_CMD_DUMPHMAC,
  313. .start = seg6_genl_dumphmac_start,
  314. .dumpit = seg6_genl_dumphmac,
  315. .done = seg6_genl_dumphmac_done,
  316. .policy = seg6_genl_policy,
  317. .flags = GENL_ADMIN_PERM,
  318. },
  319. {
  320. .cmd = SEG6_CMD_SET_TUNSRC,
  321. .doit = seg6_genl_set_tunsrc,
  322. .policy = seg6_genl_policy,
  323. .flags = GENL_ADMIN_PERM,
  324. },
  325. {
  326. .cmd = SEG6_CMD_GET_TUNSRC,
  327. .doit = seg6_genl_get_tunsrc,
  328. .policy = seg6_genl_policy,
  329. .flags = GENL_ADMIN_PERM,
  330. },
  331. };
  332. static struct genl_family seg6_genl_family __ro_after_init = {
  333. .hdrsize = 0,
  334. .name = SEG6_GENL_NAME,
  335. .version = SEG6_GENL_VERSION,
  336. .maxattr = SEG6_ATTR_MAX,
  337. .netnsok = true,
  338. .parallel_ops = true,
  339. .ops = seg6_genl_ops,
  340. .n_ops = ARRAY_SIZE(seg6_genl_ops),
  341. .module = THIS_MODULE,
  342. };
  343. int __init seg6_init(void)
  344. {
  345. int err = -ENOMEM;
  346. err = genl_register_family(&seg6_genl_family);
  347. if (err)
  348. goto out;
  349. err = register_pernet_subsys(&ip6_segments_ops);
  350. if (err)
  351. goto out_unregister_genl;
  352. #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
  353. err = seg6_iptunnel_init();
  354. if (err)
  355. goto out_unregister_pernet;
  356. err = seg6_local_init();
  357. if (err)
  358. goto out_unregister_pernet;
  359. #endif
  360. #ifdef CONFIG_IPV6_SEG6_HMAC
  361. err = seg6_hmac_init();
  362. if (err)
  363. goto out_unregister_iptun;
  364. #endif
  365. pr_info("Segment Routing with IPv6\n");
  366. out:
  367. return err;
  368. #ifdef CONFIG_IPV6_SEG6_HMAC
  369. out_unregister_iptun:
  370. #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
  371. seg6_local_exit();
  372. seg6_iptunnel_exit();
  373. #endif
  374. #endif
  375. #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
  376. out_unregister_pernet:
  377. unregister_pernet_subsys(&ip6_segments_ops);
  378. #endif
  379. out_unregister_genl:
  380. genl_unregister_family(&seg6_genl_family);
  381. goto out;
  382. }
  383. void seg6_exit(void)
  384. {
  385. #ifdef CONFIG_IPV6_SEG6_HMAC
  386. seg6_hmac_exit();
  387. #endif
  388. #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
  389. seg6_iptunnel_exit();
  390. #endif
  391. unregister_pernet_subsys(&ip6_segments_ops);
  392. genl_unregister_family(&seg6_genl_family);
  393. }