af_mpls.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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/nospec.h>
  11. #include <linux/vmalloc.h>
  12. #include <net/ip.h>
  13. #include <net/dst.h>
  14. #include <net/sock.h>
  15. #include <net/arp.h>
  16. #include <net/ip_fib.h>
  17. #include <net/netevent.h>
  18. #include <net/netns/generic.h>
  19. #if IS_ENABLED(CONFIG_IPV6)
  20. #include <net/ipv6.h>
  21. #include <net/addrconf.h>
  22. #endif
  23. #include <net/nexthop.h>
  24. #include "internal.h"
  25. /* Maximum number of labels to look ahead at when selecting a path of
  26. * a multipath route
  27. */
  28. #define MAX_MP_SELECT_LABELS 4
  29. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  30. static int zero = 0;
  31. static int label_limit = (1 << 20) - 1;
  32. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  33. struct nlmsghdr *nlh, struct net *net, u32 portid,
  34. unsigned int nlm_flags);
  35. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  36. {
  37. struct mpls_route *rt = NULL;
  38. if (index < net->mpls.platform_labels) {
  39. struct mpls_route __rcu **platform_label =
  40. rcu_dereference(net->mpls.platform_label);
  41. rt = rcu_dereference(platform_label[index]);
  42. }
  43. return rt;
  44. }
  45. static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
  46. {
  47. return rcu_dereference_rtnl(dev->mpls_ptr);
  48. }
  49. bool mpls_output_possible(const struct net_device *dev)
  50. {
  51. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  52. }
  53. EXPORT_SYMBOL_GPL(mpls_output_possible);
  54. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  55. {
  56. u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
  57. int nh_index = nh - rt->rt_nh;
  58. return nh0_via + rt->rt_max_alen * nh_index;
  59. }
  60. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  61. const struct mpls_nh *nh)
  62. {
  63. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  64. }
  65. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  66. {
  67. /* The size of the layer 2.5 labels to be added for this route */
  68. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  69. }
  70. unsigned int mpls_dev_mtu(const struct net_device *dev)
  71. {
  72. /* The amount of data the layer 2 frame can hold */
  73. return dev->mtu;
  74. }
  75. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  76. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  77. {
  78. if (skb->len <= mtu)
  79. return false;
  80. if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
  81. return false;
  82. return true;
  83. }
  84. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  85. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  86. struct sk_buff *skb, bool bos)
  87. {
  88. struct mpls_entry_decoded dec;
  89. struct mpls_shim_hdr *hdr;
  90. bool eli_seen = false;
  91. int label_index;
  92. int nh_index = 0;
  93. u32 hash = 0;
  94. /* No need to look further into packet if there's only
  95. * one path
  96. */
  97. if (rt->rt_nhn == 1)
  98. goto out;
  99. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
  100. label_index++) {
  101. if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
  102. break;
  103. /* Read and decode the current label */
  104. hdr = mpls_hdr(skb) + label_index;
  105. dec = mpls_entry_decode(hdr);
  106. /* RFC6790 - reserved labels MUST NOT be used as keys
  107. * for the load-balancing function
  108. */
  109. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  110. hash = jhash_1word(dec.label, hash);
  111. /* The entropy label follows the entropy label
  112. * indicator, so this means that the entropy
  113. * label was just added to the hash - no need to
  114. * go any deeper either in the label stack or in the
  115. * payload
  116. */
  117. if (eli_seen)
  118. break;
  119. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  120. eli_seen = true;
  121. }
  122. bos = dec.bos;
  123. if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
  124. sizeof(struct iphdr))) {
  125. const struct iphdr *v4hdr;
  126. v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
  127. label_index);
  128. if (v4hdr->version == 4) {
  129. hash = jhash_3words(ntohl(v4hdr->saddr),
  130. ntohl(v4hdr->daddr),
  131. v4hdr->protocol, hash);
  132. } else if (v4hdr->version == 6 &&
  133. pskb_may_pull(skb, sizeof(*hdr) * label_index +
  134. sizeof(struct ipv6hdr))) {
  135. const struct ipv6hdr *v6hdr;
  136. v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
  137. label_index);
  138. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  139. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  140. hash = jhash_1word(v6hdr->nexthdr, hash);
  141. }
  142. }
  143. }
  144. nh_index = hash % rt->rt_nhn;
  145. out:
  146. return &rt->rt_nh[nh_index];
  147. }
  148. static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
  149. struct mpls_entry_decoded dec)
  150. {
  151. enum mpls_payload_type payload_type;
  152. bool success = false;
  153. /* The IPv4 code below accesses through the IPv4 header
  154. * checksum, which is 12 bytes into the packet.
  155. * The IPv6 code below accesses through the IPv6 hop limit
  156. * which is 8 bytes into the packet.
  157. *
  158. * For all supported cases there should always be at least 12
  159. * bytes of packet data present. The IPv4 header is 20 bytes
  160. * without options and the IPv6 header is always 40 bytes
  161. * long.
  162. */
  163. if (!pskb_may_pull(skb, 12))
  164. return false;
  165. payload_type = rt->rt_payload_type;
  166. if (payload_type == MPT_UNSPEC)
  167. payload_type = ip_hdr(skb)->version;
  168. switch (payload_type) {
  169. case MPT_IPV4: {
  170. struct iphdr *hdr4 = ip_hdr(skb);
  171. skb->protocol = htons(ETH_P_IP);
  172. csum_replace2(&hdr4->check,
  173. htons(hdr4->ttl << 8),
  174. htons(dec.ttl << 8));
  175. hdr4->ttl = dec.ttl;
  176. success = true;
  177. break;
  178. }
  179. case MPT_IPV6: {
  180. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  181. skb->protocol = htons(ETH_P_IPV6);
  182. hdr6->hop_limit = dec.ttl;
  183. success = true;
  184. break;
  185. }
  186. case MPT_UNSPEC:
  187. break;
  188. }
  189. return success;
  190. }
  191. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  192. struct packet_type *pt, struct net_device *orig_dev)
  193. {
  194. struct net *net = dev_net(dev);
  195. struct mpls_shim_hdr *hdr;
  196. struct mpls_route *rt;
  197. struct mpls_nh *nh;
  198. struct mpls_entry_decoded dec;
  199. struct net_device *out_dev;
  200. struct mpls_dev *mdev;
  201. unsigned int hh_len;
  202. unsigned int new_header_size;
  203. unsigned int mtu;
  204. int err;
  205. /* Careful this entire function runs inside of an rcu critical section */
  206. mdev = mpls_dev_get(dev);
  207. if (!mdev || !mdev->input_enabled)
  208. goto drop;
  209. if (skb->pkt_type != PACKET_HOST)
  210. goto drop;
  211. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  212. goto drop;
  213. if (!pskb_may_pull(skb, sizeof(*hdr)))
  214. goto drop;
  215. /* Read and decode the label */
  216. hdr = mpls_hdr(skb);
  217. dec = mpls_entry_decode(hdr);
  218. /* Pop the label */
  219. skb_pull(skb, sizeof(*hdr));
  220. skb_reset_network_header(skb);
  221. skb_orphan(skb);
  222. rt = mpls_route_input_rcu(net, dec.label);
  223. if (!rt)
  224. goto drop;
  225. nh = mpls_select_multipath(rt, skb, dec.bos);
  226. if (!nh)
  227. goto drop;
  228. /* Find the output device */
  229. out_dev = rcu_dereference(nh->nh_dev);
  230. if (!mpls_output_possible(out_dev))
  231. goto drop;
  232. if (skb_warn_if_lro(skb))
  233. goto drop;
  234. skb_forward_csum(skb);
  235. /* Verify ttl is valid */
  236. if (dec.ttl <= 1)
  237. goto drop;
  238. dec.ttl -= 1;
  239. /* Verify the destination can hold the packet */
  240. new_header_size = mpls_nh_header_size(nh);
  241. mtu = mpls_dev_mtu(out_dev);
  242. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  243. goto drop;
  244. hh_len = LL_RESERVED_SPACE(out_dev);
  245. if (!out_dev->header_ops)
  246. hh_len = 0;
  247. /* Ensure there is enough space for the headers in the skb */
  248. if (skb_cow(skb, hh_len + new_header_size))
  249. goto drop;
  250. skb->dev = out_dev;
  251. skb->protocol = htons(ETH_P_MPLS_UC);
  252. if (unlikely(!new_header_size && dec.bos)) {
  253. /* Penultimate hop popping */
  254. if (!mpls_egress(rt, skb, dec))
  255. goto drop;
  256. } else {
  257. bool bos;
  258. int i;
  259. skb_push(skb, new_header_size);
  260. skb_reset_network_header(skb);
  261. /* Push the new labels */
  262. hdr = mpls_hdr(skb);
  263. bos = dec.bos;
  264. for (i = nh->nh_labels - 1; i >= 0; i--) {
  265. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  266. dec.ttl, 0, bos);
  267. bos = false;
  268. }
  269. }
  270. /* If via wasn't specified then send out using device address */
  271. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  272. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  273. out_dev->dev_addr, skb);
  274. else
  275. err = neigh_xmit(nh->nh_via_table, out_dev,
  276. mpls_nh_via(rt, nh), skb);
  277. if (err)
  278. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  279. __func__, err);
  280. return 0;
  281. drop:
  282. kfree_skb(skb);
  283. return NET_RX_DROP;
  284. }
  285. static struct packet_type mpls_packet_type __read_mostly = {
  286. .type = cpu_to_be16(ETH_P_MPLS_UC),
  287. .func = mpls_forward,
  288. };
  289. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  290. [RTA_DST] = { .type = NLA_U32 },
  291. [RTA_OIF] = { .type = NLA_U32 },
  292. };
  293. struct mpls_route_config {
  294. u32 rc_protocol;
  295. u32 rc_ifindex;
  296. u8 rc_via_table;
  297. u8 rc_via_alen;
  298. u8 rc_via[MAX_VIA_ALEN];
  299. u32 rc_label;
  300. u8 rc_output_labels;
  301. u32 rc_output_label[MAX_NEW_LABELS];
  302. u32 rc_nlflags;
  303. enum mpls_payload_type rc_payload_type;
  304. struct nl_info rc_nlinfo;
  305. struct rtnexthop *rc_mp;
  306. int rc_mp_len;
  307. };
  308. static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
  309. {
  310. u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
  311. struct mpls_route *rt;
  312. rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
  313. VIA_ALEN_ALIGN) +
  314. num_nh * max_alen_aligned,
  315. GFP_KERNEL);
  316. if (rt) {
  317. rt->rt_nhn = num_nh;
  318. rt->rt_max_alen = max_alen_aligned;
  319. }
  320. return rt;
  321. }
  322. static void mpls_rt_free(struct mpls_route *rt)
  323. {
  324. if (rt)
  325. kfree_rcu(rt, rt_rcu);
  326. }
  327. static void mpls_notify_route(struct net *net, unsigned index,
  328. struct mpls_route *old, struct mpls_route *new,
  329. const struct nl_info *info)
  330. {
  331. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  332. unsigned portid = info ? info->portid : 0;
  333. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  334. struct mpls_route *rt = new ? new : old;
  335. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  336. /* Ignore reserved labels for now */
  337. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  338. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  339. }
  340. static void mpls_route_update(struct net *net, unsigned index,
  341. struct mpls_route *new,
  342. const struct nl_info *info)
  343. {
  344. struct mpls_route __rcu **platform_label;
  345. struct mpls_route *rt;
  346. ASSERT_RTNL();
  347. platform_label = rtnl_dereference(net->mpls.platform_label);
  348. rt = rtnl_dereference(platform_label[index]);
  349. rcu_assign_pointer(platform_label[index], new);
  350. mpls_notify_route(net, index, rt, new, info);
  351. /* If we removed a route free it now */
  352. mpls_rt_free(rt);
  353. }
  354. static unsigned find_free_label(struct net *net)
  355. {
  356. struct mpls_route __rcu **platform_label;
  357. size_t platform_labels;
  358. unsigned index;
  359. platform_label = rtnl_dereference(net->mpls.platform_label);
  360. platform_labels = net->mpls.platform_labels;
  361. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  362. index++) {
  363. if (!rtnl_dereference(platform_label[index]))
  364. return index;
  365. }
  366. return LABEL_NOT_SPECIFIED;
  367. }
  368. #if IS_ENABLED(CONFIG_INET)
  369. static struct net_device *inet_fib_lookup_dev(struct net *net,
  370. const void *addr)
  371. {
  372. struct net_device *dev;
  373. struct rtable *rt;
  374. struct in_addr daddr;
  375. memcpy(&daddr, addr, sizeof(struct in_addr));
  376. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  377. if (IS_ERR(rt))
  378. return ERR_CAST(rt);
  379. dev = rt->dst.dev;
  380. dev_hold(dev);
  381. ip_rt_put(rt);
  382. return dev;
  383. }
  384. #else
  385. static struct net_device *inet_fib_lookup_dev(struct net *net,
  386. const void *addr)
  387. {
  388. return ERR_PTR(-EAFNOSUPPORT);
  389. }
  390. #endif
  391. #if IS_ENABLED(CONFIG_IPV6)
  392. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  393. const void *addr)
  394. {
  395. struct net_device *dev;
  396. struct dst_entry *dst;
  397. struct flowi6 fl6;
  398. int err;
  399. if (!ipv6_stub)
  400. return ERR_PTR(-EAFNOSUPPORT);
  401. memset(&fl6, 0, sizeof(fl6));
  402. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  403. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  404. if (err)
  405. return ERR_PTR(err);
  406. dev = dst->dev;
  407. dev_hold(dev);
  408. dst_release(dst);
  409. return dev;
  410. }
  411. #else
  412. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  413. const void *addr)
  414. {
  415. return ERR_PTR(-EAFNOSUPPORT);
  416. }
  417. #endif
  418. static struct net_device *find_outdev(struct net *net,
  419. struct mpls_route *rt,
  420. struct mpls_nh *nh, int oif)
  421. {
  422. struct net_device *dev = NULL;
  423. if (!oif) {
  424. switch (nh->nh_via_table) {
  425. case NEIGH_ARP_TABLE:
  426. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  427. break;
  428. case NEIGH_ND_TABLE:
  429. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  430. break;
  431. case NEIGH_LINK_TABLE:
  432. break;
  433. }
  434. } else {
  435. dev = dev_get_by_index(net, oif);
  436. }
  437. if (!dev)
  438. return ERR_PTR(-ENODEV);
  439. if (IS_ERR(dev))
  440. return dev;
  441. /* The caller is holding rtnl anyways, so release the dev reference */
  442. dev_put(dev);
  443. return dev;
  444. }
  445. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  446. struct mpls_nh *nh, int oif)
  447. {
  448. struct net_device *dev = NULL;
  449. int err = -ENODEV;
  450. dev = find_outdev(net, rt, nh, oif);
  451. if (IS_ERR(dev)) {
  452. err = PTR_ERR(dev);
  453. dev = NULL;
  454. goto errout;
  455. }
  456. /* Ensure this is a supported device */
  457. err = -EINVAL;
  458. if (!mpls_dev_get(dev))
  459. goto errout;
  460. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  461. (dev->addr_len != nh->nh_via_alen))
  462. goto errout;
  463. RCU_INIT_POINTER(nh->nh_dev, dev);
  464. return 0;
  465. errout:
  466. return err;
  467. }
  468. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  469. struct mpls_route *rt)
  470. {
  471. struct net *net = cfg->rc_nlinfo.nl_net;
  472. struct mpls_nh *nh = rt->rt_nh;
  473. int err;
  474. int i;
  475. if (!nh)
  476. return -ENOMEM;
  477. err = -EINVAL;
  478. /* Ensure only a supported number of labels are present */
  479. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  480. goto errout;
  481. nh->nh_labels = cfg->rc_output_labels;
  482. for (i = 0; i < nh->nh_labels; i++)
  483. nh->nh_label[i] = cfg->rc_output_label[i];
  484. nh->nh_via_table = cfg->rc_via_table;
  485. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  486. nh->nh_via_alen = cfg->rc_via_alen;
  487. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  488. if (err)
  489. goto errout;
  490. return 0;
  491. errout:
  492. return err;
  493. }
  494. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  495. struct mpls_nh *nh, int oif,
  496. struct nlattr *via, struct nlattr *newdst)
  497. {
  498. int err = -ENOMEM;
  499. if (!nh)
  500. goto errout;
  501. if (newdst) {
  502. err = nla_get_labels(newdst, MAX_NEW_LABELS,
  503. &nh->nh_labels, nh->nh_label);
  504. if (err)
  505. goto errout;
  506. }
  507. if (via) {
  508. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  509. __mpls_nh_via(rt, nh));
  510. if (err)
  511. goto errout;
  512. } else {
  513. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  514. }
  515. err = mpls_nh_assign_dev(net, rt, nh, oif);
  516. if (err)
  517. goto errout;
  518. return 0;
  519. errout:
  520. return err;
  521. }
  522. static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  523. u8 cfg_via_alen, u8 *max_via_alen)
  524. {
  525. int nhs = 0;
  526. int remaining = len;
  527. if (!rtnh) {
  528. *max_via_alen = cfg_via_alen;
  529. return 1;
  530. }
  531. *max_via_alen = 0;
  532. while (rtnh_ok(rtnh, remaining)) {
  533. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  534. int attrlen;
  535. attrlen = rtnh_attrlen(rtnh);
  536. nla = nla_find(attrs, attrlen, RTA_VIA);
  537. if (nla && nla_len(nla) >=
  538. offsetof(struct rtvia, rtvia_addr)) {
  539. int via_alen = nla_len(nla) -
  540. offsetof(struct rtvia, rtvia_addr);
  541. if (via_alen <= MAX_VIA_ALEN)
  542. *max_via_alen = max_t(u16, *max_via_alen,
  543. via_alen);
  544. }
  545. nhs++;
  546. rtnh = rtnh_next(rtnh, &remaining);
  547. }
  548. /* leftover implies invalid nexthop configuration, discard it */
  549. return remaining > 0 ? 0 : nhs;
  550. }
  551. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  552. struct mpls_route *rt)
  553. {
  554. struct rtnexthop *rtnh = cfg->rc_mp;
  555. struct nlattr *nla_via, *nla_newdst;
  556. int remaining = cfg->rc_mp_len;
  557. int nhs = 0;
  558. int err = 0;
  559. change_nexthops(rt) {
  560. int attrlen;
  561. nla_via = NULL;
  562. nla_newdst = NULL;
  563. err = -EINVAL;
  564. if (!rtnh_ok(rtnh, remaining))
  565. goto errout;
  566. /* neither weighted multipath nor any flags
  567. * are supported
  568. */
  569. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  570. goto errout;
  571. attrlen = rtnh_attrlen(rtnh);
  572. if (attrlen > 0) {
  573. struct nlattr *attrs = rtnh_attrs(rtnh);
  574. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  575. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  576. }
  577. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  578. rtnh->rtnh_ifindex, nla_via,
  579. nla_newdst);
  580. if (err)
  581. goto errout;
  582. rtnh = rtnh_next(rtnh, &remaining);
  583. nhs++;
  584. } endfor_nexthops(rt);
  585. rt->rt_nhn = nhs;
  586. return 0;
  587. errout:
  588. return err;
  589. }
  590. static bool mpls_label_ok(struct net *net, unsigned int *index)
  591. {
  592. bool is_ok = true;
  593. /* Reserved labels may not be set */
  594. if (*index < MPLS_LABEL_FIRST_UNRESERVED)
  595. is_ok = false;
  596. /* The full 20 bit range may not be supported. */
  597. if (is_ok && *index >= net->mpls.platform_labels)
  598. is_ok = false;
  599. *index = array_index_nospec(*index, net->mpls.platform_labels);
  600. return is_ok;
  601. }
  602. static int mpls_route_add(struct mpls_route_config *cfg)
  603. {
  604. struct mpls_route __rcu **platform_label;
  605. struct net *net = cfg->rc_nlinfo.nl_net;
  606. struct mpls_route *rt, *old;
  607. int err = -EINVAL;
  608. u8 max_via_alen;
  609. unsigned index;
  610. int nhs;
  611. index = cfg->rc_label;
  612. /* If a label was not specified during insert pick one */
  613. if ((index == LABEL_NOT_SPECIFIED) &&
  614. (cfg->rc_nlflags & NLM_F_CREATE)) {
  615. index = find_free_label(net);
  616. }
  617. if (!mpls_label_ok(net, &index))
  618. goto errout;
  619. /* Append makes no sense with mpls */
  620. err = -EOPNOTSUPP;
  621. if (cfg->rc_nlflags & NLM_F_APPEND)
  622. goto errout;
  623. err = -EEXIST;
  624. platform_label = rtnl_dereference(net->mpls.platform_label);
  625. old = rtnl_dereference(platform_label[index]);
  626. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  627. goto errout;
  628. err = -EEXIST;
  629. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  630. goto errout;
  631. err = -ENOENT;
  632. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  633. goto errout;
  634. err = -EINVAL;
  635. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  636. cfg->rc_via_alen, &max_via_alen);
  637. if (nhs == 0)
  638. goto errout;
  639. err = -ENOMEM;
  640. rt = mpls_rt_alloc(nhs, max_via_alen);
  641. if (!rt)
  642. goto errout;
  643. rt->rt_protocol = cfg->rc_protocol;
  644. rt->rt_payload_type = cfg->rc_payload_type;
  645. if (cfg->rc_mp)
  646. err = mpls_nh_build_multi(cfg, rt);
  647. else
  648. err = mpls_nh_build_from_cfg(cfg, rt);
  649. if (err)
  650. goto freert;
  651. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  652. return 0;
  653. freert:
  654. mpls_rt_free(rt);
  655. errout:
  656. return err;
  657. }
  658. static int mpls_route_del(struct mpls_route_config *cfg)
  659. {
  660. struct net *net = cfg->rc_nlinfo.nl_net;
  661. unsigned index;
  662. int err = -EINVAL;
  663. index = cfg->rc_label;
  664. if (!mpls_label_ok(net, &index))
  665. goto errout;
  666. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  667. err = 0;
  668. errout:
  669. return err;
  670. }
  671. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  672. (&((struct mpls_dev *)0)->field)
  673. static const struct ctl_table mpls_dev_table[] = {
  674. {
  675. .procname = "input",
  676. .maxlen = sizeof(int),
  677. .mode = 0644,
  678. .proc_handler = proc_dointvec,
  679. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  680. },
  681. { }
  682. };
  683. static int mpls_dev_sysctl_register(struct net_device *dev,
  684. struct mpls_dev *mdev)
  685. {
  686. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  687. struct ctl_table *table;
  688. int i;
  689. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  690. if (!table)
  691. goto out;
  692. /* Table data contains only offsets relative to the base of
  693. * the mdev at this point, so make them absolute.
  694. */
  695. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
  696. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  697. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  698. mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
  699. if (!mdev->sysctl)
  700. goto free;
  701. return 0;
  702. free:
  703. kfree(table);
  704. out:
  705. return -ENOBUFS;
  706. }
  707. static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
  708. {
  709. struct ctl_table *table;
  710. table = mdev->sysctl->ctl_table_arg;
  711. unregister_net_sysctl_table(mdev->sysctl);
  712. kfree(table);
  713. }
  714. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  715. {
  716. struct mpls_dev *mdev;
  717. int err = -ENOMEM;
  718. ASSERT_RTNL();
  719. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  720. if (!mdev)
  721. return ERR_PTR(err);
  722. err = mpls_dev_sysctl_register(dev, mdev);
  723. if (err)
  724. goto free;
  725. rcu_assign_pointer(dev->mpls_ptr, mdev);
  726. return mdev;
  727. free:
  728. kfree(mdev);
  729. return ERR_PTR(err);
  730. }
  731. static void mpls_ifdown(struct net_device *dev)
  732. {
  733. struct mpls_route __rcu **platform_label;
  734. struct net *net = dev_net(dev);
  735. struct mpls_dev *mdev;
  736. unsigned index;
  737. platform_label = rtnl_dereference(net->mpls.platform_label);
  738. for (index = 0; index < net->mpls.platform_labels; index++) {
  739. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  740. if (!rt)
  741. continue;
  742. for_nexthops(rt) {
  743. if (rtnl_dereference(nh->nh_dev) != dev)
  744. continue;
  745. nh->nh_dev = NULL;
  746. } endfor_nexthops(rt);
  747. }
  748. mdev = mpls_dev_get(dev);
  749. if (!mdev)
  750. return;
  751. mpls_dev_sysctl_unregister(mdev);
  752. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  753. kfree_rcu(mdev, rcu);
  754. }
  755. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  756. void *ptr)
  757. {
  758. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  759. struct mpls_dev *mdev;
  760. switch(event) {
  761. case NETDEV_REGISTER:
  762. /* For now just support ethernet devices */
  763. if ((dev->type == ARPHRD_ETHER) ||
  764. (dev->type == ARPHRD_LOOPBACK)) {
  765. mdev = mpls_add_dev(dev);
  766. if (IS_ERR(mdev))
  767. return notifier_from_errno(PTR_ERR(mdev));
  768. }
  769. break;
  770. case NETDEV_UNREGISTER:
  771. mpls_ifdown(dev);
  772. break;
  773. case NETDEV_CHANGENAME:
  774. mdev = mpls_dev_get(dev);
  775. if (mdev) {
  776. int err;
  777. mpls_dev_sysctl_unregister(mdev);
  778. err = mpls_dev_sysctl_register(dev, mdev);
  779. if (err)
  780. return notifier_from_errno(err);
  781. }
  782. break;
  783. }
  784. return NOTIFY_OK;
  785. }
  786. static struct notifier_block mpls_dev_notifier = {
  787. .notifier_call = mpls_dev_notify,
  788. };
  789. static int nla_put_via(struct sk_buff *skb,
  790. u8 table, const void *addr, int alen)
  791. {
  792. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  793. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  794. };
  795. struct nlattr *nla;
  796. struct rtvia *via;
  797. int family = AF_UNSPEC;
  798. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  799. if (!nla)
  800. return -EMSGSIZE;
  801. if (table <= NEIGH_NR_TABLES)
  802. family = table_to_family[table];
  803. via = nla_data(nla);
  804. via->rtvia_family = family;
  805. memcpy(via->rtvia_addr, addr, alen);
  806. return 0;
  807. }
  808. int nla_put_labels(struct sk_buff *skb, int attrtype,
  809. u8 labels, const u32 label[])
  810. {
  811. struct nlattr *nla;
  812. struct mpls_shim_hdr *nla_label;
  813. bool bos;
  814. int i;
  815. nla = nla_reserve(skb, attrtype, labels*4);
  816. if (!nla)
  817. return -EMSGSIZE;
  818. nla_label = nla_data(nla);
  819. bos = true;
  820. for (i = labels - 1; i >= 0; i--) {
  821. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  822. bos = false;
  823. }
  824. return 0;
  825. }
  826. EXPORT_SYMBOL_GPL(nla_put_labels);
  827. int nla_get_labels(const struct nlattr *nla,
  828. u32 max_labels, u8 *labels, u32 label[])
  829. {
  830. unsigned len = nla_len(nla);
  831. unsigned nla_labels;
  832. struct mpls_shim_hdr *nla_label;
  833. bool bos;
  834. int i;
  835. /* len needs to be an even multiple of 4 (the label size) */
  836. if (len & 3)
  837. return -EINVAL;
  838. /* Limit the number of new labels allowed */
  839. nla_labels = len/4;
  840. if (nla_labels > max_labels)
  841. return -EINVAL;
  842. nla_label = nla_data(nla);
  843. bos = true;
  844. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  845. struct mpls_entry_decoded dec;
  846. dec = mpls_entry_decode(nla_label + i);
  847. /* Ensure the bottom of stack flag is properly set
  848. * and ttl and tc are both clear.
  849. */
  850. if ((dec.bos != bos) || dec.ttl || dec.tc)
  851. return -EINVAL;
  852. switch (dec.label) {
  853. case MPLS_LABEL_IMPLNULL:
  854. /* RFC3032: This is a label that an LSR may
  855. * assign and distribute, but which never
  856. * actually appears in the encapsulation.
  857. */
  858. return -EINVAL;
  859. }
  860. label[i] = dec.label;
  861. }
  862. *labels = nla_labels;
  863. return 0;
  864. }
  865. EXPORT_SYMBOL_GPL(nla_get_labels);
  866. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  867. u8 *via_table, u8 via_addr[])
  868. {
  869. struct rtvia *via = nla_data(nla);
  870. int err = -EINVAL;
  871. int alen;
  872. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  873. goto errout;
  874. alen = nla_len(nla) -
  875. offsetof(struct rtvia, rtvia_addr);
  876. if (alen > MAX_VIA_ALEN)
  877. goto errout;
  878. /* Validate the address family */
  879. switch (via->rtvia_family) {
  880. case AF_PACKET:
  881. *via_table = NEIGH_LINK_TABLE;
  882. break;
  883. case AF_INET:
  884. *via_table = NEIGH_ARP_TABLE;
  885. if (alen != 4)
  886. goto errout;
  887. break;
  888. case AF_INET6:
  889. *via_table = NEIGH_ND_TABLE;
  890. if (alen != 16)
  891. goto errout;
  892. break;
  893. default:
  894. /* Unsupported address family */
  895. goto errout;
  896. }
  897. memcpy(via_addr, via->rtvia_addr, alen);
  898. *via_alen = alen;
  899. err = 0;
  900. errout:
  901. return err;
  902. }
  903. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  904. struct mpls_route_config *cfg)
  905. {
  906. struct rtmsg *rtm;
  907. struct nlattr *tb[RTA_MAX+1];
  908. int index;
  909. int err;
  910. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  911. if (err < 0)
  912. goto errout;
  913. err = -EINVAL;
  914. rtm = nlmsg_data(nlh);
  915. memset(cfg, 0, sizeof(*cfg));
  916. if (rtm->rtm_family != AF_MPLS)
  917. goto errout;
  918. if (rtm->rtm_dst_len != 20)
  919. goto errout;
  920. if (rtm->rtm_src_len != 0)
  921. goto errout;
  922. if (rtm->rtm_tos != 0)
  923. goto errout;
  924. if (rtm->rtm_table != RT_TABLE_MAIN)
  925. goto errout;
  926. /* Any value is acceptable for rtm_protocol */
  927. /* As mpls uses destination specific addresses
  928. * (or source specific address in the case of multicast)
  929. * all addresses have universal scope.
  930. */
  931. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  932. goto errout;
  933. if (rtm->rtm_type != RTN_UNICAST)
  934. goto errout;
  935. if (rtm->rtm_flags != 0)
  936. goto errout;
  937. cfg->rc_label = LABEL_NOT_SPECIFIED;
  938. cfg->rc_protocol = rtm->rtm_protocol;
  939. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  940. cfg->rc_nlflags = nlh->nlmsg_flags;
  941. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  942. cfg->rc_nlinfo.nlh = nlh;
  943. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  944. for (index = 0; index <= RTA_MAX; index++) {
  945. struct nlattr *nla = tb[index];
  946. if (!nla)
  947. continue;
  948. switch(index) {
  949. case RTA_OIF:
  950. cfg->rc_ifindex = nla_get_u32(nla);
  951. break;
  952. case RTA_NEWDST:
  953. if (nla_get_labels(nla, MAX_NEW_LABELS,
  954. &cfg->rc_output_labels,
  955. cfg->rc_output_label))
  956. goto errout;
  957. break;
  958. case RTA_DST:
  959. {
  960. u8 label_count;
  961. if (nla_get_labels(nla, 1, &label_count,
  962. &cfg->rc_label))
  963. goto errout;
  964. if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
  965. &cfg->rc_label))
  966. goto errout;
  967. break;
  968. }
  969. case RTA_VIA:
  970. {
  971. if (nla_get_via(nla, &cfg->rc_via_alen,
  972. &cfg->rc_via_table, cfg->rc_via))
  973. goto errout;
  974. break;
  975. }
  976. case RTA_MULTIPATH:
  977. {
  978. cfg->rc_mp = nla_data(nla);
  979. cfg->rc_mp_len = nla_len(nla);
  980. break;
  981. }
  982. default:
  983. /* Unsupported attribute */
  984. goto errout;
  985. }
  986. }
  987. err = 0;
  988. errout:
  989. return err;
  990. }
  991. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  992. {
  993. struct mpls_route_config cfg;
  994. int err;
  995. err = rtm_to_route_config(skb, nlh, &cfg);
  996. if (err < 0)
  997. return err;
  998. return mpls_route_del(&cfg);
  999. }
  1000. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1001. {
  1002. struct mpls_route_config cfg;
  1003. int err;
  1004. err = rtm_to_route_config(skb, nlh, &cfg);
  1005. if (err < 0)
  1006. return err;
  1007. return mpls_route_add(&cfg);
  1008. }
  1009. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1010. u32 label, struct mpls_route *rt, int flags)
  1011. {
  1012. struct net_device *dev;
  1013. struct nlmsghdr *nlh;
  1014. struct rtmsg *rtm;
  1015. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1016. if (nlh == NULL)
  1017. return -EMSGSIZE;
  1018. rtm = nlmsg_data(nlh);
  1019. rtm->rtm_family = AF_MPLS;
  1020. rtm->rtm_dst_len = 20;
  1021. rtm->rtm_src_len = 0;
  1022. rtm->rtm_tos = 0;
  1023. rtm->rtm_table = RT_TABLE_MAIN;
  1024. rtm->rtm_protocol = rt->rt_protocol;
  1025. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1026. rtm->rtm_type = RTN_UNICAST;
  1027. rtm->rtm_flags = 0;
  1028. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1029. goto nla_put_failure;
  1030. if (rt->rt_nhn == 1) {
  1031. const struct mpls_nh *nh = rt->rt_nh;
  1032. if (nh->nh_labels &&
  1033. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1034. nh->nh_label))
  1035. goto nla_put_failure;
  1036. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1037. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1038. nh->nh_via_alen))
  1039. goto nla_put_failure;
  1040. dev = rtnl_dereference(nh->nh_dev);
  1041. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1042. goto nla_put_failure;
  1043. } else {
  1044. struct rtnexthop *rtnh;
  1045. struct nlattr *mp;
  1046. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1047. if (!mp)
  1048. goto nla_put_failure;
  1049. for_nexthops(rt) {
  1050. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1051. if (!rtnh)
  1052. goto nla_put_failure;
  1053. dev = rtnl_dereference(nh->nh_dev);
  1054. if (dev)
  1055. rtnh->rtnh_ifindex = dev->ifindex;
  1056. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1057. nh->nh_labels,
  1058. nh->nh_label))
  1059. goto nla_put_failure;
  1060. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1061. nla_put_via(skb, nh->nh_via_table,
  1062. mpls_nh_via(rt, nh),
  1063. nh->nh_via_alen))
  1064. goto nla_put_failure;
  1065. /* length of rtnetlink header + attributes */
  1066. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1067. } endfor_nexthops(rt);
  1068. nla_nest_end(skb, mp);
  1069. }
  1070. nlmsg_end(skb, nlh);
  1071. return 0;
  1072. nla_put_failure:
  1073. nlmsg_cancel(skb, nlh);
  1074. return -EMSGSIZE;
  1075. }
  1076. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1077. {
  1078. struct net *net = sock_net(skb->sk);
  1079. struct mpls_route __rcu **platform_label;
  1080. size_t platform_labels;
  1081. unsigned int index;
  1082. ASSERT_RTNL();
  1083. index = cb->args[0];
  1084. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1085. index = MPLS_LABEL_FIRST_UNRESERVED;
  1086. platform_label = rtnl_dereference(net->mpls.platform_label);
  1087. platform_labels = net->mpls.platform_labels;
  1088. for (; index < platform_labels; index++) {
  1089. struct mpls_route *rt;
  1090. rt = rtnl_dereference(platform_label[index]);
  1091. if (!rt)
  1092. continue;
  1093. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1094. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1095. index, rt, NLM_F_MULTI) < 0)
  1096. break;
  1097. }
  1098. cb->args[0] = index;
  1099. return skb->len;
  1100. }
  1101. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1102. {
  1103. size_t payload =
  1104. NLMSG_ALIGN(sizeof(struct rtmsg))
  1105. + nla_total_size(4); /* RTA_DST */
  1106. if (rt->rt_nhn == 1) {
  1107. struct mpls_nh *nh = rt->rt_nh;
  1108. if (nh->nh_dev)
  1109. payload += nla_total_size(4); /* RTA_OIF */
  1110. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1111. payload += nla_total_size(2 + nh->nh_via_alen);
  1112. if (nh->nh_labels) /* RTA_NEWDST */
  1113. payload += nla_total_size(nh->nh_labels * 4);
  1114. } else {
  1115. /* each nexthop is packed in an attribute */
  1116. size_t nhsize = 0;
  1117. for_nexthops(rt) {
  1118. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1119. /* RTA_VIA */
  1120. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1121. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1122. if (nh->nh_labels)
  1123. nhsize += nla_total_size(nh->nh_labels * 4);
  1124. } endfor_nexthops(rt);
  1125. /* nested attribute */
  1126. payload += nla_total_size(nhsize);
  1127. }
  1128. return payload;
  1129. }
  1130. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1131. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1132. unsigned int nlm_flags)
  1133. {
  1134. struct sk_buff *skb;
  1135. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1136. int err = -ENOBUFS;
  1137. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1138. if (skb == NULL)
  1139. goto errout;
  1140. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1141. if (err < 0) {
  1142. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1143. WARN_ON(err == -EMSGSIZE);
  1144. kfree_skb(skb);
  1145. goto errout;
  1146. }
  1147. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1148. return;
  1149. errout:
  1150. if (err < 0)
  1151. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1152. }
  1153. static int resize_platform_label_table(struct net *net, size_t limit)
  1154. {
  1155. size_t size = sizeof(struct mpls_route *) * limit;
  1156. size_t old_limit;
  1157. size_t cp_size;
  1158. struct mpls_route __rcu **labels = NULL, **old;
  1159. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1160. unsigned index;
  1161. if (size) {
  1162. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1163. if (!labels)
  1164. labels = vzalloc(size);
  1165. if (!labels)
  1166. goto nolabels;
  1167. }
  1168. /* In case the predefined labels need to be populated */
  1169. if (limit > MPLS_LABEL_IPV4NULL) {
  1170. struct net_device *lo = net->loopback_dev;
  1171. rt0 = mpls_rt_alloc(1, lo->addr_len);
  1172. if (!rt0)
  1173. goto nort0;
  1174. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1175. rt0->rt_protocol = RTPROT_KERNEL;
  1176. rt0->rt_payload_type = MPT_IPV4;
  1177. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1178. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1179. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1180. lo->addr_len);
  1181. }
  1182. if (limit > MPLS_LABEL_IPV6NULL) {
  1183. struct net_device *lo = net->loopback_dev;
  1184. rt2 = mpls_rt_alloc(1, lo->addr_len);
  1185. if (!rt2)
  1186. goto nort2;
  1187. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1188. rt2->rt_protocol = RTPROT_KERNEL;
  1189. rt2->rt_payload_type = MPT_IPV6;
  1190. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1191. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1192. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1193. lo->addr_len);
  1194. }
  1195. rtnl_lock();
  1196. /* Remember the original table */
  1197. old = rtnl_dereference(net->mpls.platform_label);
  1198. old_limit = net->mpls.platform_labels;
  1199. /* Free any labels beyond the new table */
  1200. for (index = limit; index < old_limit; index++)
  1201. mpls_route_update(net, index, NULL, NULL);
  1202. /* Copy over the old labels */
  1203. cp_size = size;
  1204. if (old_limit < limit)
  1205. cp_size = old_limit * sizeof(struct mpls_route *);
  1206. memcpy(labels, old, cp_size);
  1207. /* If needed set the predefined labels */
  1208. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1209. (limit > MPLS_LABEL_IPV6NULL)) {
  1210. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1211. rt2 = NULL;
  1212. }
  1213. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1214. (limit > MPLS_LABEL_IPV4NULL)) {
  1215. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1216. rt0 = NULL;
  1217. }
  1218. /* Update the global pointers */
  1219. net->mpls.platform_labels = limit;
  1220. rcu_assign_pointer(net->mpls.platform_label, labels);
  1221. rtnl_unlock();
  1222. mpls_rt_free(rt2);
  1223. mpls_rt_free(rt0);
  1224. if (old) {
  1225. synchronize_rcu();
  1226. kvfree(old);
  1227. }
  1228. return 0;
  1229. nort2:
  1230. mpls_rt_free(rt0);
  1231. nort0:
  1232. kvfree(labels);
  1233. nolabels:
  1234. return -ENOMEM;
  1235. }
  1236. static int mpls_platform_labels(struct ctl_table *table, int write,
  1237. void __user *buffer, size_t *lenp, loff_t *ppos)
  1238. {
  1239. struct net *net = table->data;
  1240. int platform_labels = net->mpls.platform_labels;
  1241. int ret;
  1242. struct ctl_table tmp = {
  1243. .procname = table->procname,
  1244. .data = &platform_labels,
  1245. .maxlen = sizeof(int),
  1246. .mode = table->mode,
  1247. .extra1 = &zero,
  1248. .extra2 = &label_limit,
  1249. };
  1250. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1251. if (write && ret == 0)
  1252. ret = resize_platform_label_table(net, platform_labels);
  1253. return ret;
  1254. }
  1255. static const struct ctl_table mpls_table[] = {
  1256. {
  1257. .procname = "platform_labels",
  1258. .data = NULL,
  1259. .maxlen = sizeof(int),
  1260. .mode = 0644,
  1261. .proc_handler = mpls_platform_labels,
  1262. },
  1263. { }
  1264. };
  1265. static int mpls_net_init(struct net *net)
  1266. {
  1267. struct ctl_table *table;
  1268. net->mpls.platform_labels = 0;
  1269. net->mpls.platform_label = NULL;
  1270. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1271. if (table == NULL)
  1272. return -ENOMEM;
  1273. table[0].data = net;
  1274. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1275. if (net->mpls.ctl == NULL) {
  1276. kfree(table);
  1277. return -ENOMEM;
  1278. }
  1279. return 0;
  1280. }
  1281. static void mpls_net_exit(struct net *net)
  1282. {
  1283. struct mpls_route __rcu **platform_label;
  1284. size_t platform_labels;
  1285. struct ctl_table *table;
  1286. unsigned int index;
  1287. table = net->mpls.ctl->ctl_table_arg;
  1288. unregister_net_sysctl_table(net->mpls.ctl);
  1289. kfree(table);
  1290. /* An rcu grace period has passed since there was a device in
  1291. * the network namespace (and thus the last in flight packet)
  1292. * left this network namespace. This is because
  1293. * unregister_netdevice_many and netdev_run_todo has completed
  1294. * for each network device that was in this network namespace.
  1295. *
  1296. * As such no additional rcu synchronization is necessary when
  1297. * freeing the platform_label table.
  1298. */
  1299. rtnl_lock();
  1300. platform_label = rtnl_dereference(net->mpls.platform_label);
  1301. platform_labels = net->mpls.platform_labels;
  1302. for (index = 0; index < platform_labels; index++) {
  1303. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1304. RCU_INIT_POINTER(platform_label[index], NULL);
  1305. mpls_notify_route(net, index, rt, NULL, NULL);
  1306. mpls_rt_free(rt);
  1307. }
  1308. rtnl_unlock();
  1309. kvfree(platform_label);
  1310. }
  1311. static struct pernet_operations mpls_net_ops = {
  1312. .init = mpls_net_init,
  1313. .exit = mpls_net_exit,
  1314. };
  1315. static int __init mpls_init(void)
  1316. {
  1317. int err;
  1318. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1319. err = register_pernet_subsys(&mpls_net_ops);
  1320. if (err)
  1321. goto out;
  1322. err = register_netdevice_notifier(&mpls_dev_notifier);
  1323. if (err)
  1324. goto out_unregister_pernet;
  1325. dev_add_pack(&mpls_packet_type);
  1326. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1327. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1328. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1329. err = 0;
  1330. out:
  1331. return err;
  1332. out_unregister_pernet:
  1333. unregister_pernet_subsys(&mpls_net_ops);
  1334. goto out;
  1335. }
  1336. module_init(mpls_init);
  1337. static void __exit mpls_exit(void)
  1338. {
  1339. rtnl_unregister_all(PF_MPLS);
  1340. dev_remove_pack(&mpls_packet_type);
  1341. unregister_netdevice_notifier(&mpls_dev_notifier);
  1342. unregister_pernet_subsys(&mpls_net_ops);
  1343. }
  1344. module_exit(mpls_exit);
  1345. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1346. MODULE_LICENSE("GPL v2");
  1347. MODULE_ALIAS_NETPROTO(PF_MPLS);