seg6.c 9.8 KB

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