addrlabel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * IPv6 Address Label subsystem
  3. * for the IPv6 "Default" Source Address Selection
  4. *
  5. * Copyright (C)2007 USAGI/WIDE Project
  6. */
  7. /*
  8. * Author:
  9. * YOSHIFUJI Hideaki @ USAGI/WIDE Project <yoshfuji@linux-ipv6.org>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/list.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/in6.h>
  15. #include <linux/slab.h>
  16. #include <net/addrconf.h>
  17. #include <linux/if_addrlabel.h>
  18. #include <linux/netlink.h>
  19. #include <linux/rtnetlink.h>
  20. #if 0
  21. #define ADDRLABEL(x...) printk(x)
  22. #else
  23. #define ADDRLABEL(x...) do { ; } while (0)
  24. #endif
  25. /*
  26. * Policy Table
  27. */
  28. struct ip6addrlbl_entry {
  29. possible_net_t lbl_net;
  30. struct in6_addr prefix;
  31. int prefixlen;
  32. int ifindex;
  33. int addrtype;
  34. u32 label;
  35. struct hlist_node list;
  36. atomic_t refcnt;
  37. struct rcu_head rcu;
  38. };
  39. static struct ip6addrlbl_table
  40. {
  41. struct hlist_head head;
  42. spinlock_t lock;
  43. u32 seq;
  44. } ip6addrlbl_table;
  45. static inline
  46. struct net *ip6addrlbl_net(const struct ip6addrlbl_entry *lbl)
  47. {
  48. return read_pnet(&lbl->lbl_net);
  49. }
  50. /*
  51. * Default policy table (RFC6724 + extensions)
  52. *
  53. * prefix addr_type label
  54. * -------------------------------------------------------------------------
  55. * ::1/128 LOOPBACK 0
  56. * ::/0 N/A 1
  57. * 2002::/16 N/A 2
  58. * ::/96 COMPATv4 3
  59. * ::ffff:0:0/96 V4MAPPED 4
  60. * fc00::/7 N/A 5 ULA (RFC 4193)
  61. * 2001::/32 N/A 6 Teredo (RFC 4380)
  62. * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
  63. * fec0::/10 N/A 11 Site-local
  64. * (deprecated by RFC3879)
  65. * 3ffe::/16 N/A 12 6bone
  66. *
  67. * Note: 0xffffffff is used if we do not have any policies.
  68. * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724.
  69. */
  70. #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
  71. static const __net_initconst struct ip6addrlbl_init_table
  72. {
  73. const struct in6_addr *prefix;
  74. int prefixlen;
  75. u32 label;
  76. } ip6addrlbl_init_table[] = {
  77. { /* ::/0 */
  78. .prefix = &in6addr_any,
  79. .label = 1,
  80. }, { /* fc00::/7 */
  81. .prefix = &(struct in6_addr){ { { 0xfc } } } ,
  82. .prefixlen = 7,
  83. .label = 5,
  84. }, { /* fec0::/10 */
  85. .prefix = &(struct in6_addr){ { { 0xfe, 0xc0 } } },
  86. .prefixlen = 10,
  87. .label = 11,
  88. }, { /* 2002::/16 */
  89. .prefix = &(struct in6_addr){ { { 0x20, 0x02 } } },
  90. .prefixlen = 16,
  91. .label = 2,
  92. }, { /* 3ffe::/16 */
  93. .prefix = &(struct in6_addr){ { { 0x3f, 0xfe } } },
  94. .prefixlen = 16,
  95. .label = 12,
  96. }, { /* 2001::/32 */
  97. .prefix = &(struct in6_addr){ { { 0x20, 0x01 } } },
  98. .prefixlen = 32,
  99. .label = 6,
  100. }, { /* 2001:10::/28 */
  101. .prefix = &(struct in6_addr){ { { 0x20, 0x01, 0x00, 0x10 } } },
  102. .prefixlen = 28,
  103. .label = 7,
  104. }, { /* ::ffff:0:0 */
  105. .prefix = &(struct in6_addr){ { { [10] = 0xff, [11] = 0xff } } },
  106. .prefixlen = 96,
  107. .label = 4,
  108. }, { /* ::/96 */
  109. .prefix = &in6addr_any,
  110. .prefixlen = 96,
  111. .label = 3,
  112. }, { /* ::1/128 */
  113. .prefix = &in6addr_loopback,
  114. .prefixlen = 128,
  115. .label = 0,
  116. }
  117. };
  118. /* Object management */
  119. static inline void ip6addrlbl_free(struct ip6addrlbl_entry *p)
  120. {
  121. kfree(p);
  122. }
  123. static void ip6addrlbl_free_rcu(struct rcu_head *h)
  124. {
  125. ip6addrlbl_free(container_of(h, struct ip6addrlbl_entry, rcu));
  126. }
  127. static bool ip6addrlbl_hold(struct ip6addrlbl_entry *p)
  128. {
  129. return atomic_inc_not_zero(&p->refcnt);
  130. }
  131. static inline void ip6addrlbl_put(struct ip6addrlbl_entry *p)
  132. {
  133. if (atomic_dec_and_test(&p->refcnt))
  134. call_rcu(&p->rcu, ip6addrlbl_free_rcu);
  135. }
  136. /* Find label */
  137. static bool __ip6addrlbl_match(struct net *net,
  138. const struct ip6addrlbl_entry *p,
  139. const struct in6_addr *addr,
  140. int addrtype, int ifindex)
  141. {
  142. if (!net_eq(ip6addrlbl_net(p), net))
  143. return false;
  144. if (p->ifindex && p->ifindex != ifindex)
  145. return false;
  146. if (p->addrtype && p->addrtype != addrtype)
  147. return false;
  148. if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
  149. return false;
  150. return true;
  151. }
  152. static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net,
  153. const struct in6_addr *addr,
  154. int type, int ifindex)
  155. {
  156. struct ip6addrlbl_entry *p;
  157. hlist_for_each_entry_rcu(p, &ip6addrlbl_table.head, list) {
  158. if (__ip6addrlbl_match(net, p, addr, type, ifindex))
  159. return p;
  160. }
  161. return NULL;
  162. }
  163. u32 ipv6_addr_label(struct net *net,
  164. const struct in6_addr *addr, int type, int ifindex)
  165. {
  166. u32 label;
  167. struct ip6addrlbl_entry *p;
  168. type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK;
  169. rcu_read_lock();
  170. p = __ipv6_addr_label(net, addr, type, ifindex);
  171. label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
  172. rcu_read_unlock();
  173. ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
  174. __func__, addr, type, ifindex, label);
  175. return label;
  176. }
  177. /* allocate one entry */
  178. static struct ip6addrlbl_entry *ip6addrlbl_alloc(struct net *net,
  179. const struct in6_addr *prefix,
  180. int prefixlen, int ifindex,
  181. u32 label)
  182. {
  183. struct ip6addrlbl_entry *newp;
  184. int addrtype;
  185. ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
  186. __func__, prefix, prefixlen, ifindex, (unsigned int)label);
  187. addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK);
  188. switch (addrtype) {
  189. case IPV6_ADDR_MAPPED:
  190. if (prefixlen > 96)
  191. return ERR_PTR(-EINVAL);
  192. if (prefixlen < 96)
  193. addrtype = 0;
  194. break;
  195. case IPV6_ADDR_COMPATv4:
  196. if (prefixlen != 96)
  197. addrtype = 0;
  198. break;
  199. case IPV6_ADDR_LOOPBACK:
  200. if (prefixlen != 128)
  201. addrtype = 0;
  202. break;
  203. }
  204. newp = kmalloc(sizeof(*newp), GFP_KERNEL);
  205. if (!newp)
  206. return ERR_PTR(-ENOMEM);
  207. ipv6_addr_prefix(&newp->prefix, prefix, prefixlen);
  208. newp->prefixlen = prefixlen;
  209. newp->ifindex = ifindex;
  210. newp->addrtype = addrtype;
  211. newp->label = label;
  212. INIT_HLIST_NODE(&newp->list);
  213. write_pnet(&newp->lbl_net, net);
  214. atomic_set(&newp->refcnt, 1);
  215. return newp;
  216. }
  217. /* add a label */
  218. static int __ip6addrlbl_add(struct ip6addrlbl_entry *newp, int replace)
  219. {
  220. struct hlist_node *n;
  221. struct ip6addrlbl_entry *last = NULL, *p = NULL;
  222. int ret = 0;
  223. ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n", __func__, newp,
  224. replace);
  225. hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
  226. if (p->prefixlen == newp->prefixlen &&
  227. net_eq(ip6addrlbl_net(p), ip6addrlbl_net(newp)) &&
  228. p->ifindex == newp->ifindex &&
  229. ipv6_addr_equal(&p->prefix, &newp->prefix)) {
  230. if (!replace) {
  231. ret = -EEXIST;
  232. goto out;
  233. }
  234. hlist_replace_rcu(&p->list, &newp->list);
  235. ip6addrlbl_put(p);
  236. goto out;
  237. } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) ||
  238. (p->prefixlen < newp->prefixlen)) {
  239. hlist_add_before_rcu(&newp->list, &p->list);
  240. goto out;
  241. }
  242. last = p;
  243. }
  244. if (last)
  245. hlist_add_behind_rcu(&newp->list, &last->list);
  246. else
  247. hlist_add_head_rcu(&newp->list, &ip6addrlbl_table.head);
  248. out:
  249. if (!ret)
  250. ip6addrlbl_table.seq++;
  251. return ret;
  252. }
  253. /* add a label */
  254. static int ip6addrlbl_add(struct net *net,
  255. const struct in6_addr *prefix, int prefixlen,
  256. int ifindex, u32 label, int replace)
  257. {
  258. struct ip6addrlbl_entry *newp;
  259. int ret = 0;
  260. ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
  261. __func__, prefix, prefixlen, ifindex, (unsigned int)label,
  262. replace);
  263. newp = ip6addrlbl_alloc(net, prefix, prefixlen, ifindex, label);
  264. if (IS_ERR(newp))
  265. return PTR_ERR(newp);
  266. spin_lock(&ip6addrlbl_table.lock);
  267. ret = __ip6addrlbl_add(newp, replace);
  268. spin_unlock(&ip6addrlbl_table.lock);
  269. if (ret)
  270. ip6addrlbl_free(newp);
  271. return ret;
  272. }
  273. /* remove a label */
  274. static int __ip6addrlbl_del(struct net *net,
  275. const struct in6_addr *prefix, int prefixlen,
  276. int ifindex)
  277. {
  278. struct ip6addrlbl_entry *p = NULL;
  279. struct hlist_node *n;
  280. int ret = -ESRCH;
  281. ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
  282. __func__, prefix, prefixlen, ifindex);
  283. hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
  284. if (p->prefixlen == prefixlen &&
  285. net_eq(ip6addrlbl_net(p), net) &&
  286. p->ifindex == ifindex &&
  287. ipv6_addr_equal(&p->prefix, prefix)) {
  288. hlist_del_rcu(&p->list);
  289. ip6addrlbl_put(p);
  290. ret = 0;
  291. break;
  292. }
  293. }
  294. return ret;
  295. }
  296. static int ip6addrlbl_del(struct net *net,
  297. const struct in6_addr *prefix, int prefixlen,
  298. int ifindex)
  299. {
  300. struct in6_addr prefix_buf;
  301. int ret;
  302. ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
  303. __func__, prefix, prefixlen, ifindex);
  304. ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
  305. spin_lock(&ip6addrlbl_table.lock);
  306. ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
  307. spin_unlock(&ip6addrlbl_table.lock);
  308. return ret;
  309. }
  310. /* add default label */
  311. static int __net_init ip6addrlbl_net_init(struct net *net)
  312. {
  313. int err = 0;
  314. int i;
  315. ADDRLABEL(KERN_DEBUG "%s\n", __func__);
  316. for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
  317. int ret = ip6addrlbl_add(net,
  318. ip6addrlbl_init_table[i].prefix,
  319. ip6addrlbl_init_table[i].prefixlen,
  320. 0,
  321. ip6addrlbl_init_table[i].label, 0);
  322. /* XXX: should we free all rules when we catch an error? */
  323. if (ret && (!err || err != -ENOMEM))
  324. err = ret;
  325. }
  326. return err;
  327. }
  328. static void __net_exit ip6addrlbl_net_exit(struct net *net)
  329. {
  330. struct ip6addrlbl_entry *p = NULL;
  331. struct hlist_node *n;
  332. /* Remove all labels belonging to the exiting net */
  333. spin_lock(&ip6addrlbl_table.lock);
  334. hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
  335. if (net_eq(ip6addrlbl_net(p), net)) {
  336. hlist_del_rcu(&p->list);
  337. ip6addrlbl_put(p);
  338. }
  339. }
  340. spin_unlock(&ip6addrlbl_table.lock);
  341. }
  342. static struct pernet_operations ipv6_addr_label_ops = {
  343. .init = ip6addrlbl_net_init,
  344. .exit = ip6addrlbl_net_exit,
  345. };
  346. int __init ipv6_addr_label_init(void)
  347. {
  348. spin_lock_init(&ip6addrlbl_table.lock);
  349. return register_pernet_subsys(&ipv6_addr_label_ops);
  350. }
  351. void ipv6_addr_label_cleanup(void)
  352. {
  353. unregister_pernet_subsys(&ipv6_addr_label_ops);
  354. }
  355. static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
  356. [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), },
  357. [IFAL_LABEL] = { .len = sizeof(u32), },
  358. };
  359. static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh)
  360. {
  361. struct net *net = sock_net(skb->sk);
  362. struct ifaddrlblmsg *ifal;
  363. struct nlattr *tb[IFAL_MAX+1];
  364. struct in6_addr *pfx;
  365. u32 label;
  366. int err = 0;
  367. err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
  368. if (err < 0)
  369. return err;
  370. ifal = nlmsg_data(nlh);
  371. if (ifal->ifal_family != AF_INET6 ||
  372. ifal->ifal_prefixlen > 128)
  373. return -EINVAL;
  374. if (!tb[IFAL_ADDRESS])
  375. return -EINVAL;
  376. pfx = nla_data(tb[IFAL_ADDRESS]);
  377. if (!tb[IFAL_LABEL])
  378. return -EINVAL;
  379. label = nla_get_u32(tb[IFAL_LABEL]);
  380. if (label == IPV6_ADDR_LABEL_DEFAULT)
  381. return -EINVAL;
  382. switch (nlh->nlmsg_type) {
  383. case RTM_NEWADDRLABEL:
  384. if (ifal->ifal_index &&
  385. !__dev_get_by_index(net, ifal->ifal_index))
  386. return -EINVAL;
  387. err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen,
  388. ifal->ifal_index, label,
  389. nlh->nlmsg_flags & NLM_F_REPLACE);
  390. break;
  391. case RTM_DELADDRLABEL:
  392. err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen,
  393. ifal->ifal_index);
  394. break;
  395. default:
  396. err = -EOPNOTSUPP;
  397. }
  398. return err;
  399. }
  400. static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
  401. int prefixlen, int ifindex, u32 lseq)
  402. {
  403. struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
  404. ifal->ifal_family = AF_INET6;
  405. ifal->ifal_prefixlen = prefixlen;
  406. ifal->ifal_flags = 0;
  407. ifal->ifal_index = ifindex;
  408. ifal->ifal_seq = lseq;
  409. };
  410. static int ip6addrlbl_fill(struct sk_buff *skb,
  411. struct ip6addrlbl_entry *p,
  412. u32 lseq,
  413. u32 portid, u32 seq, int event,
  414. unsigned int flags)
  415. {
  416. struct nlmsghdr *nlh = nlmsg_put(skb, portid, seq, event,
  417. sizeof(struct ifaddrlblmsg), flags);
  418. if (!nlh)
  419. return -EMSGSIZE;
  420. ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
  421. if (nla_put_in6_addr(skb, IFAL_ADDRESS, &p->prefix) < 0 ||
  422. nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
  423. nlmsg_cancel(skb, nlh);
  424. return -EMSGSIZE;
  425. }
  426. nlmsg_end(skb, nlh);
  427. return 0;
  428. }
  429. static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
  430. {
  431. struct net *net = sock_net(skb->sk);
  432. struct ip6addrlbl_entry *p;
  433. int idx = 0, s_idx = cb->args[0];
  434. int err;
  435. rcu_read_lock();
  436. hlist_for_each_entry_rcu(p, &ip6addrlbl_table.head, list) {
  437. if (idx >= s_idx &&
  438. net_eq(ip6addrlbl_net(p), net)) {
  439. err = ip6addrlbl_fill(skb, p,
  440. ip6addrlbl_table.seq,
  441. NETLINK_CB(cb->skb).portid,
  442. cb->nlh->nlmsg_seq,
  443. RTM_NEWADDRLABEL,
  444. NLM_F_MULTI);
  445. if (err < 0)
  446. break;
  447. }
  448. idx++;
  449. }
  450. rcu_read_unlock();
  451. cb->args[0] = idx;
  452. return skb->len;
  453. }
  454. static inline int ip6addrlbl_msgsize(void)
  455. {
  456. return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
  457. + nla_total_size(16) /* IFAL_ADDRESS */
  458. + nla_total_size(4); /* IFAL_LABEL */
  459. }
  460. static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh)
  461. {
  462. struct net *net = sock_net(in_skb->sk);
  463. struct ifaddrlblmsg *ifal;
  464. struct nlattr *tb[IFAL_MAX+1];
  465. struct in6_addr *addr;
  466. u32 lseq;
  467. int err = 0;
  468. struct ip6addrlbl_entry *p;
  469. struct sk_buff *skb;
  470. err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
  471. if (err < 0)
  472. return err;
  473. ifal = nlmsg_data(nlh);
  474. if (ifal->ifal_family != AF_INET6 ||
  475. ifal->ifal_prefixlen != 128)
  476. return -EINVAL;
  477. if (ifal->ifal_index &&
  478. !__dev_get_by_index(net, ifal->ifal_index))
  479. return -EINVAL;
  480. if (!tb[IFAL_ADDRESS])
  481. return -EINVAL;
  482. addr = nla_data(tb[IFAL_ADDRESS]);
  483. rcu_read_lock();
  484. p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
  485. if (p && !ip6addrlbl_hold(p))
  486. p = NULL;
  487. lseq = ip6addrlbl_table.seq;
  488. rcu_read_unlock();
  489. if (!p) {
  490. err = -ESRCH;
  491. goto out;
  492. }
  493. skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL);
  494. if (!skb) {
  495. ip6addrlbl_put(p);
  496. return -ENOBUFS;
  497. }
  498. err = ip6addrlbl_fill(skb, p, lseq,
  499. NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  500. RTM_NEWADDRLABEL, 0);
  501. ip6addrlbl_put(p);
  502. if (err < 0) {
  503. WARN_ON(err == -EMSGSIZE);
  504. kfree_skb(skb);
  505. goto out;
  506. }
  507. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  508. out:
  509. return err;
  510. }
  511. void __init ipv6_addr_label_rtnl_register(void)
  512. {
  513. __rtnl_register(PF_INET6, RTM_NEWADDRLABEL, ip6addrlbl_newdel,
  514. NULL, NULL);
  515. __rtnl_register(PF_INET6, RTM_DELADDRLABEL, ip6addrlbl_newdel,
  516. NULL, NULL);
  517. __rtnl_register(PF_INET6, RTM_GETADDRLABEL, ip6addrlbl_get,
  518. ip6addrlbl_dump, NULL);
  519. }