af_mpls.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/vmalloc.h>
  11. #include <net/ip.h>
  12. #include <net/dst.h>
  13. #include <net/sock.h>
  14. #include <net/arp.h>
  15. #include <net/ip_fib.h>
  16. #include <net/netevent.h>
  17. #include <net/netns/generic.h>
  18. #include "internal.h"
  19. #define LABEL_NOT_SPECIFIED (1<<20)
  20. #define MAX_NEW_LABELS 2
  21. /* This maximum ha length copied from the definition of struct neighbour */
  22. #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
  23. struct mpls_route { /* next hop label forwarding entry */
  24. struct net_device __rcu *rt_dev;
  25. struct rcu_head rt_rcu;
  26. u32 rt_label[MAX_NEW_LABELS];
  27. u8 rt_protocol; /* routing protocol that set this entry */
  28. u8 rt_labels;
  29. u8 rt_via_alen;
  30. u8 rt_via_table;
  31. u8 rt_via[0];
  32. };
  33. static int zero = 0;
  34. static int label_limit = (1 << 20) - 1;
  35. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  36. struct nlmsghdr *nlh, struct net *net, u32 portid,
  37. unsigned int nlm_flags);
  38. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  39. {
  40. struct mpls_route *rt = NULL;
  41. if (index < net->mpls.platform_labels) {
  42. struct mpls_route __rcu **platform_label =
  43. rcu_dereference(net->mpls.platform_label);
  44. rt = rcu_dereference(platform_label[index]);
  45. }
  46. return rt;
  47. }
  48. static bool mpls_output_possible(const struct net_device *dev)
  49. {
  50. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  51. }
  52. static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
  53. {
  54. /* The size of the layer 2.5 labels to be added for this route */
  55. return rt->rt_labels * sizeof(struct mpls_shim_hdr);
  56. }
  57. static unsigned int mpls_dev_mtu(const struct net_device *dev)
  58. {
  59. /* The amount of data the layer 2 frame can hold */
  60. return dev->mtu;
  61. }
  62. static bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  63. {
  64. if (skb->len <= mtu)
  65. return false;
  66. if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
  67. return false;
  68. return true;
  69. }
  70. static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
  71. struct mpls_entry_decoded dec)
  72. {
  73. /* RFC4385 and RFC5586 encode other packets in mpls such that
  74. * they don't conflict with the ip version number, making
  75. * decoding by examining the ip version correct in everything
  76. * except for the strangest cases.
  77. *
  78. * The strange cases if we choose to support them will require
  79. * manual configuration.
  80. */
  81. struct iphdr *hdr4;
  82. bool success = true;
  83. /* The IPv4 code below accesses through the IPv4 header
  84. * checksum, which is 12 bytes into the packet.
  85. * The IPv6 code below accesses through the IPv6 hop limit
  86. * which is 8 bytes into the packet.
  87. *
  88. * For all supported cases there should always be at least 12
  89. * bytes of packet data present. The IPv4 header is 20 bytes
  90. * without options and the IPv6 header is always 40 bytes
  91. * long.
  92. */
  93. if (!pskb_may_pull(skb, 12))
  94. return false;
  95. /* Use ip_hdr to find the ip protocol version */
  96. hdr4 = ip_hdr(skb);
  97. if (hdr4->version == 4) {
  98. skb->protocol = htons(ETH_P_IP);
  99. csum_replace2(&hdr4->check,
  100. htons(hdr4->ttl << 8),
  101. htons(dec.ttl << 8));
  102. hdr4->ttl = dec.ttl;
  103. }
  104. else if (hdr4->version == 6) {
  105. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  106. skb->protocol = htons(ETH_P_IPV6);
  107. hdr6->hop_limit = dec.ttl;
  108. }
  109. else
  110. /* version 0 and version 1 are used by pseudo wires */
  111. success = false;
  112. return success;
  113. }
  114. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  115. struct packet_type *pt, struct net_device *orig_dev)
  116. {
  117. struct net *net = dev_net(dev);
  118. struct mpls_shim_hdr *hdr;
  119. struct mpls_route *rt;
  120. struct mpls_entry_decoded dec;
  121. struct net_device *out_dev;
  122. unsigned int hh_len;
  123. unsigned int new_header_size;
  124. unsigned int mtu;
  125. int err;
  126. /* Careful this entire function runs inside of an rcu critical section */
  127. if (skb->pkt_type != PACKET_HOST)
  128. goto drop;
  129. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  130. goto drop;
  131. if (!pskb_may_pull(skb, sizeof(*hdr)))
  132. goto drop;
  133. /* Read and decode the label */
  134. hdr = mpls_hdr(skb);
  135. dec = mpls_entry_decode(hdr);
  136. /* Pop the label */
  137. skb_pull(skb, sizeof(*hdr));
  138. skb_reset_network_header(skb);
  139. skb_orphan(skb);
  140. rt = mpls_route_input_rcu(net, dec.label);
  141. if (!rt)
  142. goto drop;
  143. /* Find the output device */
  144. out_dev = rcu_dereference(rt->rt_dev);
  145. if (!mpls_output_possible(out_dev))
  146. goto drop;
  147. if (skb_warn_if_lro(skb))
  148. goto drop;
  149. skb_forward_csum(skb);
  150. /* Verify ttl is valid */
  151. if (dec.ttl <= 1)
  152. goto drop;
  153. dec.ttl -= 1;
  154. /* Verify the destination can hold the packet */
  155. new_header_size = mpls_rt_header_size(rt);
  156. mtu = mpls_dev_mtu(out_dev);
  157. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  158. goto drop;
  159. hh_len = LL_RESERVED_SPACE(out_dev);
  160. if (!out_dev->header_ops)
  161. hh_len = 0;
  162. /* Ensure there is enough space for the headers in the skb */
  163. if (skb_cow(skb, hh_len + new_header_size))
  164. goto drop;
  165. skb->dev = out_dev;
  166. skb->protocol = htons(ETH_P_MPLS_UC);
  167. if (unlikely(!new_header_size && dec.bos)) {
  168. /* Penultimate hop popping */
  169. if (!mpls_egress(rt, skb, dec))
  170. goto drop;
  171. } else {
  172. bool bos;
  173. int i;
  174. skb_push(skb, new_header_size);
  175. skb_reset_network_header(skb);
  176. /* Push the new labels */
  177. hdr = mpls_hdr(skb);
  178. bos = dec.bos;
  179. for (i = rt->rt_labels - 1; i >= 0; i--) {
  180. hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
  181. bos = false;
  182. }
  183. }
  184. err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
  185. if (err)
  186. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  187. __func__, err);
  188. return 0;
  189. drop:
  190. kfree_skb(skb);
  191. return NET_RX_DROP;
  192. }
  193. static struct packet_type mpls_packet_type __read_mostly = {
  194. .type = cpu_to_be16(ETH_P_MPLS_UC),
  195. .func = mpls_forward,
  196. };
  197. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  198. [RTA_DST] = { .type = NLA_U32 },
  199. [RTA_OIF] = { .type = NLA_U32 },
  200. };
  201. struct mpls_route_config {
  202. u32 rc_protocol;
  203. u32 rc_ifindex;
  204. u16 rc_via_table;
  205. u16 rc_via_alen;
  206. u8 rc_via[MAX_VIA_ALEN];
  207. u32 rc_label;
  208. u32 rc_output_labels;
  209. u32 rc_output_label[MAX_NEW_LABELS];
  210. u32 rc_nlflags;
  211. struct nl_info rc_nlinfo;
  212. };
  213. static struct mpls_route *mpls_rt_alloc(size_t alen)
  214. {
  215. struct mpls_route *rt;
  216. rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
  217. if (rt)
  218. rt->rt_via_alen = alen;
  219. return rt;
  220. }
  221. static void mpls_rt_free(struct mpls_route *rt)
  222. {
  223. if (rt)
  224. kfree_rcu(rt, rt_rcu);
  225. }
  226. static void mpls_notify_route(struct net *net, unsigned index,
  227. struct mpls_route *old, struct mpls_route *new,
  228. const struct nl_info *info)
  229. {
  230. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  231. unsigned portid = info ? info->portid : 0;
  232. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  233. struct mpls_route *rt = new ? new : old;
  234. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  235. /* Ignore reserved labels for now */
  236. if (rt && (index >= 16))
  237. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  238. }
  239. static void mpls_route_update(struct net *net, unsigned index,
  240. struct net_device *dev, struct mpls_route *new,
  241. const struct nl_info *info)
  242. {
  243. struct mpls_route __rcu **platform_label;
  244. struct mpls_route *rt, *old = NULL;
  245. ASSERT_RTNL();
  246. platform_label = rtnl_dereference(net->mpls.platform_label);
  247. rt = rtnl_dereference(platform_label[index]);
  248. if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
  249. rcu_assign_pointer(platform_label[index], new);
  250. old = rt;
  251. }
  252. mpls_notify_route(net, index, old, new, info);
  253. /* If we removed a route free it now */
  254. mpls_rt_free(old);
  255. }
  256. static unsigned find_free_label(struct net *net)
  257. {
  258. struct mpls_route __rcu **platform_label;
  259. size_t platform_labels;
  260. unsigned index;
  261. platform_label = rtnl_dereference(net->mpls.platform_label);
  262. platform_labels = net->mpls.platform_labels;
  263. for (index = 16; index < platform_labels; index++) {
  264. if (!rtnl_dereference(platform_label[index]))
  265. return index;
  266. }
  267. return LABEL_NOT_SPECIFIED;
  268. }
  269. static int mpls_route_add(struct mpls_route_config *cfg)
  270. {
  271. struct mpls_route __rcu **platform_label;
  272. struct net *net = cfg->rc_nlinfo.nl_net;
  273. struct net_device *dev = NULL;
  274. struct mpls_route *rt, *old;
  275. unsigned index;
  276. int i;
  277. int err = -EINVAL;
  278. index = cfg->rc_label;
  279. /* If a label was not specified during insert pick one */
  280. if ((index == LABEL_NOT_SPECIFIED) &&
  281. (cfg->rc_nlflags & NLM_F_CREATE)) {
  282. index = find_free_label(net);
  283. }
  284. /* The first 16 labels are reserved, and may not be set */
  285. if (index < 16)
  286. goto errout;
  287. /* The full 20 bit range may not be supported. */
  288. if (index >= net->mpls.platform_labels)
  289. goto errout;
  290. /* Ensure only a supported number of labels are present */
  291. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  292. goto errout;
  293. err = -ENODEV;
  294. dev = dev_get_by_index(net, cfg->rc_ifindex);
  295. if (!dev)
  296. goto errout;
  297. /* For now just support ethernet devices */
  298. err = -EINVAL;
  299. if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
  300. goto errout;
  301. err = -EINVAL;
  302. if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
  303. (dev->addr_len != cfg->rc_via_alen))
  304. goto errout;
  305. /* Append makes no sense with mpls */
  306. err = -EOPNOTSUPP;
  307. if (cfg->rc_nlflags & NLM_F_APPEND)
  308. goto errout;
  309. err = -EEXIST;
  310. platform_label = rtnl_dereference(net->mpls.platform_label);
  311. old = rtnl_dereference(platform_label[index]);
  312. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  313. goto errout;
  314. err = -EEXIST;
  315. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  316. goto errout;
  317. err = -ENOENT;
  318. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  319. goto errout;
  320. err = -ENOMEM;
  321. rt = mpls_rt_alloc(cfg->rc_via_alen);
  322. if (!rt)
  323. goto errout;
  324. rt->rt_labels = cfg->rc_output_labels;
  325. for (i = 0; i < rt->rt_labels; i++)
  326. rt->rt_label[i] = cfg->rc_output_label[i];
  327. rt->rt_protocol = cfg->rc_protocol;
  328. RCU_INIT_POINTER(rt->rt_dev, dev);
  329. rt->rt_via_table = cfg->rc_via_table;
  330. memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
  331. mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
  332. dev_put(dev);
  333. return 0;
  334. errout:
  335. if (dev)
  336. dev_put(dev);
  337. return err;
  338. }
  339. static int mpls_route_del(struct mpls_route_config *cfg)
  340. {
  341. struct net *net = cfg->rc_nlinfo.nl_net;
  342. unsigned index;
  343. int err = -EINVAL;
  344. index = cfg->rc_label;
  345. /* The first 16 labels are reserved, and may not be removed */
  346. if (index < 16)
  347. goto errout;
  348. /* The full 20 bit range may not be supported */
  349. if (index >= net->mpls.platform_labels)
  350. goto errout;
  351. mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
  352. err = 0;
  353. errout:
  354. return err;
  355. }
  356. static void mpls_ifdown(struct net_device *dev)
  357. {
  358. struct mpls_route __rcu **platform_label;
  359. struct net *net = dev_net(dev);
  360. unsigned index;
  361. platform_label = rtnl_dereference(net->mpls.platform_label);
  362. for (index = 0; index < net->mpls.platform_labels; index++) {
  363. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  364. if (!rt)
  365. continue;
  366. if (rtnl_dereference(rt->rt_dev) != dev)
  367. continue;
  368. rt->rt_dev = NULL;
  369. }
  370. }
  371. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  372. void *ptr)
  373. {
  374. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  375. switch(event) {
  376. case NETDEV_UNREGISTER:
  377. mpls_ifdown(dev);
  378. break;
  379. }
  380. return NOTIFY_OK;
  381. }
  382. static struct notifier_block mpls_dev_notifier = {
  383. .notifier_call = mpls_dev_notify,
  384. };
  385. static int nla_put_via(struct sk_buff *skb,
  386. u8 table, const void *addr, int alen)
  387. {
  388. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  389. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  390. };
  391. struct nlattr *nla;
  392. struct rtvia *via;
  393. int family = AF_UNSPEC;
  394. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  395. if (!nla)
  396. return -EMSGSIZE;
  397. if (table <= NEIGH_NR_TABLES)
  398. family = table_to_family[table];
  399. via = nla_data(nla);
  400. via->rtvia_family = family;
  401. memcpy(via->rtvia_addr, addr, alen);
  402. return 0;
  403. }
  404. int nla_put_labels(struct sk_buff *skb, int attrtype,
  405. u8 labels, const u32 label[])
  406. {
  407. struct nlattr *nla;
  408. struct mpls_shim_hdr *nla_label;
  409. bool bos;
  410. int i;
  411. nla = nla_reserve(skb, attrtype, labels*4);
  412. if (!nla)
  413. return -EMSGSIZE;
  414. nla_label = nla_data(nla);
  415. bos = true;
  416. for (i = labels - 1; i >= 0; i--) {
  417. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  418. bos = false;
  419. }
  420. return 0;
  421. }
  422. int nla_get_labels(const struct nlattr *nla,
  423. u32 max_labels, u32 *labels, u32 label[])
  424. {
  425. unsigned len = nla_len(nla);
  426. unsigned nla_labels;
  427. struct mpls_shim_hdr *nla_label;
  428. bool bos;
  429. int i;
  430. /* len needs to be an even multiple of 4 (the label size) */
  431. if (len & 3)
  432. return -EINVAL;
  433. /* Limit the number of new labels allowed */
  434. nla_labels = len/4;
  435. if (nla_labels > max_labels)
  436. return -EINVAL;
  437. nla_label = nla_data(nla);
  438. bos = true;
  439. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  440. struct mpls_entry_decoded dec;
  441. dec = mpls_entry_decode(nla_label + i);
  442. /* Ensure the bottom of stack flag is properly set
  443. * and ttl and tc are both clear.
  444. */
  445. if ((dec.bos != bos) || dec.ttl || dec.tc)
  446. return -EINVAL;
  447. label[i] = dec.label;
  448. }
  449. *labels = nla_labels;
  450. return 0;
  451. }
  452. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  453. struct mpls_route_config *cfg)
  454. {
  455. struct rtmsg *rtm;
  456. struct nlattr *tb[RTA_MAX+1];
  457. int index;
  458. int err;
  459. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  460. if (err < 0)
  461. goto errout;
  462. err = -EINVAL;
  463. rtm = nlmsg_data(nlh);
  464. memset(cfg, 0, sizeof(*cfg));
  465. if (rtm->rtm_family != AF_MPLS)
  466. goto errout;
  467. if (rtm->rtm_dst_len != 20)
  468. goto errout;
  469. if (rtm->rtm_src_len != 0)
  470. goto errout;
  471. if (rtm->rtm_tos != 0)
  472. goto errout;
  473. if (rtm->rtm_table != RT_TABLE_MAIN)
  474. goto errout;
  475. /* Any value is acceptable for rtm_protocol */
  476. /* As mpls uses destination specific addresses
  477. * (or source specific address in the case of multicast)
  478. * all addresses have universal scope.
  479. */
  480. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  481. goto errout;
  482. if (rtm->rtm_type != RTN_UNICAST)
  483. goto errout;
  484. if (rtm->rtm_flags != 0)
  485. goto errout;
  486. cfg->rc_label = LABEL_NOT_SPECIFIED;
  487. cfg->rc_protocol = rtm->rtm_protocol;
  488. cfg->rc_nlflags = nlh->nlmsg_flags;
  489. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  490. cfg->rc_nlinfo.nlh = nlh;
  491. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  492. for (index = 0; index <= RTA_MAX; index++) {
  493. struct nlattr *nla = tb[index];
  494. if (!nla)
  495. continue;
  496. switch(index) {
  497. case RTA_OIF:
  498. cfg->rc_ifindex = nla_get_u32(nla);
  499. break;
  500. case RTA_NEWDST:
  501. if (nla_get_labels(nla, MAX_NEW_LABELS,
  502. &cfg->rc_output_labels,
  503. cfg->rc_output_label))
  504. goto errout;
  505. break;
  506. case RTA_DST:
  507. {
  508. u32 label_count;
  509. if (nla_get_labels(nla, 1, &label_count,
  510. &cfg->rc_label))
  511. goto errout;
  512. /* The first 16 labels are reserved, and may not be set */
  513. if (cfg->rc_label < 16)
  514. goto errout;
  515. break;
  516. }
  517. case RTA_VIA:
  518. {
  519. struct rtvia *via = nla_data(nla);
  520. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  521. goto errout;
  522. cfg->rc_via_alen = nla_len(nla) -
  523. offsetof(struct rtvia, rtvia_addr);
  524. if (cfg->rc_via_alen > MAX_VIA_ALEN)
  525. goto errout;
  526. /* Validate the address family */
  527. switch(via->rtvia_family) {
  528. case AF_PACKET:
  529. cfg->rc_via_table = NEIGH_LINK_TABLE;
  530. break;
  531. case AF_INET:
  532. cfg->rc_via_table = NEIGH_ARP_TABLE;
  533. if (cfg->rc_via_alen != 4)
  534. goto errout;
  535. break;
  536. case AF_INET6:
  537. cfg->rc_via_table = NEIGH_ND_TABLE;
  538. if (cfg->rc_via_alen != 16)
  539. goto errout;
  540. break;
  541. default:
  542. /* Unsupported address family */
  543. goto errout;
  544. }
  545. memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
  546. break;
  547. }
  548. default:
  549. /* Unsupported attribute */
  550. goto errout;
  551. }
  552. }
  553. err = 0;
  554. errout:
  555. return err;
  556. }
  557. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  558. {
  559. struct mpls_route_config cfg;
  560. int err;
  561. err = rtm_to_route_config(skb, nlh, &cfg);
  562. if (err < 0)
  563. return err;
  564. return mpls_route_del(&cfg);
  565. }
  566. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  567. {
  568. struct mpls_route_config cfg;
  569. int err;
  570. err = rtm_to_route_config(skb, nlh, &cfg);
  571. if (err < 0)
  572. return err;
  573. return mpls_route_add(&cfg);
  574. }
  575. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  576. u32 label, struct mpls_route *rt, int flags)
  577. {
  578. struct net_device *dev;
  579. struct nlmsghdr *nlh;
  580. struct rtmsg *rtm;
  581. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  582. if (nlh == NULL)
  583. return -EMSGSIZE;
  584. rtm = nlmsg_data(nlh);
  585. rtm->rtm_family = AF_MPLS;
  586. rtm->rtm_dst_len = 20;
  587. rtm->rtm_src_len = 0;
  588. rtm->rtm_tos = 0;
  589. rtm->rtm_table = RT_TABLE_MAIN;
  590. rtm->rtm_protocol = rt->rt_protocol;
  591. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  592. rtm->rtm_type = RTN_UNICAST;
  593. rtm->rtm_flags = 0;
  594. if (rt->rt_labels &&
  595. nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
  596. goto nla_put_failure;
  597. if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
  598. goto nla_put_failure;
  599. dev = rtnl_dereference(rt->rt_dev);
  600. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  601. goto nla_put_failure;
  602. if (nla_put_labels(skb, RTA_DST, 1, &label))
  603. goto nla_put_failure;
  604. nlmsg_end(skb, nlh);
  605. return 0;
  606. nla_put_failure:
  607. nlmsg_cancel(skb, nlh);
  608. return -EMSGSIZE;
  609. }
  610. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  611. {
  612. struct net *net = sock_net(skb->sk);
  613. struct mpls_route __rcu **platform_label;
  614. size_t platform_labels;
  615. unsigned int index;
  616. ASSERT_RTNL();
  617. index = cb->args[0];
  618. if (index < 16)
  619. index = 16;
  620. platform_label = rtnl_dereference(net->mpls.platform_label);
  621. platform_labels = net->mpls.platform_labels;
  622. for (; index < platform_labels; index++) {
  623. struct mpls_route *rt;
  624. rt = rtnl_dereference(platform_label[index]);
  625. if (!rt)
  626. continue;
  627. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  628. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  629. index, rt, NLM_F_MULTI) < 0)
  630. break;
  631. }
  632. cb->args[0] = index;
  633. return skb->len;
  634. }
  635. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  636. {
  637. size_t payload =
  638. NLMSG_ALIGN(sizeof(struct rtmsg))
  639. + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
  640. + nla_total_size(4); /* RTA_DST */
  641. if (rt->rt_labels) /* RTA_NEWDST */
  642. payload += nla_total_size(rt->rt_labels * 4);
  643. if (rt->rt_dev) /* RTA_OIF */
  644. payload += nla_total_size(4);
  645. return payload;
  646. }
  647. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  648. struct nlmsghdr *nlh, struct net *net, u32 portid,
  649. unsigned int nlm_flags)
  650. {
  651. struct sk_buff *skb;
  652. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  653. int err = -ENOBUFS;
  654. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  655. if (skb == NULL)
  656. goto errout;
  657. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  658. if (err < 0) {
  659. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  660. WARN_ON(err == -EMSGSIZE);
  661. kfree_skb(skb);
  662. goto errout;
  663. }
  664. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  665. return;
  666. errout:
  667. if (err < 0)
  668. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  669. }
  670. static int resize_platform_label_table(struct net *net, size_t limit)
  671. {
  672. size_t size = sizeof(struct mpls_route *) * limit;
  673. size_t old_limit;
  674. size_t cp_size;
  675. struct mpls_route __rcu **labels = NULL, **old;
  676. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  677. unsigned index;
  678. if (size) {
  679. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  680. if (!labels)
  681. labels = vzalloc(size);
  682. if (!labels)
  683. goto nolabels;
  684. }
  685. /* In case the predefined labels need to be populated */
  686. if (limit > LABEL_IPV4_EXPLICIT_NULL) {
  687. struct net_device *lo = net->loopback_dev;
  688. rt0 = mpls_rt_alloc(lo->addr_len);
  689. if (!rt0)
  690. goto nort0;
  691. RCU_INIT_POINTER(rt0->rt_dev, lo);
  692. rt0->rt_protocol = RTPROT_KERNEL;
  693. rt0->rt_via_table = NEIGH_LINK_TABLE;
  694. memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
  695. }
  696. if (limit > LABEL_IPV6_EXPLICIT_NULL) {
  697. struct net_device *lo = net->loopback_dev;
  698. rt2 = mpls_rt_alloc(lo->addr_len);
  699. if (!rt2)
  700. goto nort2;
  701. RCU_INIT_POINTER(rt2->rt_dev, lo);
  702. rt2->rt_protocol = RTPROT_KERNEL;
  703. rt2->rt_via_table = NEIGH_LINK_TABLE;
  704. memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
  705. }
  706. rtnl_lock();
  707. /* Remember the original table */
  708. old = rtnl_dereference(net->mpls.platform_label);
  709. old_limit = net->mpls.platform_labels;
  710. /* Free any labels beyond the new table */
  711. for (index = limit; index < old_limit; index++)
  712. mpls_route_update(net, index, NULL, NULL, NULL);
  713. /* Copy over the old labels */
  714. cp_size = size;
  715. if (old_limit < limit)
  716. cp_size = old_limit * sizeof(struct mpls_route *);
  717. memcpy(labels, old, cp_size);
  718. /* If needed set the predefined labels */
  719. if ((old_limit <= LABEL_IPV6_EXPLICIT_NULL) &&
  720. (limit > LABEL_IPV6_EXPLICIT_NULL)) {
  721. RCU_INIT_POINTER(labels[LABEL_IPV6_EXPLICIT_NULL], rt2);
  722. rt2 = NULL;
  723. }
  724. if ((old_limit <= LABEL_IPV4_EXPLICIT_NULL) &&
  725. (limit > LABEL_IPV4_EXPLICIT_NULL)) {
  726. RCU_INIT_POINTER(labels[LABEL_IPV4_EXPLICIT_NULL], rt0);
  727. rt0 = NULL;
  728. }
  729. /* Update the global pointers */
  730. net->mpls.platform_labels = limit;
  731. rcu_assign_pointer(net->mpls.platform_label, labels);
  732. rtnl_unlock();
  733. mpls_rt_free(rt2);
  734. mpls_rt_free(rt0);
  735. if (old) {
  736. synchronize_rcu();
  737. kvfree(old);
  738. }
  739. return 0;
  740. nort2:
  741. mpls_rt_free(rt0);
  742. nort0:
  743. kvfree(labels);
  744. nolabels:
  745. return -ENOMEM;
  746. }
  747. static int mpls_platform_labels(struct ctl_table *table, int write,
  748. void __user *buffer, size_t *lenp, loff_t *ppos)
  749. {
  750. struct net *net = table->data;
  751. int platform_labels = net->mpls.platform_labels;
  752. int ret;
  753. struct ctl_table tmp = {
  754. .procname = table->procname,
  755. .data = &platform_labels,
  756. .maxlen = sizeof(int),
  757. .mode = table->mode,
  758. .extra1 = &zero,
  759. .extra2 = &label_limit,
  760. };
  761. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  762. if (write && ret == 0)
  763. ret = resize_platform_label_table(net, platform_labels);
  764. return ret;
  765. }
  766. static struct ctl_table mpls_table[] = {
  767. {
  768. .procname = "platform_labels",
  769. .data = NULL,
  770. .maxlen = sizeof(int),
  771. .mode = 0644,
  772. .proc_handler = mpls_platform_labels,
  773. },
  774. { }
  775. };
  776. static int mpls_net_init(struct net *net)
  777. {
  778. struct ctl_table *table;
  779. net->mpls.platform_labels = 0;
  780. net->mpls.platform_label = NULL;
  781. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  782. if (table == NULL)
  783. return -ENOMEM;
  784. table[0].data = net;
  785. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  786. if (net->mpls.ctl == NULL)
  787. return -ENOMEM;
  788. return 0;
  789. }
  790. static void mpls_net_exit(struct net *net)
  791. {
  792. struct mpls_route __rcu **platform_label;
  793. size_t platform_labels;
  794. struct ctl_table *table;
  795. unsigned int index;
  796. table = net->mpls.ctl->ctl_table_arg;
  797. unregister_net_sysctl_table(net->mpls.ctl);
  798. kfree(table);
  799. /* An rcu grace period has passed since there was a device in
  800. * the network namespace (and thus the last in flight packet)
  801. * left this network namespace. This is because
  802. * unregister_netdevice_many and netdev_run_todo has completed
  803. * for each network device that was in this network namespace.
  804. *
  805. * As such no additional rcu synchronization is necessary when
  806. * freeing the platform_label table.
  807. */
  808. rtnl_lock();
  809. platform_label = rtnl_dereference(net->mpls.platform_label);
  810. platform_labels = net->mpls.platform_labels;
  811. for (index = 0; index < platform_labels; index++) {
  812. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  813. RCU_INIT_POINTER(platform_label[index], NULL);
  814. mpls_rt_free(rt);
  815. }
  816. rtnl_unlock();
  817. kvfree(platform_label);
  818. }
  819. static struct pernet_operations mpls_net_ops = {
  820. .init = mpls_net_init,
  821. .exit = mpls_net_exit,
  822. };
  823. static int __init mpls_init(void)
  824. {
  825. int err;
  826. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  827. err = register_pernet_subsys(&mpls_net_ops);
  828. if (err)
  829. goto out;
  830. err = register_netdevice_notifier(&mpls_dev_notifier);
  831. if (err)
  832. goto out_unregister_pernet;
  833. dev_add_pack(&mpls_packet_type);
  834. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  835. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  836. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  837. err = 0;
  838. out:
  839. return err;
  840. out_unregister_pernet:
  841. unregister_pernet_subsys(&mpls_net_ops);
  842. goto out;
  843. }
  844. module_init(mpls_init);
  845. static void __exit mpls_exit(void)
  846. {
  847. rtnl_unregister_all(PF_MPLS);
  848. dev_remove_pack(&mpls_packet_type);
  849. unregister_netdevice_notifier(&mpls_dev_notifier);
  850. unregister_pernet_subsys(&mpls_net_ops);
  851. }
  852. module_exit(mpls_exit);
  853. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  854. MODULE_LICENSE("GPL v2");
  855. MODULE_ALIAS_NETPROTO(PF_MPLS);