af_mpls.c 27 KB

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