seg6.c 9.8 KB

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