addrlabel.c 14 KB

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